Skip to content

Cope with Namespace annotations in Python 3.14 #3132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
import logging
import warnings
from collections.abc import Iterable

try:
# Python >= 3.14
from annotationlib import (
get_annotations, # type: ignore[attr-defined,unused-ignore]
)
except ImportError: # pragma: no cover
try:
# Python >= 3.10
from inspect import get_annotations # type: ignore[attr-defined,unused-ignore]
except ImportError:

def get_annotations(thing: Any) -> dict:
return thing.__annotations__


from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -311,15 +327,15 @@ def __contains__(cls, item: str) -> bool:
if item_str.startswith(str(this_ns)):
item_str = item_str[len(str(this_ns)) :]
return any(
item_str in c.__annotations__
item_str in get_annotations(c)
or item_str in c._extras
or (cls._underscore_num and item_str[0] == "_" and item_str[1:].isdigit())
for c in cls.mro()
if issubclass(c, DefinedNamespace)
)

def __dir__(cls) -> Iterable[str]:
attrs = {str(x) for x in cls.__annotations__}
attrs = {str(x) for x in get_annotations(cls)}
# Removing these as they should not be considered part of the namespace.
attrs.difference_update(_DFNS_RESERVED_ATTRS)
values = {cls[str(x)] for x in attrs}
Expand All @@ -328,7 +344,7 @@ def __dir__(cls) -> Iterable[str]:
def as_jsonld_context(self, pfx: str) -> dict: # noqa: N804
"""Returns this DefinedNamespace as a JSON-LD 'context' object"""
terms = {pfx: str(self._NS)}
for key, term in self.__annotations__.items():
for key, term in get_annotations(self).items():
if issubclass(term, URIRef):
terms[key] = f"{pfx}:{key}"

Expand Down
2 changes: 1 addition & 1 deletion test_reports/rdflib_w3c_sparql10-HEAD.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2001/sw/DataAccess/tests/data-r2/solution-seq/manifest#slice-3> .

Expand Down
20 changes: 10 additions & 10 deletions test_reports/rdflib_w3c_sparql11-HEAD.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -691,47 +691,47 @@
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#csv01> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#csv02> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#csv03> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#tsv01> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#tsv02> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#tsv03> .

Expand Down Expand Up @@ -1939,15 +1939,15 @@
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/functions/manifest#plus-1> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/functions/manifest#plus-2> .

Expand Down Expand Up @@ -2251,15 +2251,15 @@
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/json-res/manifest#jsonres01> .

[] a earl:Assertion ;
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
earl:mode earl:automatic ;
earl:result [ a earl:TestResult ;
earl:outcome earl:passed ] ;
earl:outcome earl:failed ] ;
earl:subject <https://github.com/RDFLib/rdflib> ;
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/json-res/manifest#jsonres02> .

Expand Down
Loading