Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT REVIEW] Add a bunch of typing #1850

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/resource_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
bill.add(RDF.type, FOAF.Agent)
bill.set(RDFS.label, Literal("Bill"))

bill.add(FOAF.knows, bob)
# type error: Argument 2 to "add" of "Resource" has incompatible type "Resource"; expected "Node" [arg-type]
bill.add(FOAF.knows, bob) # type: ignore[arg-type]

# Resources returned when querying are 'auto-boxed' as resources:
print(f"Bill knows: {bill.value(FOAF.knows).value(FOAF.name)}")
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ no_implicit_optional = false
implicit_reexport = false


[[tool.mypy.overrides]]
module = "rdflib.*"
check_untyped_defs = true


[tool.coverage.run]
branch = true
source = ["rdflib"]
Expand Down
23 changes: 23 additions & 0 deletions rdflib/_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# import sys
# from typing import TYPE_CHECKING, Optional, Tuple, TypeVar

# if sys.version_info >= (3, 10):
# from typing import TypeAlias
# else:
# from typing_extensions import TypeAlias

# if TYPE_CHECKING:
# from rdflib.graph import Graph
# from rdflib.term import IdentifiedNode, Identifier

# _SubjectType: TypeAlias = "IdentifiedNode"
# _PredicateType: TypeAlias = "IdentifiedNode"
# _ObjectType: TypeAlias = "Identifier"

# _TripleType = Tuple["_SubjectType", "_PredicateType", "_ObjectType"]
# _QuadType = Tuple["_SubjectType", "_PredicateType", "_ObjectType", "Graph"]
# _TriplePatternType = Tuple[
# Optional["_SubjectType"], Optional["_PredicateType"], Optional["_ObjectType"]
# ]

# _GraphT = TypeVar("_GraphT", bound="Graph")
4 changes: 2 additions & 2 deletions rdflib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def append(self, item: Node) -> Collection:
self.graph.add((end, RDF.rest, RDF.nil))
return self

def __iadd__(self, other: Iterable[Node]):
def __iadd__(self, other: Iterable[Node]) -> Collection:
end = self._end()
self.graph.remove((end, RDF.rest, None))

Expand All @@ -257,7 +257,7 @@ def __iadd__(self, other: Iterable[Node]):
self.graph.add((end, RDF.rest, RDF.nil))
return self

def clear(self):
def clear(self) -> Collection:
container: Optional[Node] = self.uri
graph = self.graph
while container:
Expand Down
5 changes: 4 additions & 1 deletion rdflib/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
and different versions of support libraries.
"""

from __future__ import annotations

import codecs
import re
import warnings
Expand All @@ -20,7 +22,8 @@ def ascii(stream):


def bopen(*args, **kwargs):
return open(*args, mode="rb", **kwargs)
# type error: No overload variant of "open" matches argument types "Tuple[Any, ...]", "str", "Dict[str, Any]"
return open(*args, mode="rb", **kwargs) # type: ignore[call-overload]


long_type = int
Expand Down
13 changes: 9 additions & 4 deletions rdflib/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

from __future__ import annotations

from typing import Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Dict, Optional

if TYPE_CHECKING:
import typing_extensions as te

__all__ = ["Event", "Dispatcher"]

Expand Down Expand Up @@ -59,7 +62,7 @@ class Dispatcher:

_dispatch_map: Optional[Dict[Any, Any]] = None

def set_map(self, amap: Dict[Any, Any]):
def set_map(self, amap: Dict[Any, Any]) -> te.Self:
self._dispatch_map = amap
return self

Expand All @@ -72,12 +75,14 @@ def subscribe(self, event_type, handler):
"""
if self._dispatch_map is None:
self.set_map({})
lst = self._dispatch_map.get(event_type, None)
# type error: error: Item "None" of "Optional[Dict[Any, Any]]" has no attribute "get"
lst = self._dispatch_map.get(event_type, None) # type: ignore[union-attr]
if lst is None:
lst = [handler]
else:
lst.append(handler)
self._dispatch_map[event_type] = lst
# type error: Unsupported target for indexed assignment ("Optional[Dict[Any, Any]]")
self._dispatch_map[event_type] = lst # type: ignore[index]
return self

def dispatch(self, event):
Expand Down
4 changes: 4 additions & 0 deletions rdflib/extras/cmdlineutils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

import codecs
import getopt
import sys
import time
from typing import TextIO, Union

import rdflib
from rdflib.util import guess_format
Expand Down Expand Up @@ -40,6 +43,7 @@ def main(target, _help=_help, options="", stdin=True):
else:
f = None

out: Union[TextIO, codecs.StreamReaderWriter]
if "-o" in dargs:
sys.stderr.write("Output to %s\n" % dargs["-o"])
out = codecs.open(dargs["-o"], "w", "utf-8")
Expand Down
65 changes: 43 additions & 22 deletions rdflib/extras/infixowl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@
>>> print(g.serialize(format='pretty-xml')) # doctest: +SKIP

