Skip to content

Commit a683d19

Browse files
Cope with Namespace annotations in Python 3.14 (#3132)
The __annotations__ member can be incomplete, use the get_annotations() helper from annotationlib (Python >= 3.14) or inspect (Python >= 3.10) if available. Related: #3083 Signed-off-by: Nils Philippsen <[email protected]> Co-authored-by: Nicholas Car <[email protected]>
1 parent 87ed8d1 commit a683d19

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

rdflib/namespace/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@
7373
import logging
7474
import warnings
7575
from collections.abc import Iterable
76+
77+
try:
78+
# Python >= 3.14
79+
from annotationlib import (
80+
get_annotations, # type: ignore[attr-defined,unused-ignore]
81+
)
82+
except ImportError: # pragma: no cover
83+
try:
84+
# Python >= 3.10
85+
from inspect import get_annotations # type: ignore[attr-defined,unused-ignore]
86+
except ImportError:
87+
88+
def get_annotations(thing: Any) -> dict:
89+
return thing.__annotations__
90+
91+
7692
from functools import lru_cache
7793
from pathlib import Path
7894
from typing import TYPE_CHECKING, Any
@@ -316,15 +332,15 @@ def __contains__(cls, item: str) -> bool:
316332
if item_str.startswith(str(this_ns)):
317333
item_str = item_str[len(str(this_ns)) :]
318334
return any(
319-
item_str in c.__annotations__
335+
item_str in get_annotations(c)
320336
or item_str in c._extras
321337
or (cls._underscore_num and item_str[0] == "_" and item_str[1:].isdigit())
322338
for c in cls.mro()
323339
if issubclass(c, DefinedNamespace)
324340
)
325341

326342
def __dir__(cls) -> Iterable[str]:
327-
attrs = {str(x) for x in cls.__annotations__}
343+
attrs = {str(x) for x in get_annotations(cls)}
328344
# Removing these as they should not be considered part of the namespace.
329345
attrs.difference_update(_DFNS_RESERVED_ATTRS)
330346
values = {cls[str(x)] for x in attrs}
@@ -333,7 +349,7 @@ def __dir__(cls) -> Iterable[str]:
333349
def as_jsonld_context(self, pfx: str) -> dict: # noqa: N804
334350
"""Returns this DefinedNamespace as a JSON-LD 'context' object"""
335351
terms = {pfx: str(self._NS)}
336-
for key, term in self.__annotations__.items():
352+
for key, term in get_annotations(self).items():
337353
if issubclass(term, URIRef):
338354
terms[key] = f"{pfx}:{key}"
339355

test_reports/rdflib_w3c_sparql10-HEAD.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@
16031603
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
16041604
earl:mode earl:automatic ;
16051605
earl:result [ a earl:TestResult ;
1606-
earl:outcome earl:passed ] ;
1606+
earl:outcome earl:failed ] ;
16071607
earl:subject <https://github.com/RDFLib/rdflib> ;
16081608
earl:test <http://www.w3.org/2001/sw/DataAccess/tests/data-r2/solution-seq/manifest#slice-3> .
16091609

test_reports/rdflib_w3c_sparql11-HEAD.ttl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -691,47 +691,47 @@
691691
earl:assertedBy <https://github.com/RDFLib/rdflib> ;
692692
earl:mode earl:automatic ;
693693
earl:result [ a earl:TestResult ;
694-
earl:outcome earl:passed ] ;
694+
earl:outcome earl:failed ] ;
695695
earl:subject <https://github.com/RDFLib/rdflib> ;
696696
earl:test <http://www.w3.org/2009/sparql/docs/tests/data-sparql11/csv-tsv-res/manifest#csv01> .
697697

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)