paths.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. r"""
  2. This module implements the SPARQL 1.1 Property path operators, as
  3. defined in:
  4. [http://www.w3.org/TR/sparql11-query/#propertypaths](http://www.w3.org/TR/sparql11-query/#propertypaths)
  5. In SPARQL the syntax is as follows:
  6. | Syntax | Matches |
  7. |---------------------|-------------------------------------------------------------------------|
  8. | `iri` | An IRI. A path of length one. |
  9. | `^elt` | Inverse path (object to subject). |
  10. | `elt1 / elt2` | A sequence path of `elt1` followed by `elt2`. |
  11. | `elt1 \| elt2` | An alternative path of `elt1` or `elt2` (all possibilities are tried). |
  12. | `elt*` | A path that connects subject and object by zero or more matches of `elt`.|
  13. | `elt+` | A path that connects subject and object by one or more matches of `elt`.|
  14. | `elt?` | A path that connects subject and object by zero or one matches of `elt`.|
  15. | `!iri` or <br> `!(iri1 \| ... \| irin)` | Negated property set. An IRI not among `iri1` to `irin`. <br> `!iri` is short for `!(iri)`. |
  16. | `!^iri` or <br> `!(^iri1 \| ... \| ^irin)` | Negated reverse property set. Excludes `^iri1` to `^irin` as reverse paths. <br> `!^iri` is short for `!(^iri)`. |
  17. | `!(iri1 \| ... \| irij \| ^irij+1 \| ... \| ^irin)` | A combination of forward and reverse properties in a negated property set. |
  18. | `(elt)` | A grouped path `elt`, where parentheses control precedence. |
  19. This module is used internally by the SPARQL engine, but the property paths
  20. can also be used to query RDFLib Graphs directly.
  21. Where possible the SPARQL syntax is mapped to Python operators, and property
  22. path objects can be constructed from existing URIRefs.
  23. ```python
  24. >>> from rdflib import Graph, Namespace
  25. >>> from rdflib.namespace import FOAF
  26. >>> ~FOAF.knows
  27. Path(~http://xmlns.com/foaf/0.1/knows)
  28. >>> FOAF.knows/FOAF.name
  29. Path(http://xmlns.com/foaf/0.1/knows / http://xmlns.com/foaf/0.1/name)
  30. >>> FOAF.name|FOAF.givenName
  31. Path(http://xmlns.com/foaf/0.1/name | http://xmlns.com/foaf/0.1/givenName)
  32. ```
  33. Modifiers (?, \*, +) are done using \* (the multiplication operator) and
  34. the strings '\*', '?', '+', also defined as constants in this file.
  35. ```python
  36. >>> FOAF.knows*OneOrMore
  37. Path(http://xmlns.com/foaf/0.1/knows+)
  38. ```
  39. The path objects can also be used with the normal graph methods.
  40. First some example data:
  41. ```python
  42. >>> g=Graph()
  43. >>> g=g.parse(data='''
  44. ... @prefix : <ex:> .
  45. ...
  46. ... :a :p1 :c ; :p2 :f .
  47. ... :c :p2 :e ; :p3 :g .
  48. ... :g :p3 :h ; :p2 :j .
  49. ... :h :p3 :a ; :p2 :g .
  50. ...
  51. ... :q :px :q .
  52. ...
  53. ... ''', format='n3') # doctest: +ELLIPSIS
  54. >>> e = Namespace('ex:')
  55. ```
  56. Graph contains:
  57. ```python
  58. >>> (e.a, e.p1/e.p2, e.e) in g
  59. True
  60. ```
  61. Graph generator functions, triples, subjects, objects, etc. :
  62. ```python
  63. >>> list(g.objects(e.c, (e.p3*OneOrMore)/e.p2)) # doctest: +NORMALIZE_WHITESPACE
  64. [rdflib.term.URIRef('ex:j'), rdflib.term.URIRef('ex:g'),
  65. rdflib.term.URIRef('ex:f')]
  66. ```
  67. A more complete set of tests:
  68. ```python
  69. >>> list(eval_path(g, (None, e.p1/e.p2, None)))==[(e.a, e.e)]
  70. True
  71. >>> list(eval_path(g, (e.a, e.p1|e.p2, None)))==[(e.a,e.c), (e.a,e.f)]
  72. True
  73. >>> list(eval_path(g, (e.c, ~e.p1, None))) == [ (e.c, e.a) ]
  74. True
  75. >>> list(eval_path(g, (e.a, e.p1*ZeroOrOne, None))) == [(e.a, e.a), (e.a, e.c)]
  76. True
  77. >>> list(eval_path(g, (e.c, e.p3*OneOrMore, None))) == [
  78. ... (e.c, e.g), (e.c, e.h), (e.c, e.a)]
  79. True
  80. >>> list(eval_path(g, (e.c, e.p3*ZeroOrMore, None))) == [(e.c, e.c),
  81. ... (e.c, e.g), (e.c, e.h), (e.c, e.a)]
  82. True
  83. >>> list(eval_path(g, (e.a, -e.p1, None))) == [(e.a, e.f)]
  84. True
  85. >>> list(eval_path(g, (e.a, -(e.p1|e.p2), None))) == []
  86. True
  87. >>> list(eval_path(g, (e.g, -~e.p2, None))) == [(e.g, e.j)]
  88. True
  89. >>> list(eval_path(g, (e.e, ~(e.p1/e.p2), None))) == [(e.e, e.a)]
  90. True
  91. >>> list(eval_path(g, (e.a, e.p1/e.p3/e.p3, None))) == [(e.a, e.h)]
  92. True
  93. >>> list(eval_path(g, (e.q, e.px*OneOrMore, None)))
  94. [(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]
  95. >>> list(eval_path(g, (None, e.p1|e.p2, e.c)))
  96. [(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]
  97. >>> list(eval_path(g, (None, ~e.p1, e.a))) == [ (e.c, e.a) ]
  98. True
  99. >>> list(eval_path(g, (None, e.p1*ZeroOrOne, e.c))) # doctest: +NORMALIZE_WHITESPACE
  100. [(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:c')),
  101. (rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:c'))]
  102. >>> list(eval_path(g, (None, e.p3*OneOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
  103. [(rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
  104. (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
  105. (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]
  106. >>> list(eval_path(g, (None, e.p3*ZeroOrMore, e.a))) # doctest: +NORMALIZE_WHITESPACE
  107. [(rdflib.term.URIRef('ex:a'), rdflib.term.URIRef('ex:a')),
  108. (rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a')),
  109. (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
  110. (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a'))]
  111. >>> list(eval_path(g, (None, -e.p1, e.f))) == [(e.a, e.f)]
  112. True
  113. >>> list(eval_path(g, (None, -(e.p1|e.p2), e.c))) == []
  114. True
  115. >>> list(eval_path(g, (None, -~e.p2, e.j))) == [(e.g, e.j)]
  116. True
  117. >>> list(eval_path(g, (None, ~(e.p1/e.p2), e.a))) == [(e.e, e.a)]
  118. True
  119. >>> list(eval_path(g, (None, e.p1/e.p3/e.p3, e.h))) == [(e.a, e.h)]
  120. True
  121. >>> list(eval_path(g, (e.q, e.px*OneOrMore, None)))
  122. [(rdflib.term.URIRef('ex:q'), rdflib.term.URIRef('ex:q'))]
  123. >>> list(eval_path(g, (e.c, (e.p2|e.p3)*ZeroOrMore, e.j)))
  124. [(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:j'))]
  125. ```
  126. No vars specified:
  127. ```python
  128. >>> sorted(list(eval_path(g, (None, e.p3*OneOrMore, None)))) #doctest: +NORMALIZE_WHITESPACE
  129. [(rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:a')),
  130. (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:g')),
  131. (rdflib.term.URIRef('ex:c'), rdflib.term.URIRef('ex:h')),
  132. (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:a')),
  133. (rdflib.term.URIRef('ex:g'), rdflib.term.URIRef('ex:h')),
  134. (rdflib.term.URIRef('ex:h'), rdflib.term.URIRef('ex:a'))]
  135. ```
  136. """
  137. from __future__ import annotations
  138. import warnings
  139. from abc import ABC, abstractmethod
  140. from functools import total_ordering
  141. from typing import (
  142. TYPE_CHECKING,
  143. Any,
  144. Callable,
  145. Generator,
  146. Iterator,
  147. List,
  148. Optional,
  149. Set,
  150. Tuple,
  151. Union,
  152. )
  153. from rdflib.term import Node, URIRef
  154. if TYPE_CHECKING:
  155. from rdflib._type_checking import _MulPathMod
  156. from rdflib.graph import Graph, _ObjectType, _PredicateType, _SubjectType
  157. from rdflib.namespace import NamespaceManager
  158. # property paths
  159. ZeroOrMore = "*"
  160. OneOrMore = "+"
  161. ZeroOrOne = "?"
  162. def _n3(
  163. arg: Union[URIRef, Path], namespace_manager: Optional[NamespaceManager] = None
  164. ) -> str:
  165. if isinstance(arg, (SequencePath, AlternativePath)) and len(arg.args) > 1:
  166. return "(%s)" % arg.n3(namespace_manager)
  167. return arg.n3(namespace_manager)
  168. @total_ordering
  169. class Path(ABC):
  170. """Base class for all property paths."""
  171. __or__: Callable[[Path, Union[URIRef, Path]], AlternativePath]
  172. __invert__: Callable[[Path], InvPath]
  173. __neg__: Callable[[Path], NegatedPath]
  174. __truediv__: Callable[[Path, Union[URIRef, Path]], SequencePath]
  175. __mul__: Callable[[Path, str], MulPath]
  176. @abstractmethod
  177. def eval(
  178. self,
  179. graph: Graph,
  180. subj: Optional[_SubjectType] = None,
  181. obj: Optional[_ObjectType] = None,
  182. ) -> Iterator[Tuple[_SubjectType, _ObjectType]]: ...
  183. @abstractmethod
  184. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str: ...
  185. def __hash__(self):
  186. return hash(repr(self))
  187. def __eq__(self, other):
  188. return repr(self) == repr(other)
  189. def __lt__(self, other: Any) -> bool:
  190. if not isinstance(other, (Path, Node)):
  191. raise TypeError(
  192. "unorderable types: %s() < %s()" % (repr(self), repr(other))
  193. )
  194. return repr(self) < repr(other)
  195. class InvPath(Path):
  196. def __init__(self, arg: Union[Path, URIRef]):
  197. self.arg = arg
  198. def eval(
  199. self,
  200. graph: Graph,
  201. subj: Optional[_SubjectType] = None,
  202. obj: Optional[_ObjectType] = None,
  203. ) -> Generator[Tuple[_ObjectType, _SubjectType], None, None]:
  204. for s, o in eval_path(graph, (obj, self.arg, subj)):
  205. yield o, s
  206. def __repr__(self) -> str:
  207. return "Path(~%s)" % (self.arg,)
  208. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  209. return "^%s" % _n3(self.arg, namespace_manager)
  210. class SequencePath(Path):
  211. def __init__(self, *args: Union[Path, URIRef]):
  212. self.args: List[Union[Path, URIRef]] = []
  213. for a in args:
  214. if isinstance(a, SequencePath):
  215. self.args += a.args
  216. else:
  217. self.args.append(a)
  218. def eval(
  219. self,
  220. graph: Graph,
  221. subj: Optional[_SubjectType] = None,
  222. obj: Optional[_ObjectType] = None,
  223. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  224. def _eval_seq(
  225. paths: List[Union[Path, URIRef]],
  226. subj: Optional[_SubjectType],
  227. obj: Optional[_ObjectType],
  228. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  229. if paths[1:]:
  230. for s, o in eval_path(graph, (subj, paths[0], None)):
  231. for r in _eval_seq(paths[1:], o, obj):
  232. yield s, r[1]
  233. else:
  234. for s, o in eval_path(graph, (subj, paths[0], obj)):
  235. yield s, o
  236. def _eval_seq_bw(
  237. paths: List[Union[Path, URIRef]],
  238. subj: Optional[_SubjectType],
  239. obj: _ObjectType,
  240. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  241. if paths[:-1]:
  242. for s, o in eval_path(graph, (None, paths[-1], obj)):
  243. for r in _eval_seq(paths[:-1], subj, s):
  244. yield r[0], o
  245. else:
  246. for s, o in eval_path(graph, (subj, paths[0], obj)):
  247. yield s, o
  248. if subj:
  249. return _eval_seq(self.args, subj, obj)
  250. elif obj:
  251. return _eval_seq_bw(self.args, subj, obj)
  252. else: # no vars bound, we can start anywhere
  253. return _eval_seq(self.args, subj, obj)
  254. def __repr__(self) -> str:
  255. return "Path(%s)" % " / ".join(str(x) for x in self.args)
  256. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  257. return "/".join(_n3(a, namespace_manager) for a in self.args)
  258. class AlternativePath(Path):
  259. def __init__(self, *args: Union[Path, URIRef]):
  260. self.args: List[Union[Path, URIRef]] = []
  261. for a in args:
  262. if isinstance(a, AlternativePath):
  263. self.args += a.args
  264. else:
  265. self.args.append(a)
  266. def eval(
  267. self,
  268. graph: Graph,
  269. subj: Optional[_SubjectType] = None,
  270. obj: Optional[_ObjectType] = None,
  271. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  272. for x in self.args:
  273. for y in eval_path(graph, (subj, x, obj)):
  274. yield y
  275. def __repr__(self) -> str:
  276. return "Path(%s)" % " | ".join(str(x) for x in self.args)
  277. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  278. return "|".join(_n3(a, namespace_manager) for a in self.args)
  279. class MulPath(Path):
  280. def __init__(self, path: Union[Path, URIRef], mod: _MulPathMod):
  281. self.path = path
  282. self.mod = mod
  283. if mod == ZeroOrOne:
  284. self.zero = True
  285. self.more = False
  286. elif mod == ZeroOrMore:
  287. self.zero = True
  288. self.more = True
  289. elif mod == OneOrMore:
  290. self.zero = False
  291. self.more = True
  292. else:
  293. raise Exception("Unknown modifier %s" % mod)
  294. def eval(
  295. self,
  296. graph: Graph,
  297. subj: Optional[_SubjectType] = None,
  298. obj: Optional[_ObjectType] = None,
  299. first: bool = True,
  300. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  301. if self.zero and first:
  302. if subj and obj:
  303. if subj == obj:
  304. yield subj, obj
  305. elif subj:
  306. yield subj, subj
  307. elif obj:
  308. yield obj, obj
  309. def _fwd(
  310. subj: Optional[_SubjectType] = None,
  311. obj: Optional[_ObjectType] = None,
  312. seen: Optional[Set[_SubjectType]] = None,
  313. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  314. # type error: Item "None" of "Optional[Set[Node]]" has no attribute "add"
  315. # type error: Argument 1 to "add" of "set" has incompatible type "Optional[Node]"; expected "Node"
  316. seen.add(subj) # type: ignore[union-attr, arg-type]
  317. for s, o in eval_path(graph, (subj, self.path, None)):
  318. if not obj or o == obj:
  319. yield s, o
  320. if self.more:
  321. # type error: Unsupported right operand type for in ("Optional[Set[Node]]")
  322. if o in seen: # type: ignore[operator]
  323. continue
  324. for s2, o2 in _fwd(o, obj, seen):
  325. yield s, o2
  326. def _bwd(
  327. subj: Optional[_SubjectType] = None,
  328. obj: Optional[_ObjectType] = None,
  329. seen: Optional[Set[_ObjectType]] = None,
  330. ) -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  331. # type error: Item "None" of "Optional[Set[Node]]" has no attribute "add"
  332. # type error: Argument 1 to "add" of "set" has incompatible type "Optional[Node]"; expected "Node"
  333. seen.add(obj) # type: ignore[union-attr, arg-type]
  334. for s, o in eval_path(graph, (None, self.path, obj)):
  335. if not subj or subj == s:
  336. yield s, o
  337. if self.more:
  338. # type error: Unsupported right operand type for in ("Optional[Set[Node]]")
  339. if s in seen: # type: ignore[operator]
  340. continue
  341. for s2, o2 in _bwd(None, s, seen):
  342. yield s2, o
  343. def _all_fwd_paths() -> Generator[Tuple[_SubjectType, _ObjectType], None, None]:
  344. if self.zero:
  345. seen1 = set()
  346. # According to the spec, ALL nodes are possible solutions
  347. # (even literals)
  348. # we cannot do this without going through ALL triples
  349. # unless we keep an index of all terms somehow
  350. # but let's just hope this query doesn't happen very often...
  351. for s, o in graph.subject_objects(None):
  352. if s not in seen1:
  353. seen1.add(s)
  354. yield s, s
  355. if o not in seen1:
  356. seen1.add(o)
  357. yield o, o
  358. seen = set()
  359. for s, o in eval_path(graph, (None, self.path, None)):
  360. if not self.more:
  361. yield s, o
  362. else:
  363. if s not in seen:
  364. seen.add(s)
  365. f = list(_fwd(s, None, set()))
  366. for s1, o1 in f:
  367. assert s1 == s
  368. yield s1, o1
  369. done = set() # the spec does, by defn, not allow duplicates
  370. if subj:
  371. for x in _fwd(subj, obj, set()):
  372. if x not in done:
  373. done.add(x)
  374. yield x
  375. elif obj:
  376. for x in _bwd(subj, obj, set()):
  377. if x not in done:
  378. done.add(x)
  379. yield x
  380. else:
  381. for x in _all_fwd_paths():
  382. if x not in done:
  383. done.add(x)
  384. yield x
  385. def __repr__(self) -> str:
  386. return "Path(%s%s)" % (self.path, self.mod)
  387. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  388. return "%s%s" % (_n3(self.path, namespace_manager), self.mod)
  389. class NegatedPath(Path):
  390. def __init__(self, arg: Union[AlternativePath, InvPath, URIRef]):
  391. self.args: List[Union[URIRef, Path]]
  392. if isinstance(arg, (URIRef, InvPath)):
  393. self.args = [arg]
  394. elif isinstance(arg, AlternativePath):
  395. self.args = arg.args
  396. else:
  397. raise Exception(
  398. "Can only negate URIRefs, InvPaths or "
  399. + "AlternativePaths, not: %s" % (arg,)
  400. )
  401. def eval(self, graph, subj=None, obj=None):
  402. for s, p, o in graph.triples((subj, None, obj)):
  403. for a in self.args:
  404. if isinstance(a, URIRef):
  405. if p == a:
  406. break
  407. elif isinstance(a, InvPath):
  408. if (o, a.arg, s) in graph:
  409. break
  410. else:
  411. raise Exception("Invalid path in NegatedPath: %s" % a)
  412. else:
  413. yield s, o
  414. def __repr__(self) -> str:
  415. return "Path(! %s)" % ",".join(str(x) for x in self.args)
  416. def n3(self, namespace_manager: Optional[NamespaceManager] = None) -> str:
  417. return "!(%s)" % ("|".join(_n3(arg, namespace_manager) for arg in self.args))
  418. class PathList(list):
  419. pass
  420. def path_alternative(self: Union[URIRef, Path], other: Union[URIRef, Path]):
  421. """
  422. alternative path
  423. """
  424. if not isinstance(other, (URIRef, Path)):
  425. raise Exception("Only URIRefs or Paths can be in paths!")
  426. return AlternativePath(self, other)
  427. def path_sequence(self: Union[URIRef, Path], other: Union[URIRef, Path]):
  428. """
  429. sequence path
  430. """
  431. if not isinstance(other, (URIRef, Path)):
  432. raise Exception("Only URIRefs or Paths can be in paths!")
  433. return SequencePath(self, other)
  434. def evalPath( # noqa: N802
  435. graph: Graph,
  436. t: Tuple[
  437. Optional[_SubjectType],
  438. Union[None, Path, _PredicateType],
  439. Optional[_ObjectType],
  440. ],
  441. ) -> Iterator[Tuple[_SubjectType, _ObjectType]]:
  442. warnings.warn(
  443. DeprecationWarning(
  444. "rdflib.path.evalPath() is deprecated, use the (snake-cased) eval_path(). "
  445. "The mixed-case evalPath() function name is incompatible with PEP8 "
  446. "recommendations and will be replaced by eval_path() in rdflib 7.0.0."
  447. )
  448. )
  449. return eval_path(graph, t)
  450. def eval_path(
  451. graph: Graph,
  452. t: Tuple[
  453. Optional[_SubjectType],
  454. Union[None, Path, _PredicateType],
  455. Optional[_ObjectType],
  456. ],
  457. ) -> Iterator[Tuple[_SubjectType, _ObjectType]]:
  458. return ((s, o) for s, p, o in graph.triples(t))
  459. def mul_path(p: Union[URIRef, Path], mul: _MulPathMod) -> MulPath:
  460. """
  461. cardinality path
  462. """
  463. return MulPath(p, mul)
  464. def inv_path(p: Union[URIRef, Path]) -> InvPath:
  465. """
  466. inverse path
  467. """
  468. return InvPath(p)
  469. def neg_path(p: Union[URIRef, AlternativePath, InvPath]) -> NegatedPath:
  470. """
  471. negated path
  472. """
  473. return NegatedPath(p)
  474. if __name__ == "__main__":
  475. pass
  476. else:
  477. # monkey patch
  478. # (these cannot be directly in terms.py
  479. # as it would introduce circular imports)
  480. URIRef.__or__ = path_alternative
  481. # ignore typing here as URIRef inherits from str,
  482. # which has an incompatible definition of __mul__.
  483. URIRef.__mul__ = mul_path # type: ignore
  484. URIRef.__invert__ = inv_path
  485. URIRef.__neg__ = neg_path
  486. URIRef.__truediv__ = path_sequence
  487. Path.__invert__ = inv_path
  488. # type error: Incompatible types in assignment (expression has type "Callable[[Union[URIRef, AlternativePath, InvPath]], NegatedPath]", variable has type "Callable[[Path], NegatedPath]")
  489. Path.__neg__ = neg_path # type: ignore[assignment]
  490. # type error: Incompatible types in assignment (expression has type "Callable[[Union[URIRef, Path], Literal['*', '+', '?']], MulPath]", variable has type "Callable[[Path, str], MulPath]")
  491. Path.__mul__ = mul_path # type: ignore[assignment]
  492. Path.__or__ = path_alternative
  493. Path.__truediv__ = path_sequence