"""
from __future__ import annotations

import itertools
import logging
from typing import Iterable, Union

from rdflib.collection import Collection
from rdflib.graph import Graph
from rdflib.graph import Graph, _ObjectType
from rdflib.namespace import OWL, RDF, RDFS, XSD, Namespace, NamespaceManager
from rdflib.term import BNode, Identifier, Literal, URIRef, Variable
from rdflib.util import first
Expand Down Expand Up @@ -432,11 +434,11 @@ def replace(self, other):
self.graph.add((s, p, classOrIdentifier(other)))
self.delete()

def _get_type(self):
def _get_type(self) -> Iterable[_ObjectType]:
for _t in self.graph.objects(subject=self.identifier, predicate=RDF.type):
yield _t

def _set_type(self, kind):
def _set_type(self, kind: Union[Individual, Identifier, Iterable[_ObjectType]]):
if not kind:
return
if isinstance(kind, (Individual, Identifier)):
Expand All @@ -462,10 +464,10 @@ def _delete_type(self):

type = property(_get_type, _set_type, _delete_type)

def _get_identifier(self):
def _get_identifier(self) -> Identifier:
return self.__identifier

def _set_identifier(self, i):
def _set_identifier(self, i: Identifier):
assert i
if i != self.__identifier:
oldstatements_out = [
Expand All @@ -492,11 +494,13 @@ def _set_identifier(self, i):

identifier = property(_get_identifier, _set_identifier)

def _get_sameAs(self): # noqa: N802
def _get_sameAs(self) -> Iterable[_ObjectType]: # noqa: N802
for _t in self.graph.objects(subject=self.identifier, predicate=OWL.sameAs):
yield _t

def _set_sameAs(self, term): # noqa: N802
def _set_sameAs( # noqa: N802
self, term: Union[Individual, Identifier, Iterable[_ObjectType]]
):
# if not kind:
# return
if isinstance(term, (Individual, Identifier)):
Expand Down Expand Up @@ -1305,7 +1309,8 @@ def isPrimitive(self): # noqa: N802
# sc = list(self.subClassOf)
ec = list(self.equivalentClass)
for _boolclass, p, rdf_list in self.graph.triples_choices(
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None)
# type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None) # type: ignore[arg-type]
):
ec.append(manchesterSyntax(rdf_list, self.graph, boolean=p))
for _e in ec:
Expand All @@ -1331,7 +1336,8 @@ def __repr__(self, full=False, normalization=True):
sc = list(self.subClassOf)
ec = list(self.equivalentClass)
for _boolclass, p, rdf_list in self.graph.triples_choices(
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None)
# type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None) # type: ignore[arg-type]
):
ec.append(manchesterSyntax(rdf_list, self.graph, boolean=p))
dc = list(self.disjointWith)
Expand All @@ -1340,7 +1346,9 @@ def __repr__(self, full=False, normalization=True):
dc.append(c)
klasskind = ""
label = list(self.graph.objects(self.identifier, RDFS.label))
label = label and "(" + label[0] + ")" or ""
# type error: Incompatible types in assignment (expression has type "str", variable has type "List[Node]")
# type error: Unsupported operand types for + ("str" and "Node")
label = label and "(" + label[0] + ")" or "" # type: ignore[assignment, operator]
if sc:
if full:
scjoin = "\n "
Expand Down Expand Up @@ -1421,7 +1429,9 @@ def __init__(self, rdf_list, members=None, graph=None):
self._rdfList = Collection(
self.graph, BNode(), [classOrIdentifier(m) for m in members]
)
self.graph.add((self.identifier, self._operator, self._rdfList.uri))
# type error: "OWLRDFListProxy" has no attribute "identifier"
# type error: "OWLRDFListProxy" has no attribute "_operator"
self.graph.add((self.identifier, self._operator, self._rdfList.uri)) # type: ignore[attr-defined]

def __eq__(self, other):
"""
Expand All @@ -1439,7 +1449,8 @@ def __eq__(self, other):
return False
return True
else:
return self.identifier == other.identifier
# type error: "OWLRDFListProxy" has no attribute "identifier"
return self.identifier == other.identifier # type: ignore[attr-defined]

