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

Fix errors reported by mypy #1330

Merged
merged 3 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ steps:
- pip install --default-timeout 60 coveralls && export HAS_COVERALLS=1
- python setup.py install
- flake8 --exit-zero rdflib
- mypy --show-error-context --show-error-codes test rdflib examples
- PYTHONWARNINGS=default nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib
- coverage report

Expand All @@ -38,6 +39,7 @@ steps:
- pip install --default-timeout 60 coveralls && export HAS_COVERALLS=1
- python setup.py install
- flake8 --exit-zero rdflib
- mypy --show-error-context --show-error-codes test rdflib examples
- PYTHONWARNINGS=default nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib
- coverage report

Expand All @@ -59,5 +61,6 @@ steps:
- pip install --default-timeout 60 coveralls && export HAS_COVERALLS=1
- python setup.py install
- flake8 --exit-zero rdflib
- mypy --show-error-context --show-error-codes test rdflib examples
- PYTHONWARNINGS=default nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib
- coverage report
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ install:

before_script:
- flake8 --exit-zero rdflib
- mypy --show-error-context --show-error-codes test rdflib examples

script:
- PYTHONWARNINGS=default nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib
Expand Down
8 changes: 4 additions & 4 deletions examples/conjunctive_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@
print("-- %s " % c)

# separate graphs
print(gjohn.serialize(format="n3").decode("utf-8"))
print(gjohn.serialize(format="n3"))
print("===================")
print(gmary.serialize(format="n3").decode("utf-8"))
print(gmary.serialize(format="n3"))
print("===================")

# full graph
print(g.serialize(format="n3").decode("utf-8"))
print(g.serialize(format="n3"))

# query the conjunction of all graphs
xx = None
for x in g[mary : ns.loves / ns.hasCuteName]:
for x in g[mary : ns.loves / ns.hasCuteName]: # type: ignore[misc]
xx = x
print("Q: Who does Mary love?")
print("A: Mary loves {}".format(xx))
8 changes: 4 additions & 4 deletions examples/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@

# Serialize as XML
print("--- start: rdf-xml ---")
print(store.serialize(format="pretty-xml").decode("utf-8"))
print(store.serialize(format="pretty-xml"))
print("--- end: rdf-xml ---\n")

# Serialize as Turtle
print("--- start: turtle ---")
print(store.serialize(format="turtle").decode("utf-8"))
print(store.serialize(format="turtle"))
print("--- end: turtle ---\n")

# Serialize as NTriples
print("--- start: ntriples ---")
print(store.serialize(format="nt").decode("utf-8"))
print(store.serialize(format="nt"))
print("--- end: ntriples ---\n")

# Serialize as JSON-LD
# only if you have the JSON-LD plugin installed!
print("--- start: JSON-LD ---")
print(store.serialize(format="json-ld").decode("utf-8"))
print(store.serialize(format="json-ld"))
print("--- end: JSON-LD ---\n")
2 changes: 0 additions & 2 deletions examples/sleepycat_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
# close when done, otherwise sleepycat will leak lock entries.
graph.close()

graph = None

# reopen the graph

graph = ConjunctiveGraph("Sleepycat")
Expand Down
2 changes: 1 addition & 1 deletion examples/smushing.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
o = newURI.get(o, o) # might be linked to another person
out.add((s, p, o))

print(out.serialize(format="n3").decode("utf-8"))
print(out.serialize(format="n3"))
4 changes: 2 additions & 2 deletions examples/sparqlstore_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

print(
"According to DBPedia, Berlin has a population of {0:,}".format(
int(pop), ",d"
int(pop)
).replace(",", ".")
)

Expand All @@ -27,7 +27,7 @@
for p in st.objects(URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal):
print(
"According to DBPedia, Brisbane has a population of "
"{0:,}".format(int(pop), ",d")
"{0:,}".format(int(pop))
)

# EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
Expand Down
2 changes: 1 addition & 1 deletion examples/swap_primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@

print()
print("Printing bigger example as N3:")
print(primer.serialize(format="n3").decode("utf-8"))
print(primer.serialize(format="n3"))

# for more insight into things already done, lets see the namespaces

Expand Down
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# https://mypy.readthedocs.io/en/stable/config_file.html
[mypy]
python_version = 3.6
warn_unused_configs = True
ignore_missing_imports = True
disallow_subclassing_any = False
31 changes: 18 additions & 13 deletions rdflib/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,34 @@
import re
import codecs
import warnings
import typing as t


# clean ElementTree import
try:
from lxml import etree
except ImportError:
if t.TYPE_CHECKING:
import xml.etree.ElementTree as etree
else:
try:
# Python 2.5
import xml.etree.cElementTree as etree
from lxml import etree
except ImportError:
try:
# Python 2.5
import xml.etree.ElementTree as etree
import xml.etree.cElementTree as etree
except ImportError:
try:
# normal cElementTree install
import cElementTree as etree
# Python 2.5
aucampia marked this conversation as resolved.
Show resolved Hide resolved
import xml.etree.ElementTree as etree
except ImportError:
try:
# normal ElementTree install
import elementtree.ElementTree as etree
# normal cElementTree install
import cElementTree as etree
except ImportError:
raise Exception("Failed to import ElementTree from any known place")
try:
# normal ElementTree install
import elementtree.ElementTree as etree
except ImportError:
raise Exception(
"Failed to import ElementTree from any known place"
)


try:
etree_register_namespace = etree.register_namespace
Expand Down
6 changes: 3 additions & 3 deletions rdflib/extras/infixowl.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ def _get_annotation(self, term=RDFS.label):
for annotation in self.graph.objects(subject=self, predicate=term):
yield annotation

annotation = property(_get_annotation, lambda x: x)
annotation = property(_get_annotation, lambda x: x) # type: ignore[arg-type,misc]

def _get_extentQuery(self):
return (Variable("CLASS"), RDF.type, self.identifier)
Expand Down Expand Up @@ -1496,14 +1496,14 @@ class BooleanClass(OWLRDFListProxy, Class):

@BooleanClassExtentHelper(OWL_NS.intersectionOf)
@Callable
def getIntersections():
def getIntersections(): # type: ignore[misc]
pass

getIntersections = Callable(getIntersections)

@BooleanClassExtentHelper(OWL_NS.unionOf)
@Callable
def getUnions():
def getUnions(): # type: ignore[misc]
pass

getUnions = Callable(getUnions)
Expand Down
94 changes: 85 additions & 9 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional, Union, Type, cast, overload
import logging
from warnings import warn
import random
Expand All @@ -20,7 +20,7 @@
import shutil
import tempfile

from io import BytesIO
from io import BytesIO, BufferedIOBase
from urllib.parse import urlparse

assert Literal # avoid warning
Expand Down Expand Up @@ -956,8 +956,82 @@ def absolutize(self, uri, defrag=1):
"""Turn uri into an absolute URI if it's not one already"""
return self.namespace_manager.absolutize(uri, defrag)

# no destination and non-None positional encoding
@overload
def serialize(
self, destination=None, format="turtle", base=None, encoding=None, **args
self, destination: None, format: str, base: Optional[str], encoding: str, **args
) -> bytes:
...

# no destination and non-None keyword encoding
@overload
def serialize(
self,
*,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
encoding: str,
**args
) -> bytes:
...

# no destination and None positional encoding
@overload
def serialize(
self,
destination: None,
format: str,
base: Optional[str],
encoding: None,
**args
) -> str:
...

# no destination and None keyword encoding
@overload
def serialize(
self,
*,
destination: None = ...,
format: str = ...,
base: Optional[str] = ...,
encoding: None = None,
**args
) -> str:
...

