Skip to content

Commit

Permalink
Fix sphinx autodocs complaining while probing attributes on DefinedNa…
Browse files Browse the repository at this point in the history
…mespace classes.

Add correct annotation details for items that sphinx cannot find.
  • Loading branch information
ashleysommer committed Jul 25, 2024
1 parent 7f97222 commit 46006cf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
18 changes: 10 additions & 8 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,9 +1398,9 @@ def parse(
:doc:`Security Considerations </security_considerations>`
documentation.
:param source: An `InputSource`, file-like object, `Path` like object,
or string. In the case of a string the string is the location of the
source.
:param source: An `xml.sax.xmlreader.InputSource`, file-like object,
`pathlib.Path` like object, or string. In the case of a string the string
is the location of the source.
:param location: A string indicating the relative or absolute URL of the
source. `Graph`'s absolutize method is used if a relative location
is specified.
Expand Down Expand Up @@ -2235,7 +2235,8 @@ def parse(
See :meth:`rdflib.graph.Graph.parse` for documentation on arguments.
If the source is in a format that does not support named graphs its triples
will be added to the default graph (i.e. `ConjunctiveGraph.default_context`).
will be added to the default graph
(i.e. :attr:`ConjunctiveGraph.default_context`).
:Returns:
Expand All @@ -2261,7 +2262,7 @@ def parse(
the ``publicID`` parameter will also not be used as the name for the
graph that the data is loaded into, and instead the triples from sources
that do not support named graphs will be loaded into the default graph
(i.e. `ConjunctiveGraph.default_context`).
(i.e. :attr:`ConjunctiveGraph.default_context`).
"""

source = create_input_source(
Expand Down Expand Up @@ -2495,8 +2496,9 @@ def parse(
The source is specified using one of source, location, file or data.
If the source is in a format that does not support named graphs it's triples
will be added to the default graph (i.e. `Dataset.default_context`).
If the source is in a format that does not support named graphs its triples
will be added to the default graph
(i.e. :attr:`.Dataset.default_context`).
.. caution::
Expand All @@ -2517,7 +2519,7 @@ def parse(
the ``publicID`` parameter will also not be used as the name for the
graph that the data is loaded into, and instead the triples from sources
that do not support named graphs will be loaded into the default graph
(i.e. `Dataset.default_context`).
(i.e. :attr:`.Dataset.default_context`).
"""

c = ConjunctiveGraph.parse(
Expand Down
2 changes: 1 addition & 1 deletion rdflib/namespace/_WGS.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class WGS(DefinedNamespace):
Basic Geo (WGS84 lat/long) Vocabulary
The HTML Specification for the vocabulary can be found
`here <https://www.w3.org/2003/01/geo/>`.
here <https://www.w3.org/2003/01/geo/>.
"""

_NS = Namespace("https://www.w3.org/2003/01/geo/wgs84_pos#")
Expand Down
12 changes: 12 additions & 0 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ def __repr__(self) -> str:
"_underscore_num",
}

# Some libraries probe classes for certain attributes or items.
# This is a list of those attributes and items that should be ignored.
_IGNORED_ATTR_LOOKUP: Set[str] = {
"_pytestfixturefunction", # pytest tries to look this up on Defined namespaces
"_partialmethod", # sphinx tries to look this up during autodoc generation
}


class DefinedNamespaceMeta(type):
"""Utility metaclass for generating URIRefs with a common prefix."""
Expand All @@ -246,10 +253,13 @@ class DefinedNamespaceMeta(type):
@lru_cache(maxsize=None)
def __getitem__(cls, name: str, default=None) -> URIRef:
name = str(name)

if name in _DFNS_RESERVED_ATTRS:
raise AttributeError(
f"DefinedNamespace like object has no attribute {name!r}"
)
elif name in _IGNORED_ATTR_LOOKUP:
raise KeyError()
if str(name).startswith("__"):
# NOTE on type ignore: This seems to be a real bug, super() does not
# implement this method, it will fail if it is ever reached.
Expand All @@ -265,6 +275,8 @@ def __getitem__(cls, name: str, default=None) -> URIRef:
return cls._NS[name]

def __getattr__(cls, name: str):
if name in _IGNORED_ATTR_LOOKUP:
raise AttributeError()
return cls.__getitem__(name)

def __repr__(cls) -> str:
Expand Down

0 comments on commit 46006cf

Please sign in to comment.