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.Path
s 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 Path
s 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
- Checked that there aren't other open pull requests for
the same change. - Added tests for any changes that have a runtime impact.
- Checked that all tests and type checking passes. (checked mypy - no new/related errors)
- For changes that have a potential impact on users of this project:
- Updated relevant documentation to avoid inaccuracies.
- Considered adding additional documentation.
- Considered adding an example in
./examples
for new features. - Considered updating our changelog (
CHANGELOG.md
).
- Considered granting push permissions to the PR branch,
so maintainers can fix minor issues and keep your PR up to date.