# Redirect python list accessors to the underlying Collection instance
def __len__(self):
Expand Down Expand Up @@ -1777,8 +1788,10 @@ def __init__(
elif isinstance(restriction_range, Class):
self.restrictionRange = classOrIdentifier(restriction_range)
else:
self.restrictionRange = first(
self.graph.objects(self.identifier, restriction_type)
# error: Incompatible types in assignment (expression has type "Optional[Identifier]", variable has type "Identifier")
self.restrictionRange = first( # type: ignore[assignment]
# type error: Argument 1 to "first" has incompatible type "Generator[Node, None, None]"; expected "Iterable[Identifier]"
self.graph.objects(self.identifier, restriction_type) # type: ignore[arg-type]
)
if (
self.identifier,
Expand Down Expand Up @@ -1834,7 +1847,8 @@ def __eq__(self, other):
if isinstance(other, Restriction):
return (
other.onProperty == self.onProperty
and other.restriction_range == self.restrictionRange
# type error: "Restriction" has no attribute "restriction_range"; maybe "restrictionRange"?
and other.restriction_range == self.restrictionRange # type: ignore[attr-defined]
)
else:
return False
Expand Down Expand Up @@ -1999,9 +2013,11 @@ def _del_mincardinality(self):

def restrictionKind(self): # noqa: N802
for s, p, o in self.graph.triples_choices(
(self.identifier, self.restrictionKinds, None)
# type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
(self.identifier, self.restrictionKinds, None) # type: ignore[arg-type]
):
return p.split(str(OWL))[-1]
# type error: "Node" has no attribute "split"
return p.split(str(OWL))[-1] # type: ignore[attr-defined]
return None

def __repr__(self):
Expand Down Expand Up @@ -2155,9 +2171,11 @@ def __repr__(self):
% (self.qname, first(self.comment) and first(self.comment) or "")
)
if first(self.inverseOf):
two_link_inverse = first(first(self.inverseOf).inverseOf)
# type error: Item "None" of "Optional[Any]" has no attribute "inverseOf"
two_link_inverse = first(first(self.inverseOf).inverseOf) # type: ignore[union-attr]
if two_link_inverse and two_link_inverse.identifier == self.identifier:
inverserepr = first(self.inverseOf).qname
# type error: Item "None" of "Optional[Any]" has no attribute "qname"
inverserepr = first(self.inverseOf).qname # type: ignore[union-attr]
else:
inverserepr = repr(first(self.inverseOf))
rt.append(
Expand All @@ -2168,7 +2186,8 @@ def __repr__(self):
)
)
for _s, _p, roletype in self.graph.triples_choices(
(
# type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, URIRef, List[URIRef]]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
( # type: ignore[arg-type]
self.identifier,
RDF.type,
[
Expand All @@ -2178,7 +2197,8 @@ def __repr__(self):
],
)
):
rt.append(str(roletype.split(str(OWL))[-1]))
# type error: "Node" has no attribute "split"
rt.append(str(roletype.split(str(OWL))[-1])) # type: ignore[attr-defined]
else:
rt.append(
"DatatypeProperty( %s %s"
Expand Down Expand Up @@ -2228,7 +2248,8 @@ def canonicalName(term, g): # noqa: N802
]
)
)
rt = "\n".join([expr for expr in rt if expr])
# type error: Incompatible types in assignment (expression has type "str", variable has type "List[str]")
rt = "\n".join([expr for expr in rt if expr]) # type: ignore[assignment]
rt += "\n)"
return rt

Expand Down
3 changes: 2 additions & 1 deletion rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ def __getitem__(self, item):
return (s, p, o) in self

elif isinstance(item, (Path, Node)):
return self.predicate_objects(item)
# type error: Argument 1 to "predicate_objects" of "Graph" has incompatible type "Union[Path, Node]"; expected "Optional[Node]"
return self.predicate_objects(item) # type: ignore[arg-type]

else:
raise TypeError(
Expand Down
5 changes: 3 additions & 2 deletions rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, wrapped: str, encoding="utf-8"):
super(BytesIOWrapper, self).__init__()
self.wrapped = wrapped
self.encoding = encoding
self.encoded = None
self.encoded: Optional[BytesIO] = None

def read(self, *args, **kwargs):
if self.encoded is None:
Expand All @@ -81,7 +81,8 @@ def read(self, *args, **kwargs):
def read1(self, *args, **kwargs):
if self.encoded is None:
b = codecs.getencoder(self.encoding)(self.wrapped)
self.encoded = BytesIO(b)
# type error: Argument 1 to "BytesIO" has incompatible type "Tuple[bytes, int]"; expected "Buffer"
self.encoded = BytesIO(b) # type: ignore[arg-type]
return self.encoded.read1(*args, **kwargs)

def readinto(self, *args, **kwargs):
Expand Down
8 changes: 7 additions & 1 deletion rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ def to_rdf(
data: Any,
dataset: Graph,
base: Optional[str] = None,
context_data: Optional[bool] = None,
context_data: Optional[
Union[
List[Union[Dict[str, Any], str, None]],
Dict[str, Any],
str,
]
] = None,
version: Optional[float] = None,
generalized_rdf: bool = False,
allow_lists_of_lists: Optional[bool] = None,
Expand Down
6 changes: 4 additions & 2 deletions rdflib/plugins/parsers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ class BagID(URIRef):
__slots__ = ["li"]

def __init__(self, val):
super(URIRef, self).__init__(val)
# type error: Too many arguments for "__init__" of "object"
super(URIRef, self).__init__(val) # type: ignore[call-arg]
self.li = 0

def next_li(self):
self.li += 1
return RDFNS["_%s" % self.li]
# type error: Type expected within [...]
return RDFNS["_%s" % self.li] # type: ignore[misc]


class ElementHandler:
Expand Down
Loading