# non-none destination
@overload
def serialize(
self,
destination: Union[str, BufferedIOBase],
format: str = ...,
base: Optional[str] = ...,
encoding: Optional[str] = ...,
**args
) -> None:
...

# fallback
@overload
def serialize(
self,
destination: Union[str, BufferedIOBase, None] = None,
format: str = "turtle",
base: Optional[str] = None,
encoding: Optional[str] = None,
**args
) -> Optional[Union[bytes, str]]:
...

def serialize(
self,
destination: Union[str, BufferedIOBase, None] = None,
format: str = "turtle",
base: Optional[str] = None,
encoding: Optional[str] = None,
**args
) -> Optional[Union[bytes, str]]:
"""Serialize the Graph to destination
Expand All @@ -978,6 +1052,7 @@ def serialize(
base = self.base

serializer = plugin.get(format, Serializer)(self)
stream: BufferedIOBase
if destination is None:
stream = BytesIO()
if encoding is None:
Expand All @@ -987,16 +1062,16 @@ def serialize(
serializer.serialize(stream, base=base, encoding=encoding, **args)
return stream.getvalue()
if hasattr(destination, "write"):
stream = destination
stream = cast(BufferedIOBase, destination)
serializer.serialize(stream, base=base, encoding=encoding, **args)
else:
location = destination
location = cast(str, destination)
scheme, netloc, path, params, _query, fragment = urlparse(location)
if netloc != "":
print(
"WARNING: not saving as location" + "is not a local file reference"
)
return
return None
fd, name = tempfile.mkstemp()
stream = os.fdopen(fd, "wb")
serializer.serialize(stream, base=base, encoding=encoding, **args)
Expand All @@ -1007,6 +1082,7 @@ def serialize(
else:
shutil.copy(name, dest)
os.remove(name)
return None

def print(self, format="turtle", encoding="utf-8", out=None):
print(
Expand Down Expand Up @@ -1146,8 +1222,8 @@ def load(self, source, publicID=None, format="xml"):
def query(
self,
query_object,
processor: str = "sparql",
result: str = "sparql",
processor: Union[str, query.Processor] = "sparql",
result: Union[str, Type[query.Result]] = "sparql",
initNs=None,
initBindings=None,
use_store_provided: bool = True,
Expand Down Expand Up @@ -1183,7 +1259,7 @@ def query(
pass # store has no own implementation

if not isinstance(result, query.Result):
result = plugin.get(result, query.Result)
result = plugin.get(cast(str, result), query.Result)
if not isinstance(processor, query.Processor):
processor = plugin.get(processor, query.Processor)(self)

Expand Down
2 changes: 1 addition & 1 deletion rdflib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


class Parser(object):
__slots__ = set()
__slots__ = ()

def __init__(self):
pass
Expand Down
12 changes: 11 additions & 1 deletion rdflib/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@


from rdflib.term import URIRef, Node
from typing import Union, Callable


# property paths
Expand All @@ -194,6 +195,13 @@


class Path(object):

__or__: Callable[["Path", Union["URIRef", "Path"]], "AlternativePath"]
__invert__: Callable[["Path"], "InvPath"]
__neg__: Callable[["Path"], "NegatedPath"]
__truediv__: Callable[["Path", Union["URIRef", "Path"]], "SequencePath"]
__mul__: Callable[["Path", str], "MulPath"]

def eval(self, graph, subj=None, obj=None):
raise NotImplementedError()

Expand Down Expand Up @@ -502,7 +510,9 @@ def neg_path(p):
# as it would introduce circular imports)

URIRef.__or__ = path_alternative
URIRef.__mul__ = mul_path
# ignore typing here as URIRef inherits from str,
# which has an incompatible definition of __mul__.
URIRef.__mul__ = mul_path # type: ignore
URIRef.__invert__ = inv_path
URIRef.__neg__ = neg_path
URIRef.__truediv__ = path_sequence
Expand Down