Skip to content

Commit 10f9ebe

Browse files
authored
fix: typing errors from dmypy (#2451)
Fix various typing errors that are reported when running with `dmypy`, the mypy daemon. Also add a task for running `dmypy` to the Taskfile that can be selected as the default mypy variant by setting the `MYPY_VARIANT` environment variable to `dmypy`.
1 parent f278b86 commit 10f9ebe

File tree

13 files changed

+53
-26
lines changed

13 files changed

+53
-26
lines changed

Taskfile.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ vars:
3535
PIP_COMPILE: pip-compile
3636
DOCKER: docker
3737
OCI_REFERENCE: ghcr.io/rdflib/rdflib
38+
MYPY_VARIANT: '{{ env "MYPY_VARIANT" | default "mypy" }}'
3839
tasks:
3940
install:system-deps:
4041
desc: Install system dependencies
@@ -130,10 +131,17 @@ tasks:
130131
cmds:
131132
- '{{.VENV_PYTHON}} -m isort {{if (mustFromJson (.CHECK | default "false"))}}--check --diff {{end}}{{.CLI_ARGS | default "."}}'
132133
mypy:
134+
desc: Run mypy
135+
cmds:
136+
- task: "mypy:{{ .MYPY_VARIANT }}"
137+
mypy:mypy:
133138
desc: Run mypy
134139
cmds:
135140
- "{{.VENV_PYTHON}} -m mypy --show-error-context --show-error-codes {{.CLI_ARGS}}"
136-
141+
mypy:dmypy:
142+
desc: Run dmypy
143+
cmds:
144+
- "{{.RUN_PREFIX}} dmypy run {{.CLI_ARGS}}"
137145
lint:fix:
138146
desc: Fix auto-fixable linting errors
139147
cmds:

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ def find_version(filename):
302302
("py:class", "ParseFailAction"),
303303
("py:class", "pyparsing.core.TokenConverter"),
304304
("py:class", "pyparsing.results.ParseResults"),
305+
("py:class", "pyparsing.core.ParserElement"),
305306
# These are related to BerkeleyDB
306307
("py:class", "db.DBEnv"),
307308
]

rdflib/events.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
__doc__ = """
24
Dirt Simple Events
35
@@ -23,6 +25,9 @@
2325
<rdflib.events.Event ['data', 'foo', 'used_by']>
2426
"""
2527

28+
29+
from typing import Any, Dict, Optional
30+
2631
__all__ = ["Event", "Dispatcher"]
2732

2833

@@ -53,9 +58,9 @@ class Dispatcher:
5358
subscribers.
5459
"""
5560

56-
_dispatch_map = None
61+
_dispatch_map: Optional[Dict[Any, Any]] = None
5762

58-
def set_map(self, amap):
63+
def set_map(self, amap: Dict[Any, Any]):
5964
self._dispatch_map = amap
6065
return self
6166

rdflib/plugins/parsers/notation3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def _fixslash(s: str) -> str:
276276
N3_Empty = (SYMBOL, List_NS + "Empty")
277277

278278

279-
runNamespaceValue = None
279+
runNamespaceValue: Optional[str] = None
280280

281281

282282
def runNamespace() -> str:

rdflib/plugins/sparql/parserutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Union,
1515
)
1616

17-
from pyparsing import ParseResults, TokenConverter, originalTextFor
17+
from pyparsing import ParserElement, ParseResults, TokenConverter, originalTextFor
1818

1919
from rdflib.term import BNode, Identifier, Variable
2020

@@ -241,7 +241,7 @@ class Comp(TokenConverter):
241241
Returns CompValue / Expr objects - depending on whether evalFn is set.
242242
"""
243243

244-
def __init__(self, name: str, expr):
244+
def __init__(self, name: str, expr: ParserElement):
245245
self.expr = expr
246246
TokenConverter.__init__(self, expr)
247247
self.setName(name)

rdflib/plugins/stores/memory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#
22
#
3+
from __future__ import annotations
4+
35
from typing import (
46
TYPE_CHECKING,
57
Any,
@@ -34,7 +36,7 @@
3436

3537
__all__ = ["SimpleMemory", "Memory"]
3638

37-
ANY = None
39+
ANY: None = None
3840

3941

4042
class SimpleMemory(Store):

rdflib/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
VALID_STORE = 1
6666
CORRUPTED_STORE = 0
6767
NO_STORE = -1
68-
UNKNOWN = None
68+
UNKNOWN: None = None
6969

7070

7171
Pickler = pickle.Pickler

rdflib/tools/csv2rdf.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
try: ``csv2rdf --help``
77
88
"""
9+
from __future__ import annotations
910

1011
import codecs
1112
import configparser
@@ -17,11 +18,12 @@
1718
import sys
1819
import time
1920
import warnings
21+
from typing import Any, Dict, List, Optional, Tuple
2022
from urllib.parse import quote
2123

2224
import rdflib
23-
from rdflib import RDF, RDFS
24-
from rdflib.namespace import split_uri
25+
from rdflib.namespace import RDF, RDFS, split_uri
26+
from rdflib.term import URIRef
2527

2628
__all__ = ["CSV2RDF"]
2729

@@ -88,7 +90,7 @@
8890
"""
8991

9092
# bah - ugly global
91-
uris = {}
93+
uris: Dict[Any, Tuple[URIRef, Optional[URIRef]]] = {}
9294

9395

9496
def toProperty(label):
@@ -113,7 +115,7 @@ def toPropertyLabel(label):
113115
return label
114116

115117

116-
def index(l_, i):
118+
def index(l_: List[int], i: Tuple[int, ...]) -> Tuple[int, ...]:
117119
"""return a set of indexes from a list
118120
>>> index([1,2,3],(0,2))
119121
(1, 3)
@@ -127,7 +129,7 @@ def csv_reader(csv_data, dialect=csv.excel, **kwargs):
127129
yield row
128130

129131

130-
def prefixuri(x, prefix, class_=None):
132+
def prefixuri(x, prefix, class_: Optional[URIRef] = None):
131133
if prefix:
132134
r = rdflib.URIRef(prefix + quote(x.encode("utf8").replace(" ", "_"), safe=""))
133135
else:
@@ -143,7 +145,7 @@ class NodeMaker:
143145
def range(self):
144146
return rdflib.RDFS.Literal
145147

146-
def __call__(self, x):
148+
def __call__(self, x: Any):
147149
return rdflib.Literal(x)
148150

149151

test/jsonld/test_compaction.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# -*- coding: UTF-8 -*-
2+
from __future__ import annotations
23

34
import itertools
45
import json
56
import re
7+
from typing import Any, Dict, List, Tuple
68

79
import pytest
810

@@ -13,11 +15,11 @@
1315
register("json-ld", Serializer, "rdflib.plugins.serializers.jsonld", "JsonLDSerializer")
1416

1517

16-
cases = []
18+
cases: List[Tuple[str, Dict[str, Any]]] = []
1719

1820

19-
def case(*args):
20-
cases.append(args)
21+
def case(source: str, data: Dict[str, Any]):
22+
cases.append((source, data))
2123

2224

2325
case(

test/test_graph/test_graph_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from __future__ import annotations
2+
13
import os
24
import shutil
35
import sys
46
import unittest
57
from tempfile import mkdtemp, mkstemp
8+
from typing import Optional
69

710
import pytest
811

@@ -13,7 +16,7 @@
1316
class ContextTestCase(unittest.TestCase):
1417
store = "default"
1518
slow = True
16-
tmppath = None
19+
tmppath: Optional[str] = None
1720

1821
def setUp(self):
1922
try:

0 commit comments

Comments
 (0)