Skip to content

Commit

Permalink
fix: correct imports and __all__ (#2340)
Browse files Browse the repository at this point in the history
Disable
[`implicit_reexport`](https://mypy.readthedocs.io/en/stable/config_file.html#confval-implicit_reexport)
and eliminate all errors reported by mypy after this.

This helps ensure that import statements import from the right module and that
the `__all__` variable is correct.
  • Loading branch information
aucampia authored Apr 12, 2023
1 parent 81d13d4 commit 7df77cd
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 72 deletions.
50 changes: 50 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import os
import re
import sys
from typing import Any, Dict

import sphinx
import sphinx.application

import rdflib

Expand Down Expand Up @@ -47,6 +49,7 @@
"sphinx.ext.autosectionlabel",
]

# https://github.com/sphinx-contrib/apidoc/blob/master/README.rst#configuration
apidoc_module_dir = "../rdflib"
apidoc_output_dir = "apidocs"

Expand Down Expand Up @@ -328,3 +331,50 @@ def find_version(filename):

if sys.version_info < (3, 8):
nitpick_ignore.extend([("py:class", "importlib_metadata.EntryPoint")])


def autodoc_skip_member_handler(
app: sphinx.application.Sphinx,
what: str,
name: str,
obj: Any,
skip: bool,
options: Dict[str, Any],
):
"""
This function will be called by Sphinx when it is deciding whether to skip a
member of a class or module.
"""
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#event-autodoc-skip-member
if (
app.env.docname == "apidocs/rdflib"
and what == "module"
and type(obj).__name__.endswith("DefinedNamespaceMeta")
):
# Don't document namespaces in the `rdflib` module, they will be
# documented in the `rdflib.namespace` module instead and Sphinx does
# not like when these are documented in two places.
#
# An example of the WARNINGS that occur without this is:
#
# "WARNING: duplicate object description of rdflib.namespace._SDO.SDO,
# other instance in apidocs/rdflib, use :noindex: for one of them"
logging.info(
"Skipping %s %s in %s, it will be documented in ",
what,
name,
app.env.docname,
)
return True
return None


# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#skipping-members
def setup(app: sphinx.application.Sphinx) -> None:
"""
Setup the Sphinx application.
"""

# Register a autodoc-skip-member handler so that certain members can be
# skipped.
app.connect("autodoc-skip-member", autodoc_skip_member_handler)
2 changes: 1 addition & 1 deletion docs/rdf_terms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Common XSD datatypes
^^^^^^^^^^^^^^^^^^^^
Most simple literals such as *string* or *integer* have XML Schema (XSD) datatypes defined for them, see the figure
below. Additionally, these XSD datatypes are listed in the :class:`XSD Namespace class <rdflib.XSD>` that
below. Additionally, these XSD datatypes are listed in the :class:`XSD Namespace class <rdflib.namespace.XSD>` that
ships with RDFLib, so many Python code editors will prompt you with autocomplete for them when using it.
Remember, you don't *have* to use XSD datatypes and can always make up your own, as GeoSPARQL does, as described above.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ disallow_subclassing_any = false
warn_unreachable = true
warn_unused_ignores = true
no_implicit_optional = false
implicit_reexport = false

[[tool.mypy.overrides]]
module = "pyparsing.*"
Expand Down
26 changes: 14 additions & 12 deletions rdflib/namespace/_GEO.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ class GEO(DefinedNamespace):
Generated from: http://schemas.opengis.net/geosparql/1.0/geosparql_vocab_all.rdf
Date: 2021-12-27 17:38:15.101187
<http://www.opengis.net/ont/geosparql> dc:creator "Open Geospatial Consortium"^^xsd:string
dc:date "2012-04-30"^^xsd:date
dc:source <http://www.opengis.net/doc/IS/geosparql/1.0>
"OGC GeoSPARQL – A Geographic Query Language for RDF Data OGC 11-052r5"^^xsd:string
rdfs:seeAlso <http://www.opengis.net/def/function/ogc-geosparql/1.0>
<http://www.opengis.net/def/rule/ogc-geosparql/1.0>
<http://www.opengis.net/doc/IS/geosparql/1.0>
owl:imports dc:
<http://www.opengis.net/ont/gml>
<http://www.opengis.net/ont/sf>
<http://www.w3.org/2004/02/skos/core>
owl:versionInfo "OGC GeoSPARQL 1.0"^^xsd:string
.. code-block:: Turtle
<http://www.opengis.net/ont/geosparql> dc:creator "Open Geospatial Consortium"^^xsd:string
dc:date "2012-04-30"^^xsd:date
dc:source <http://www.opengis.net/doc/IS/geosparql/1.0>
"OGC GeoSPARQL – A Geographic Query Language for RDF Data OGC 11-052r5"^^xsd:string
rdfs:seeAlso <http://www.opengis.net/def/function/ogc-geosparql/1.0>
<http://www.opengis.net/def/rule/ogc-geosparql/1.0>
<http://www.opengis.net/doc/IS/geosparql/1.0>
owl:imports dc:
<http://www.opengis.net/ont/gml>
<http://www.opengis.net/ont/sf>
<http://www.w3.org/2004/02/skos/core>
owl:versionInfo "OGC GeoSPARQL 1.0"^^xsd:string
"""

# http://www.w3.org/2000/01/rdf-schema#Datatype
Expand Down
28 changes: 28 additions & 0 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@
"ClosedNamespace",
"DefinedNamespace",
"NamespaceManager",
"BRICK",
"CSVW",
"DC",
"DCAM",
"DCAT",
"DCMITYPE",
"DCTERMS",
"DOAP",
"FOAF",
"GEO",
"ODRL2",
"ORG",
"OWL",
"PROF",
"PROV",
"QB",
"RDF",
"RDFS",
"SDO",
"SH",
"SKOS",
"SOSA",
"SSN",
"TIME",
"VANN",
"VOID",
"WGS",
"XSD",
]

logger = logging.getLogger(__name__)
Expand Down
3 changes: 3 additions & 0 deletions rdflib/plugins/stores/sparqlconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,6 @@ def update(
self.update_endpoint + qsa, data=query.encode(), headers=args["headers"]
)
)


__all__ = ["SPARQLConnector", "SPARQLConnectorException"]
3 changes: 3 additions & 0 deletions rdflib/plugins/stores/sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,3 +1011,6 @@ def predicate_objects(
"""A generator of (predicate, object) tuples for the given subject"""
for t, c in self.triples((subject, None, None)):
yield t[1], t[2]


__all__ = ["SPARQLUpdateStore", "SPARQLStore"]
6 changes: 3 additions & 3 deletions test/jsonld/runner.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# -*- coding: UTF-8 -*-
import json

from rdflib import ConjunctiveGraph
from rdflib import BNode, ConjunctiveGraph
from rdflib.compare import isomorphic
from rdflib.parser import InputSource
from rdflib.plugins.parsers.jsonld import JsonLDParser, to_rdf

# monkey-patch N-Quads parser via it's underlying W3CNTriplesParser to keep source bnode id:s ..
from rdflib.plugins.parsers.ntriples import W3CNTriplesParser, bNode, r_nodeid
from rdflib.plugins.parsers.ntriples import W3CNTriplesParser, r_nodeid
from rdflib.plugins.serializers.jsonld import from_rdf
from rdflib.plugins.shared.jsonld.keys import CONTEXT, GRAPH


def _preserving_nodeid(self, bnode_context=None):
if not self.peek("_"):
return False
return bNode(self.eat(r_nodeid).group(1))
return BNode(self.eat(r_nodeid).group(1))


DEFAULT_PARSER_VERSION = 1.0
Expand Down
4 changes: 3 additions & 1 deletion test/jsonld/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: UTF-8 -*-
from rdflib.plugin import Parser, Serializer, register
from rdflib.parser import Parser
from rdflib.plugin import register
from rdflib.serializer import Serializer

register("json-ld", Parser, "rdflib.plugins.parsers.jsonld", "JsonLDParser")
register("json-ld", Serializer, "rdflib.plugins.serializers.jsonld", "JsonLDSerializer")
Expand Down
3 changes: 2 additions & 1 deletion test/jsonld/test_compaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import pytest

from rdflib import Graph
from rdflib.plugin import Serializer, register
from rdflib.plugin import register
from rdflib.serializer import Serializer

register("json-ld", Serializer, "rdflib.plugins.serializers.jsonld", "JsonLDSerializer")

Expand Down
3 changes: 2 additions & 1 deletion test/jsonld/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def test_prefix_like_vocab():

# Mock external sources loading
SOURCES: Dict[str, Dict[str, Any]] = {}
_source_to_json = context.source_to_json
# type error: Module "rdflib.plugins.shared.jsonld.context" does not explicitly export attribute "source_to_json"
_source_to_json = context.source_to_json # type: ignore[attr-defined]


def _mock_source_loader(f):
Expand Down
3 changes: 2 additions & 1 deletion test/jsonld/test_named_graphs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*-
from rdflib import *
from rdflib.plugin import Parser, register
from rdflib.parser import Parser
from rdflib.plugin import register

register("json-ld", Parser, "rdflib.plugins.parsers.jsonld", "JsonLDParser")
register("application/ld+json", Parser, "rdflib.plugins.parsers.jsonld", "JsonLDParser")
Expand Down
6 changes: 4 additions & 2 deletions test/test_dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import pytest

from rdflib import URIRef, plugin
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Dataset, Graph, Namespace
from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Dataset, Graph
from rdflib.namespace import Namespace
from rdflib.store import Store

# Will also run SPARQLUpdateStore tests against local SPARQL1.1 endpoint if
# available. This assumes SPARQL1.1 query/update endpoints running locally at
Expand All @@ -26,7 +28,7 @@

pluginstores = []

for s in plugin.plugins(None, plugin.Store):
for s in plugin.plugins(None, Store):
if s.name in ("Memory", "Auditable", "Concurrent", "SPARQLStore"):
continue # these are tested by default

Expand Down
4 changes: 2 additions & 2 deletions test/test_graph/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

from rdflib import Graph, URIRef, plugin
from rdflib import Graph, URIRef
from rdflib.exceptions import ParserError
from rdflib.namespace import Namespace, NamespaceManager
from rdflib.plugin import PluginException
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_property_namespace_manager() -> None:


def get_store_names() -> Set[Optional[str]]:
names: Set[Optional[str]] = {*get_unique_plugin_names(plugin.Store)}
names: Set[Optional[str]] = {*get_unique_plugin_names(Store)}
names.difference_update(
{
"default",
Expand Down
3 changes: 2 additions & 1 deletion test/test_graph/test_graph_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from rdflib import BNode, ConjunctiveGraph, Graph, URIRef, plugin
from rdflib.store import Store


class ContextTestCase(unittest.TestCase):
Expand Down Expand Up @@ -367,7 +368,7 @@ def testTriples(self):
pluginname = sys.argv[1]

tests = 0
for s in plugin.plugins(pluginname, plugin.Store):
for s in plugin.plugins(pluginname, Store):
if s.name in (
"default",
"Memory",
Expand Down
8 changes: 3 additions & 5 deletions test/test_graph/test_graph_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
from test.utils.graph import cached_graph
from test.utils.http import (
MOCK_HTTP_REQUEST_WILDCARD,
MockHTTPRequest,
ctx_http_handler,
)
from test.utils.httpservermock import (
MethodName,
MockHTTPRequest,
MockHTTPResponse,
ServedBaseHTTPServerMock,
ctx_http_handler,
)
from test.utils.httpservermock import ServedBaseHTTPServerMock
from test.utils.wildcard import URL_PARSE_RESULT_WILDCARD
from urllib.error import HTTPError

Expand Down
4 changes: 2 additions & 2 deletions test/test_namespace/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from rdflib import DCTERMS
from rdflib.graph import BNode, Graph, Literal
from rdflib.graph import Graph
from rdflib.namespace import (
FOAF,
OWL,
Expand All @@ -17,7 +17,7 @@
Namespace,
URIPattern,
)
from rdflib.term import URIRef
from rdflib.term import BNode, Literal, URIRef


class TestNamespace:
Expand Down
9 changes: 4 additions & 5 deletions test/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import rdflib.compare
from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.namespace import XSD
from rdflib.parser import create_input_source
from rdflib.parser import Parser, create_input_source
from rdflib.plugins.parsers.notation3 import BadSyntax
from rdflib.serializer import Serializer
from rdflib.util import guess_format

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -302,10 +303,8 @@ def roundtrip(
def get_formats() -> Set[str]:
global _formats
if not _formats:
serializers = set(
x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Serializer)
)
parsers = set(x.name for x in rdflib.plugin.plugins(None, rdflib.plugin.Parser))
serializers = set(x.name for x in rdflib.plugin.plugins(None, Serializer))
parsers = set(x.name for x in rdflib.plugin.plugins(None, Parser))
_formats = {
format for format in parsers.intersection(serializers) if "/" not in format
}
Expand Down
7 changes: 2 additions & 5 deletions test/test_sparql/test_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import json
from contextlib import ExitStack
from test.utils import helper
from test.utils.httpservermock import (
MethodName,
MockHTTPResponse,
ServedBaseHTTPServerMock,
)
from test.utils.http import MethodName, MockHTTPResponse
from test.utils.httpservermock import ServedBaseHTTPServerMock
from typing import (
Dict,
FrozenSet,
Expand Down
9 changes: 3 additions & 6 deletions test/test_store/test_store_sparqlstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
from test.utils import helper
from test.utils.httpservermock import (
MethodName,
MockHTTPResponse,
ServedBaseHTTPServerMock,
)
from test.utils.http import MethodName, MockHTTPResponse
from test.utils.httpservermock import ServedBaseHTTPServerMock
from threading import Thread
from typing import Callable, ClassVar, Type
from unittest.mock import patch
Expand All @@ -16,7 +13,7 @@

from rdflib import Graph, Literal, URIRef
from rdflib.namespace import FOAF, RDF, RDFS, XMLNS, XSD
from rdflib.plugins.stores.sparqlstore import SPARQLConnector
from rdflib.plugins.stores.sparqlconnector import SPARQLConnector


class TestSPARQLStoreGraph:
Expand Down
7 changes: 2 additions & 5 deletions test/test_store/test_store_sparqlstore_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import itertools
import logging
from test.utils import GraphHelper
from test.utils.httpservermock import (
MethodName,
MockHTTPResponse,
ServedBaseHTTPServerMock,
)
from test.utils.http import MethodName, MockHTTPResponse
from test.utils.httpservermock import ServedBaseHTTPServerMock
from typing import Dict, Iterable, List, Optional, Set, Tuple

import pytest
Expand Down
Loading

0 comments on commit 7df77cd

Please sign in to comment.