Skip to content

Commit 90f6fe5

Browse files
committed
Fix mypy type errors and add mypy to .drone.yml
Fixes #1311 Add `mypy` to .drone.yml and fix type errors that come up. Not type checking examples or tests. Other changes: - fix return value for `Graph.serialize` (refs #1394) - remove default value for `rdflib.plugins.sparql.algebra.translateAlgebra` (refs #1322) - add .dockerignore to reduce context size and make docker quicker to run. - add .flake8 config to ignore line length as black is managing formatting. - add mypy to docker-compose, makefile and tox.ini - fix the way csv2rdf is invoked to ensure that the right code gets executed.
1 parent fd935e2 commit 90f6fe5

File tree

21 files changed

+112
-40
lines changed

21 files changed

+112
-40
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.tox
2+
.venv
3+
.mypy_cache
4+
.git

.drone.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ steps:
2626
- pip install --default-timeout 60 -r requirements.dev.txt
2727
- pip install --default-timeout 60 coveralls && export HAS_COVERALLS=1
2828
- python setup.py install
29-
- black --config black.toml --check ./rdflib | true
29+
- black --config black.toml --check ./rdflib || true
3030
- flake8 --exit-zero rdflib
31+
- mypy --show-error-context --show-error-codes rdflib
3132
- PYTHONWARNINGS=default nosetests --with-timer --timer-top-n 42 --with-coverage --cover-tests --cover-package=rdflib
3233
- coverage report --skip-covered
3334
- coveralls

.flake8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://flake8.pycqa.org/en/latest/user/configuration.html
2+
[flake8]
3+
extend-ignore =
4+
# E501: line too long
5+
# Disabled so that black can control line length.
6+
E501,

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tests:
22
docker-compose -f docker-compose.tests.yml up test-runner
33
docker-compose -f docker-compose.tests.yml down
44

5+
.PHONY: build
56
build:
67
docker-compose -f docker-compose.tests.yml build
78

@@ -14,3 +15,6 @@ reformat:
1415

1516
check-format:
1617
black --config ./black.toml --check .
18+
19+
check-types:
20+
docker-compose -f docker-compose.tests.yml up check-types

docker-compose.tests.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ services:
1313
dockerfile: test/Dockerfile
1414
volumes:
1515
- .:/rdflib
16-
command: ["/rdflib/run_tests_with_coverage_report.sh"]
16+
command: ["/rdflib/run_tests_with_coverage_report.sh"]
17+
18+
check-types:
19+
build:
20+
context: .
21+
dockerfile: test/Dockerfile
22+
volumes:
23+
- .:/rdflib
24+
command: ["python", "-m", "mypy", "--show-error-context", "--show-error-codes" ,"/rdflib/rdflib"]

docs/developers.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Code should be formatted using `black <https://github.com/psf/black>`_.
1515
While not yet mandatory, it will be required in the future (6.0.0+).1
1616
Use Black v21.6b1, with the black.toml config file provided.
1717

18+
Code should also pass `flake8 <https://github.com/psf/black>`_ linting
19+
and `mypy <http://mypy-lang.org/>`_ type checking.
20+
1821
Any new functionality being added to RDFLib should have doc tests and
1922
unit tests. Tests should be added for any functionality being changed
2023
that currently does not have any doc tests or unit tests. And all the
@@ -28,7 +31,7 @@ Running tests
2831
-------------
2932
Run tests with `nose <https://nose.readthedocs.org/en/latest/>`_:
3033

31-
.. code-block: bash
34+
.. code-block:: bash
3235
3336
$ pip install nose
3437
$ python run_tests.py
@@ -42,10 +45,31 @@ Specific tests can either be run by module name or file name. For example::
4245
$ python run_tests.py --tests rdflib.graph
4346
$ python run_tests.py --tests test/test_graph.py
4447

48+
Running static checks
49+
---------------------
50+
51+
Check formatting with `black <https://github.com/psf/black>`_:
52+
53+
.. code-block:: bash
54+
55+
python -m black --config black.toml --check ./rdflib
56+
57+
Check style and conventions with `flake8 <https://github.com/psf/black>`_:
58+
59+
.. code-block:: bash
60+
61+
python -m flake8 rdflib
62+
63+
Check types with `mypy <http://mypy-lang.org/>`_:
64+
65+
.. code-block:: bash
66+
67+
python -m mypy --show-error-context --show-error-codes rdflib
68+
4569
Writing documentation
4670
---------------------
4771

48-
We use sphinx for generating HTML docs, see :ref:`docs`
72+
We use sphinx for generating HTML docs, see :ref:`docs`.
4973

5074
Continuous Integration
5175
----------------------

