Skip to content

Commit

Permalink
fix: typing errors from dmypy (#2451)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
aucampia committed Jun 20, 2023
1 parent f278b86 commit 10f9ebe
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 26 deletions.
10 changes: 9 additions & 1 deletion Taskfile.yml
Expand Up @@ -35,6 +35,7 @@ vars:
PIP_COMPILE: pip-compile
DOCKER: docker
OCI_REFERENCE: ghcr.io/rdflib/rdflib
MYPY_VARIANT: '{{ env "MYPY_VARIANT" | default "mypy" }}'
tasks:
install:system-deps:
desc: Install system dependencies
Expand Down Expand Up @@ -130,10 +131,17 @@ tasks:
cmds:
- '{{.VENV_PYTHON}} -m isort {{if (mustFromJson (.CHECK | default "false"))}}--check --diff {{end}}{{.CLI_ARGS | default "."}}'
mypy:
desc: Run mypy
cmds:
- task: "mypy:{{ .MYPY_VARIANT }}"
mypy:mypy:
desc: Run mypy
cmds:
- "{{.VENV_PYTHON}} -m mypy --show-error-context --show-error-codes {{.CLI_ARGS}}"

mypy:dmypy:
desc: Run dmypy
cmds:
- "{{.RUN_PREFIX}} dmypy run {{.CLI_ARGS}}"
lint:fix:
desc: Fix auto-fixable linting errors
cmds:
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Expand Up @@ -302,6 +302,7 @@ def find_version(filename):
("py:class", "ParseFailAction"),
("py:class", "pyparsing.core.TokenConverter"),
("py:class", "pyparsing.results.ParseResults"),
("py:class", "pyparsing.core.ParserElement"),
# These are related to BerkeleyDB
("py:class", "db.DBEnv"),
]
Expand Down
9 changes: 7 additions & 2 deletions rdflib/events.py
@@ -1,3 +1,5 @@
from __future__ import annotations

__doc__ = """
Dirt Simple Events
Expand All @@ -23,6 +25,9 @@
<rdflib.events.Event ['data', 'foo', 'used_by']>
"""


from typing import Any, Dict, Optional

__all__ = ["Event", "Dispatcher"]


Expand Down Expand Up @@ -53,9 +58,9 @@ class Dispatcher:
subscribers.
"""

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

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

Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/parsers/notation3.py
Expand Up @@ -276,7 +276,7 @@ def _fixslash(s: str) -> str:
N3_Empty = (SYMBOL, List_NS + "Empty")


runNamespaceValue = None
runNamespaceValue: Optional[str] = None


def runNamespace() -> str:
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/sparql/parserutils.py
Expand Up @@ -14,7 +14,7 @@
Union,
)

from pyparsing import ParseResults, TokenConverter, originalTextFor
from pyparsing import ParserElement, ParseResults, TokenConverter, originalTextFor

from rdflib.term import BNode, Identifier, Variable

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

def __init__(self, name: str, expr):
def __init__(self, name: str, expr: ParserElement):
self.expr = expr
TokenConverter.__init__(self, expr)
self.setName(name)
Expand Down
4 changes: 3 additions & 1 deletion rdflib/plugins/stores/memory.py
@@ -1,5 +1,7 @@
#
#
from __future__ import annotations

from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -34,7 +36,7 @@

__all__ = ["SimpleMemory", "Memory"]

ANY = None
ANY: None = None


class SimpleMemory(Store):
Expand Down
2 changes: 1 addition & 1 deletion rdflib/store.py
Expand Up @@ -65,7 +65,7 @@
VALID_STORE = 1
CORRUPTED_STORE = 0
NO_STORE = -1
UNKNOWN = None
UNKNOWN: None = None


Pickler = pickle.Pickler
Expand Down
14 changes: 8 additions & 6 deletions rdflib/tools/csv2rdf.py
Expand Up @@ -6,6 +6,7 @@
try: ``csv2rdf --help``
"""
from __future__ import annotations

