Skip to content

Commit

Permalink
Add typing to rdflib.term
Browse files Browse the repository at this point in the history
This adds as much typing as possible to `rdflib.term`.

Other changes:
- Added back `warn_unused_ignores`. I actually thought this was enabled
  but I forgot I disabled it because of some issue on python 3.10.
- Disabled `warn_unused_ignores` only for `rdflib.plugin`. There is an
  ignore in this module which is not needed on python 3.10, this is the
  most targetted way to avoid having that fail the type checking that I
  can think of for now.
- Removed unused type ignores.

This changeset includes no runtime changes.
  • Loading branch information
aucampia committed Jan 22, 2022
1 parent d3f9453 commit 480dd37
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 110 deletions.
4 changes: 2 additions & 2 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ def parse(
parser = plugin.get(format, Parser)()
try:
# TODO FIXME: Parser.parse should have **kwargs argument.
parser.parse(source, self, **args) # type: ignore[call-arg]
parser.parse(source, self, **args)
except SyntaxError as se:
if could_not_guess_format:
raise ParserError(
Expand Down Expand Up @@ -1624,7 +1624,7 @@ def __contains__(self, triple_or_quad):
return True
return False

def add(self, triple_or_quad: Union[Tuple[Node, Node, Node, Optional[Any]], Tuple[Node, Node, Node]]) -> "ConjunctiveGraph": # type: ignore[override]
def add(self, triple_or_quad: Union[Tuple[Node, Node, Node, Optional[Any]], Tuple[Node, Node, Node]]) -> "ConjunctiveGraph":
"""
Add a triple or quad to the store.
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/parsers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def property_element_start(self, name, qname, attrs):
o = URIRef(atts[att])
else:
if datatype is not None:
language = None # type: ignore[unreachable]
language = None
o = Literal(atts[att], language, datatype)

if object is None:
Expand Down
1 change: 1 addition & 0 deletions rdflib/plugins/serializers/trig.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def serialize(
if self.default_context and store.identifier == self.default_context:
self.write(self.indent() + "\n{")
else:
iri: Optional[str]
if isinstance(store.identifier, BNode):
iri = store.identifier.n3()
else:
Expand Down
12 changes: 10 additions & 2 deletions rdflib/plugins/sparql/results/xmlresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ def __init__(self, source, content_type: Optional[str] = None):
for result in results: # type: ignore[union-attr]
r = {}
for binding in result:
r[Variable(binding.get("name"))] = parseTerm(binding[0])
# type error: error: Argument 1 to "Variable" has incompatible type "Union[str, None, Any]"; expected "str"
# NOTE on type error: Element.get() can return None, and
# this will invariably fail if passed into Variable
# constructor as value
r[Variable(binding.get("name"))] = parseTerm(binding[0]) # type: ignore[arg-type] # FIXME
self.bindings.append(r)

self.vars = [
Variable(x.get("name"))
# type error: Argument 1 to "Variable" has incompatible type "Optional[str]"; expected "str"
# NOTE on type error: Element.get() can return None, and this
# will invariably fail if passed into Variable constructor as
# value
Variable(x.get("name")) # type: ignore[arg-type] # FIXME
for x in tree.findall(
"./%shead/%svariable" % (RESULTS_NS_ET, RESULTS_NS_ET)
)
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _node_to_sparql(node) -> str:
return node.n3()


class SPARQLStore(SPARQLConnector, Store): # type: ignore[misc]
class SPARQLStore(SPARQLConnector, Store):
"""An RDFLib store around a SPARQL endpoint
This is context-aware and should work as expected
Expand Down Expand Up @@ -543,7 +543,7 @@ def open(self, configuration: Union[str, Tuple[str, str]], create=False):
Graph("SPARQLStore"), can set the required parameters
"""
if type(configuration) == str:
self.query_endpoint = configuration # type: ignore[assignment]
self.query_endpoint = configuration
elif type(configuration) == tuple:
self.query_endpoint = configuration[0]
self.update_endpoint = configuration[1]
Expand Down
2 changes: 1 addition & 1 deletion rdflib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def namespaces(self):
# See https://stackoverflow.com/q/13243766 and
# https://www.python.org/dev/peps/pep-0255/#why-a-new-keyword-for-yield-why-not-a-builtin-function-instead
if False:
yield None # type: ignore[unreachable]
yield None

# Optional Transactional methods

Expand Down

0 comments on commit 480dd37

Please sign in to comment.