diff options
| author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-12-29 08:34:07 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-29 08:34:07 +1000 |
| commit | 8d5b1c8b9d82bceb0a14f76cca6d87bd06382c81 (patch) | |
| tree | b7bd0261d82260478dc518a03446fb8ae254e503 | |
| parent | bc7d74f8ee1ad58ba040eb94272654373c7adf27 (diff) | |
| parent | c3425a1274add61cc5cb9b71fc37c2aca803b424 (diff) | |
| download | rdflib-8d5b1c8b9d82bceb0a14f76cca6d87bd06382c81.tar.gz | |
Merge pull request #1621 from aucampia/iwana-20211228T0141-test_typing
Fix typing errors in tests
| -rw-r--r-- | setup.cfg | 5 | ||||
| -rw-r--r-- | test/earl.py | 7 | ||||
| -rw-r--r-- | test/jsonld/test_context.py | 3 | ||||
| -rw-r--r-- | test/jsonld/test_onedotone.py | 5 | ||||
| -rw-r--r-- | test/jsonld/test_testsuite.py | 3 | ||||
| -rw-r--r-- | test/manifest.py | 31 | ||||
| -rw-r--r-- | test/test_dawg.py | 38 | ||||
| -rw-r--r-- | test/test_issue200.py | 5 | ||||
| -rw-r--r-- | test/test_rdfxml.py | 3 |
9 files changed, 62 insertions, 38 deletions
@@ -32,11 +32,6 @@ ignore_missing_imports = True disallow_subclassing_any = False warn_unreachable = True -[mypy-test.*] -# NOTE: Currently there are many type errors in tests, these should be fixed but -# until they are fixed errors are ignored. -ignore_errors = True - [tool:pytest] addopts = --doctest-modules diff --git a/test/earl.py b/test/earl.py index e211bd29..dc8cfec1 100644 --- a/test/earl.py +++ b/test/earl.py @@ -84,9 +84,12 @@ class EarlReport: self.asserter: Node asserter: Node if asserter_uri is not None or asserter_homepage is not None: - self.asserter = asserter = URIRef( - asserter_homepage if asserter_uri is None else asserter_uri + # cast to remove Optional because mypy is not smart enough to + # figure out that it won't be optional. + asserter_ref = cast( + str, asserter_homepage if asserter_uri is None else asserter_uri ) + self.asserter = asserter = URIRef(asserter_ref) graph.add((asserter, RDF.type, FOAF.Person)) else: self.asserter = asserter = BNode() diff --git a/test/jsonld/test_context.py b/test/jsonld/test_context.py index 2bff4673..df0269ba 100644 --- a/test/jsonld/test_context.py +++ b/test/jsonld/test_context.py @@ -3,6 +3,7 @@ JSON-LD Context Spec """ from functools import wraps +from typing import Any, Dict from rdflib.plugins.shared.jsonld.context import Context, Term from rdflib.plugins.shared.jsonld import context from rdflib.plugins.shared.jsonld import errors @@ -129,7 +130,7 @@ def test_prefix_like_vocab(): # Mock external sources loading -SOURCES = {} +SOURCES: Dict[str, Dict[str, Any]] = {} _source_to_json = context.source_to_json diff --git a/test/jsonld/test_onedotone.py b/test/jsonld/test_onedotone.py index aa50f061..ec7a8027 100644 --- a/test/jsonld/test_onedotone.py +++ b/test/jsonld/test_onedotone.py @@ -1,6 +1,7 @@ from os import environ, chdir, getcwd, path as p import json import os +from typing import Tuple import pytest @@ -15,7 +16,7 @@ TC_BASE = "https://w3c.github.io/json-ld-api/tests/toRdf/" testsuite_dir = p.join(p.abspath(p.dirname(__file__)), "1.1") -unsupported_tests = ("frame", "normalize") +unsupported_tests: Tuple[str, ...] = ("frame", "normalize") unsupported_tests += ( "error", "remote", @@ -24,7 +25,7 @@ unsupported_tests += ("flatten", "compact", "expand") unsupported_tests += ("html",) unsupported_tests += ("fromRdf",) # The JSON-LD 1.1 enhancement applies to parsing only -known_bugs = ( +known_bugs: Tuple[str, ...] = ( # TODO: Literal doesn't preserve representations "fromRdf/0002-in", # RDflib does not print Integer with scientific notation diff --git a/test/jsonld/test_testsuite.py b/test/jsonld/test_testsuite.py index 96cf7850..200db054 100644 --- a/test/jsonld/test_testsuite.py +++ b/test/jsonld/test_testsuite.py @@ -1,5 +1,6 @@ from os import environ, chdir, getcwd, path as p import json +from typing import Tuple import pytest import rdflib @@ -8,7 +9,7 @@ from rdflib.term import URIRef from . import runner -unsupported_tests = ("frame", "normalize") +unsupported_tests: Tuple[str, ...] = ("frame", "normalize") unsupported_tests += ( "error", "remote", diff --git a/test/manifest.py b/test/manifest.py index e09257aa..4ab5582d 100644 --- a/test/manifest.py +++ b/test/manifest.py @@ -1,8 +1,8 @@ -from typing import Iterable, List, NamedTuple, Optional, Tuple +from typing import Iterable, List, NamedTuple, Optional, Tuple, Union, cast from rdflib import RDF, RDFS, Graph, Namespace from rdflib.namespace import DefinedNamespace -from rdflib.term import Node, URIRef +from rdflib.term import Identifier, Node, URIRef MF = Namespace("http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#") QT = Namespace("http://www.w3.org/2001/sw/DataAccess/tests/test-query#") @@ -42,15 +42,18 @@ class RDFT(DefinedNamespace): DAWG = Namespace("http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#") +ResultType = Union[Identifier, Tuple[Identifier, List[Tuple[Identifier, Identifier]]]] +GraphDataType = Union[List[Identifier], List[Tuple[Identifier, Identifier]]] + class RDFTest(NamedTuple): uri: URIRef name: str comment: str - data: Node - graphdata: Optional[List[Node]] - action: Node - result: Optional[Node] + data: Identifier + graphdata: Optional[GraphDataType] + action: Identifier + result: Optional[ResultType] syntax: bool @@ -102,29 +105,33 @@ def read_manifest(f, base=None, legacy=False) -> Iterable[Tuple[Node, URIRef, RD name = g.value(e, MF.name) comment = g.value(e, RDFS.comment) data = None - graphdata = None - res = None + graphdata: Optional[GraphDataType] = None + res: Optional[ResultType] = None syntax = True if _type in (MF.QueryEvaluationTest, MF.CSVResultFormatTest): a = g.value(e, MF.action) query = g.value(a, QT.query) data = g.value(a, QT.data) - graphdata = list(g.objects(a, QT.graphData)) + # NOTE: Casting to identifier because g.objects return Node + # but should probably return identifier instead. + graphdata = list( + cast(Iterable[Identifier], g.objects(a, QT.graphData)) + ) res = g.value(e, MF.result) elif _type in (MF.UpdateEvaluationTest, UP.UpdateEvaluationTest): a = g.value(e, MF.action) query = g.value(a, UP.request) data = g.value(a, UP.data) - graphdata = [] + graphdata = cast(List[Tuple[Identifier, Identifier]], []) for gd in g.objects(a, UP.graphData): graphdata.append( (g.value(gd, UP.graph), g.value(gd, RDFS.label)) ) r = g.value(e, MF.result) - resdata = g.value(r, UP.data) - resgraphdata = [] + resdata: Identifier = g.value(r, UP.data) + resgraphdata: List[Tuple[Identifier, Identifier]] = [] for gd in g.objects(r, UP.graphData): resgraphdata.append( (g.value(gd, UP.graph), g.value(gd, RDFS.label)) diff --git a/test/test_dawg.py b/test/test_dawg.py index a7856bf3..9d4cda77 100644 --- a/test/test_dawg.py +++ b/test/test_dawg.py @@ -9,7 +9,7 @@ from io import TextIOWrapper # http://www.w3.org/2009/sparql/docs/tests/data-sparql11/ # syntax-update-2/manifest#syntax-update-other-01 from test import TEST_DIR -from test.manifest import UP, MF, RDFTest, read_manifest +from test.manifest import UP, MF, RDFTest, ResultType, read_manifest import pytest from test.testutils import file_uri_to_path @@ -23,11 +23,11 @@ from collections import Counter import datetime import isodate import typing -from typing import Dict, Callable +from typing import Dict, Callable, List, Optional, Tuple, cast from rdflib import Dataset, Graph, URIRef, BNode -from rdflib.term import Node +from rdflib.term import Identifier, Node from rdflib.query import Result from rdflib.compare import isomorphic @@ -204,6 +204,10 @@ def update_test(t: RDFTest): rdflib_sparql_module.SPARQL_LOAD_GRAPHS = False uri, name, comment, data, graphdata, query, res, syntax = t + # These casts are here because the RDFTest type is not sufficently + # expressive to capture the two different flavors of tests. + res = cast(Optional[ResultType], res) + graphdata = cast(Optional[List[Tuple[Identifier, Identifier]]], graphdata) query_path: PurePath = file_uri_to_path(query) @@ -226,7 +230,10 @@ def update_test(t: RDFTest): pass # negative syntax test return - resdata, resgraphdata = res + res = cast(ResultType, res) + resdata: Identifier + resgraphdata: List[Tuple[Identifier, Identifier]] + resdata, resgraphdata = res # type: ignore[assignment] # read input graphs if data: @@ -250,19 +257,21 @@ def update_test(t: RDFTest): resg.load(x, publicID=URIRef(l), format=_fmt(x)) eq( - set(x.identifier for x in g.contexts() if x != g.default_context), - set(x.identifier for x in resg.contexts() if x != resg.default_context), + set(ctx.identifier for ctx in g.contexts() if ctx != g.default_context), + set( + ctx.identifier for ctx in resg.contexts() if ctx != resg.default_context + ), "named graphs in datasets do not match", ) assert isomorphic( g.default_context, resg.default_context ), "Default graphs are not isomorphic" - for x in g.contexts(): - if x == g.default_context: + for ctx in g.contexts(): + if ctx == g.default_context: continue - assert isomorphic(x, resg.get_context(x.identifier)), ( - "Graphs with ID %s are not isomorphic" % x.identifier + assert isomorphic(ctx, resg.get_context(ctx.identifier)), ( + "Graphs with ID %s are not isomorphic" % ctx.identifier ) except Exception as e: @@ -337,6 +346,11 @@ def update_test(t: RDFTest): def query_test(t: RDFTest): uri, name, comment, data, graphdata, query, resfile, syntax = t + # These casts are here because the RDFTest type is not sufficently + # expressive to capture the two different flavors of tests. + graphdata = cast(Optional[List[Identifier]], graphdata) + resfile = cast(Optional[Identifier], resfile) + # the query-eval tests refer to graphs to load by resolvable filenames rdflib_sparql_module.SPARQL_LOAD_GRAPHS = True @@ -418,6 +432,7 @@ def query_test(t: RDFTest): if not DETAILEDASSERT: eq(res.type, res2.type, "Types do not match") if res.type == "SELECT": + assert res2.vars is not None eq(set(res.vars), set(res2.vars), "Vars do not match") comp = bindingsCompatible(set(res), set(res2)) assert comp, "Bindings do not match" @@ -434,6 +449,7 @@ def query_test(t: RDFTest): "Types do not match: %r != %r" % (res.type, res2.type), ) if res.type == "SELECT": + assert res2.vars is not None eq( set(res.vars), set(res2.vars), @@ -441,7 +457,7 @@ def query_test(t: RDFTest): ) assert bindingsCompatible( set(res), set(res2) - ), "Bindings do not match: \nexpected:\n%s\n!=\ngot:\n%s" % ( + ), "Bindings do not match: \nexpected:\n%r\n!=\ngot:\n%r" % ( res.serialize(format="txt", namespace_manager=g.namespace_manager), res2.serialize(format="txt", namespace_manager=g.namespace_manager), ) diff --git a/test/test_issue200.py b/test/test_issue200.py index 1af0e839..818fa839 100644 --- a/test/test_issue200.py +++ b/test/test_issue200.py @@ -6,10 +6,7 @@ import unittest import pytest -try: - from os import fork - from os import pipe -except ImportError: +if os.name == "nt": pytestmark = pytest.mark.skip( reason="No os.fork() and/or os.pipe() on this platform, skipping" ) diff --git a/test/test_rdfxml.py b/test/test_rdfxml.py index e695355c..f9f8708c 100644 --- a/test/test_rdfxml.py +++ b/test/test_rdfxml.py @@ -1,5 +1,6 @@ import sys from encodings.utf_8 import StreamWriter +from typing import ClassVar import unittest @@ -171,6 +172,8 @@ class ParserTestCase(unittest.TestCase): path = "store" slow = True + RDF_setting: ClassVar[bool] + @classmethod def setUpClass(cls) -> None: cls.RDF_setting = RDF._fail |