import codecs
import configparser
Expand All @@ -17,11 +18,12 @@
import sys
import time
import warnings
from typing import Any, Dict, List, Optional, Tuple
from urllib.parse import quote

import rdflib
from rdflib import RDF, RDFS
from rdflib.namespace import split_uri
from rdflib.namespace import RDF, RDFS, split_uri
from rdflib.term import URIRef

__all__ = ["CSV2RDF"]

Expand Down Expand Up @@ -88,7 +90,7 @@
"""

# bah - ugly global
uris = {}
uris: Dict[Any, Tuple[URIRef, Optional[URIRef]]] = {}


def toProperty(label):
Expand All @@ -113,7 +115,7 @@ def toPropertyLabel(label):
return label


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


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

def __call__(self, x):
def __call__(self, x: Any):
return rdflib.Literal(x)


Expand Down
8 changes: 5 additions & 3 deletions test/jsonld/test_compaction.py
@@ -1,8 +1,10 @@
# -*- coding: UTF-8 -*-
from __future__ import annotations

import itertools
import json
import re
from typing import Any, Dict, List, Tuple

import pytest

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


cases = []
cases: List[Tuple[str, Dict[str, Any]]] = []


def case(*args):
cases.append(args)
def case(source: str, data: Dict[str, Any]):
cases.append((source, data))


case(
Expand Down
5 changes: 4 additions & 1 deletion test/test_graph/test_graph_context.py
@@ -1,8 +1,11 @@
from __future__ import annotations

import os
import shutil
import sys
import unittest
from tempfile import mkdtemp, mkstemp
from typing import Optional

import pytest

Expand All @@ -13,7 +16,7 @@
class ContextTestCase(unittest.TestCase):
store = "default"
slow = True
tmppath = None
tmppath: Optional[str] = None

def setUp(self):
try:
Expand Down
5 changes: 3 additions & 2 deletions test/test_sparql/test_prefixed_name.py
@@ -1,11 +1,12 @@
from __future__ import annotations

import itertools
import logging
from contextlib import ExitStack
from typing import Type, Union
from typing import Optional, Type, Union

import pyparsing
import pytest
from pyparsing import Optional

import rdflib
from rdflib import Graph
Expand Down
10 changes: 6 additions & 4 deletions test/utils/sparql_checker.py
@@ -1,5 +1,7 @@
"""This runs the nt tests for the W3C RDF Working Group's N-Quads
test suite."""
from __future__ import annotations

import enum
import logging
import pprint
Expand Down Expand Up @@ -290,11 +292,11 @@ def check_syntax(monkeypatch: MonkeyPatch, entry: SPARQLEntry) -> None:
if entry.type_info.negative:
catcher = xstack.enter_context(pytest.raises(Exception))
if entry.type_info.query_type is QueryType.UPDATE:
tree = parseUpdate(query_text)
translateUpdate(tree)
parse_tree = parseUpdate(query_text)
translateUpdate(parse_tree)
elif entry.type_info.query_type is QueryType.QUERY:
tree = parseQuery(query_text)
translateQuery(tree)
query_tree = parseQuery(query_text)
translateQuery(query_tree)
if catcher is not None:
assert catcher.value is not None
logging.info("catcher.value = %s", catcher.value)
Expand Down
5 changes: 3 additions & 2 deletions test/utils/test/test_result.py
@@ -1,9 +1,10 @@
from __future__ import annotations

from contextlib import ExitStack
from test.utils.result import BindingsCollectionType, assert_bindings_collections_equal
from typing import Type, Union
from typing import Optional, Type, Union

import pytest
from pyparsing import Optional

from rdflib.namespace import XSD
from rdflib.term import BNode, Literal, URIRef, Variable
Expand Down

0 comments on commit 10f9ebe

Please sign in to comment.