112
112
>>> print(g.serialize(format='pretty-xml')) # doctest: +SKIP
113
113
114
114
"""
115
+ from __future__ import annotations
115
116
116
117
import itertools
117
118
import logging
119
+ from typing import Iterable , Union
118
120
119
121
from rdflib .collection import Collection
120
- from rdflib .graph import Graph
122
+ from rdflib .graph import Graph , _ObjectType
121
123
from rdflib .namespace import OWL , RDF , RDFS , XSD , Namespace , NamespaceManager
122
124
from rdflib .term import BNode , Identifier , Literal , URIRef , Variable
123
125
from rdflib .util import first
@@ -432,11 +434,11 @@ def replace(self, other):
432
434
self .graph .add ((s , p , classOrIdentifier (other )))
433
435
self .delete ()
434
436
435
- def _get_type (self ):
437
+ def _get_type (self ) -> Iterable [ _ObjectType ] :
436
438
for _t in self .graph .objects (subject = self .identifier , predicate = RDF .type ):
437
439
yield _t
438
440
439
- def _set_type (self , kind ):
441
+ def _set_type (self , kind : Union [ Individual , Identifier , Iterable [ _ObjectType ]] ):
440
442
if not kind :
441
443
return
442
444
if isinstance (kind , (Individual , Identifier )):
@@ -462,10 +464,10 @@ def _delete_type(self):
462
464
463
465
type = property (_get_type , _set_type , _delete_type )
464
466
465
- def _get_identifier (self ):
467
+ def _get_identifier (self ) -> Identifier :
466
468
return self .__identifier
467
469
468
- def _set_identifier (self , i ):
470
+ def _set_identifier (self , i : Identifier ):
469
471
assert i
470
472
if i != self .__identifier :
471
473
oldstatements_out = [
@@ -492,11 +494,13 @@ def _set_identifier(self, i):
492
494
493
495
identifier = property (_get_identifier , _set_identifier )
494
496
495
- def _get_sameAs (self ): # noqa: N802
497
+ def _get_sameAs (self ) -> Iterable [ _ObjectType ] : # noqa: N802
496
498
for _t in self .graph .objects (subject = self .identifier , predicate = OWL .sameAs ):
497
499
yield _t
498
500
499
- def _set_sameAs (self , term ): # noqa: N802
501
+ def _set_sameAs ( # noqa: N802
502
+ self , term : Union [Individual , Identifier , Iterable [_ObjectType ]]
503
+ ):
500
504
# if not kind:
501
505
# return
502
506
if isinstance (term , (Individual , Identifier )):
@@ -1305,7 +1309,8 @@ def isPrimitive(self): # noqa: N802
1305
1309
# sc = list(self.subClassOf)
1306
1310
ec = list (self .equivalentClass )
1307
1311
for _boolclass , p , rdf_list in self .graph .triples_choices (
1308
- (self .identifier , [OWL .intersectionOf , OWL .unionOf ], None )
1312
+ # type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
1313
+ (self .identifier , [OWL .intersectionOf , OWL .unionOf ], None ) # type: ignore[arg-type]
1309
1314
):
1310
1315
ec .append (manchesterSyntax (rdf_list , self .graph , boolean = p ))
1311
1316
for _e in ec :
@@ -1331,7 +1336,8 @@ def __repr__(self, full=False, normalization=True):
1331
1336
sc = list (self .subClassOf )
1332
1337
ec = list (self .equivalentClass )
1333
1338
for _boolclass , p , rdf_list in self .graph .triples_choices (
1334
- (self .identifier , [OWL .intersectionOf , OWL .unionOf ], None )
1339
+ # type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
1340
+ (self .identifier , [OWL .intersectionOf , OWL .unionOf ], None ) # type: ignore[arg-type]
1335
1341
):
1336
1342
ec .append (manchesterSyntax (rdf_list , self .graph , boolean = p ))
1337
1343
dc = list (self .disjointWith )
@@ -1340,7 +1346,9 @@ def __repr__(self, full=False, normalization=True):
1340
1346
dc .append (c )
1341
1347
klasskind = ""
1342
1348
label = list (self .graph .objects (self .identifier , RDFS .label ))
1343
- label = label and "(" + label [0 ] + ")" or ""
1349
+ # type error: Incompatible types in assignment (expression has type "str", variable has type "List[Node]")
1350
+ # type error: Unsupported operand types for + ("str" and "Node")
1351
+ label = label and "(" + label [0 ] + ")" or "" # type: ignore[assignment, operator]
1344
1352
if sc :
1345
1353
if full :
1346
1354
scjoin = "\n "
@@ -1421,7 +1429,9 @@ def __init__(self, rdf_list, members=None, graph=None):
1421
1429
self ._rdfList = Collection (
1422
1430
self .graph , BNode (), [classOrIdentifier (m ) for m in members ]
1423
1431
)
1424
- self .graph .add ((self .identifier , self ._operator , self ._rdfList .uri ))
1432
+ # type error: "OWLRDFListProxy" has no attribute "identifier"
1433
+ # type error: "OWLRDFListProxy" has no attribute "_operator"
1434
+ self .graph .add ((self .identifier , self ._operator , self ._rdfList .uri )) # type: ignore[attr-defined]
1425
1435
1426
1436
def __eq__ (self , other ):
1427
1437
"""
@@ -1439,7 +1449,8 @@ def __eq__(self, other):
1439
1449
return False
1440
1450
return True
1441
1451
else :
1442
- return self .identifier == other .identifier
1452
+ # type error: "OWLRDFListProxy" has no attribute "identifier"
1453
+ return self .identifier == other .identifier # type: ignore[attr-defined]
1443
1454
1444
1455
# Redirect python list accessors to the underlying Collection instance
1445
1456
def __len__ (self ):
@@ -1777,8 +1788,10 @@ def __init__(
1777
1788
elif isinstance (restriction_range , Class ):
1778
1789
self .restrictionRange = classOrIdentifier (restriction_range )
1779
1790
else :
1780
- self .restrictionRange = first (
1781
- self .graph .objects (self .identifier , restriction_type )
1791
+ # error: Incompatible types in assignment (expression has type "Optional[Identifier]", variable has type "Identifier")
1792
+ self .restrictionRange = first ( # type: ignore[assignment]
1793
+ # type error: Argument 1 to "first" has incompatible type "Generator[Node, None, None]"; expected "Iterable[Identifier]"
1794
+ self .graph .objects (self .identifier , restriction_type ) # type: ignore[arg-type]
1782
1795
)
1783
1796
if (
1784
1797
self .identifier ,
@@ -1834,7 +1847,8 @@ def __eq__(self, other):
1834
1847
if isinstance (other , Restriction ):
1835
1848
return (
1836
1849
other .onProperty == self .onProperty
1837
- and other .restriction_range == self .restrictionRange
1850
+ # type error: "Restriction" has no attribute "restriction_range"; maybe "restrictionRange"?
1851
+ and other .restriction_range == self .restrictionRange # type: ignore[attr-defined]
1838
1852
)
1839
1853
else :
1840
1854
return False
@@ -1999,9 +2013,11 @@ def _del_mincardinality(self):
1999
2013
2000
2014
def restrictionKind (self ): # noqa: N802
2001
2015
for s , p , o in self .graph .triples_choices (
2002
- (self .identifier , self .restrictionKinds , None )
2016
+ # type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, List[URIRef], None]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
2017
+ (self .identifier , self .restrictionKinds , None ) # type: ignore[arg-type]
2003
2018
):
2004
- return p .split (str (OWL ))[- 1 ]
2019
+ # type error: "Node" has no attribute "split"
2020
+ return p .split (str (OWL ))[- 1 ] # type: ignore[attr-defined]
2005
2021
return None
2006
2022
2007
2023
def __repr__ (self ):
@@ -2155,9 +2171,11 @@ def __repr__(self):
2155
2171
% (self .qname , first (self .comment ) and first (self .comment ) or "" )
2156
2172
)
2157
2173
if first (self .inverseOf ):
2158
- two_link_inverse = first (first (self .inverseOf ).inverseOf )
2174
+ # type error: Item "None" of "Optional[Any]" has no attribute "inverseOf"
2175
+ two_link_inverse = first (first (self .inverseOf ).inverseOf ) # type: ignore[union-attr]
2159
2176
if two_link_inverse and two_link_inverse .identifier == self .identifier :
2160
- inverserepr = first (self .inverseOf ).qname
2177
+ # type error: Item "None" of "Optional[Any]" has no attribute "qname"
2178
+ inverserepr = first (self .inverseOf ).qname # type: ignore[union-attr]
2161
2179
else :
2162
2180
inverserepr = repr (first (self .inverseOf ))
2163
2181
rt .append (
@@ -2168,7 +2186,8 @@ def __repr__(self):
2168
2186
)
2169
2187
)
2170
2188
for _s , _p , roletype in self .graph .triples_choices (
2171
- (
2189
+ # type error: Argument 1 to "triples_choices" of "Graph" has incompatible type "Tuple[Any, URIRef, List[URIRef]]"; expected "Union[Tuple[List[Node], Node, Node], Tuple[Node, List[Node], Node], Tuple[Node, Node, List[Node]]]"
2190
+ ( # type: ignore[arg-type]
2172
2191
self .identifier ,
2173
2192
RDF .type ,
2174
2193
[
@@ -2178,7 +2197,8 @@ def __repr__(self):
2178
2197
],
2179
2198
)
2180
2199
):
2181
- rt .append (str (roletype .split (str (OWL ))[- 1 ]))
2200
+ # type error: "Node" has no attribute "split"
2201
+ rt .append (str (roletype .split (str (OWL ))[- 1 ])) # type: ignore[attr-defined]
2182
2202
else :
2183
2203
rt .append (
2184
2204
"DatatypeProperty( %s %s"
@@ -2228,7 +2248,8 @@ def canonicalName(term, g): # noqa: N802
2228
2248
]
2229
2249
)
2230
2250
)
2231
- rt = "\n " .join ([expr for expr in rt if expr ])
2251
+ # type error: Incompatible types in assignment (expression has type "str", variable has type "List[str]")
2252
+ rt = "\n " .join ([expr for expr in rt if expr ]) # type: ignore[assignment]
2232
2253
rt += "\n )"
2233
2254
return rt
2234
2255
0 commit comments