rdflib/compare.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,15 @@ def _traces(
454454
experimental = self._experimental_path(coloring_copy)
455455
experimental_score = set([c.key() for c in experimental])
456456
if last_coloring:
457-
generator = self._create_generator(
457+
generator = self._create_generator( # type: ignore[unreachable]
458458
[last_coloring, experimental], generator
459459
)
460460
last_coloring = experimental
461-
if best_score is None or best_score < color_score:
461+
if best_score is None or best_score < color_score: # type: ignore[unreachable]
462462
best = [refined_coloring]
463463
best_score = color_score
464464
best_experimental_score = experimental_score
465-
elif best_score > color_score:
465+
elif best_score > color_score: # type: ignore[unreachable]
466466
# prune this branch.
467467
if stats is not None:
468468
stats["prunings"] += 1
@@ -480,7 +480,7 @@ def _traces(
480480
d = [depth[0]]
481481
new_color = self._traces(coloring, stats=stats, depth=d)
482482
color_score = tuple([c.key() for c in refined_coloring])
483-
if best_score is None or color_score > best_score:
483+
if best_score is None or color_score > best_score: # type: ignore[unreachable]
484484
discrete = [new_color]
485485
best_score = color_score
486486
best_depth = d[0]

rdflib/extras/infixowl.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,6 @@ def castToQName(x):
331331
if isinstance(thing, BNode):
332332
return thing.n3()
333333
return "<" + thing + ">"
334-
logger.debug(list(store.objects(subject=thing, predicate=RDF.type)))
335-
raise
336-
return "[]" # +thing._id.encode('utf-8')+'</em>'
337334
label = first(Class(thing, graph=store).label)
338335
if label:
339336
return label

rdflib/graph.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,34 +1057,34 @@ def serialize(
10571057
@overload
10581058
def serialize(
10591059
self,
1060-
destination: Union[str, BufferedIOBase],
1060+
destination: Union[str, BufferedIOBase, pathlib.PurePath],
10611061
format: str = ...,
10621062
base: Optional[str] = ...,
10631063
encoding: Optional[str] = ...,
10641064
**args,
1065-
) -> None:
1065+
) -> "Graph":
10661066
...
10671067

10681068
# fallback
10691069
@overload
10701070
def serialize(
10711071
self,
1072-
destination: Union[str, BufferedIOBase, None] = None,
1072+
destination: Union[str, BufferedIOBase, pathlib.PurePath, None] = None,
10731073
format: str = "turtle",
10741074
base: Optional[str] = None,
10751075
encoding: Optional[str] = None,
10761076
**args,
1077-
) -> Optional[Union[bytes, str]]:
1077+
) -> Union[bytes, str, "Graph"]:
10781078
...
10791079

10801080
def serialize(
10811081
self,
1082-
destination: Union[str, BufferedIOBase, None] = None,
1082+
destination: Union[str, BufferedIOBase, pathlib.PurePath, None] = None,
10831083
format: str = "turtle",
10841084
base: Optional[str] = None,
10851085
encoding: Optional[str] = None,
10861086
**args,
1087-
) -> Optional[Union[bytes, str]]:
1087+
) -> Union[bytes, str, "Graph"]:
10881088
"""Serialize the Graph to destination
10891089
10901090
If destination is None serialize method returns the serialization as
@@ -1123,10 +1123,10 @@ def serialize(
11231123
location = cast(str, destination)
11241124
scheme, netloc, path, params, _query, fragment = urlparse(location)
11251125
if netloc != "":
1126-
print(
1126+
logger.warning(
11271127
"WARNING: not saving as location" + "is not a local file reference"
11281128
)
1129-
return None
1129+
return self
11301130
fd, name = tempfile.mkstemp()
11311131
stream = os.fdopen(fd, "wb")
11321132
serializer.serialize(stream, base=base, encoding=encoding, **args)
@@ -1967,7 +1967,6 @@ def __iter__(self) -> Generator[DatasetQuad, None, None]:
19671967
return self.quads((None, None, None, None))
19681968

19691969

1970-
19711970
class QuotedGraph(Graph):
19721971
"""
19731972
Quoted Graphs are intended to implement Notation 3 formulae. They are

rdflib/plugins/parsers/jsonld.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
import warnings
3737
from rdflib.graph import ConjunctiveGraph
38-
from rdflib.parser import Parser, URLInputSource
38+
from rdflib.parser import URLInputSource
39+
import rdflib.parser
3940
from rdflib.namespace import RDF, XSD
4041
from rdflib.term import URIRef, BNode, Literal
4142

@@ -78,12 +79,12 @@
7879
pass
7980

8081

81-
TYPE_TERM = Term(str(RDF.type), TYPE, VOCAB)
82+
TYPE_TERM = Term(str(RDF.type), TYPE, VOCAB) # type: ignore[call-arg]
8283

8384
ALLOW_LISTS_OF_LISTS = True # NOTE: Not allowed in JSON-LD 1.0
8485

8586

86-
class JsonLDParser(Parser):
87+
class JsonLDParser(rdflib.parser.Parser):
8788
def __init__(self):
8889
super(JsonLDParser, self).__init__()
8990

0 commit comments

Comments
 (0)