Widen Graph.__contains__ type hint by EFord36 · Pull Request #2323 · RDFLib/rdflib (original) (raw)

Summary of changes

The typing on Graph.__contains__ doesn't currently allow rdflib.paths.Paths in the predicate position of the triple argument, even though the implementation supports this (it calls the triple method, which is type hinted to accept Paths and handles them fine). This can cause mypy errors for rdflib users on valid code. Minimal repro:

import rdflib

g = rdflib.Graph()

skos_xl_label_path = rdflib.URIRef("http://www.w3.org/2008/05/skos-xl#Label") / rdflib.URIRef("http://www.w3.org/2008/05/skos-xl#literalForm")

print((rdflib.URIRef("http://example.org/ns#bob"), skos_xl_label_path, rdflib.Literal("Bob")) in g)

gives a mypy error:

test_rdflib_typing.py:8: error: Unsupported operand types for in ("Tuple[URIRef, SequencePath, Literal]" and "Graph")  [operator]

which goes away after the change in this PR.

Occurence in my codebase here that triggers the above error (although some context is required to see why, I just wanted to point out that this was a real problem I faced and had to add a # type: ignore for, not just a hypothetical).

I also removed some commented out type hints that aren't used or referenced anywhere while I was staring at them - this looked like useful cleanup but let me know if it isn't and I'll happily remove the commit from the PR.

Thanks for rdflib, and for adding (and distributing) the type hints!

Checklist