-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Iterate over dataset return quads #1382
Conversation
rdflib/graph.py
Outdated
@@ -1896,6 +1912,11 @@ def quads(self, quad): | |||
else: | |||
yield s, p, o, c.identifier | |||
|
|||
def __iter__(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I'm just some guy commenting and should not be considered to be someone with any authority here.
I'd recommend putting a type hint here so that the codebase gradually shifts towards full type hint / mypy support. I believe that the following would work:
from typing import Optional, Union
# For python < 3.9, else built-in tuple and collections.abc.Generator
from typing import Generator, Tuple
# Type aliases to make unpacking what's going on a little more human friendly
ContextNode = Union[BNode, URIRef]
DatasetQuad = Tuple[Node, URIRef, Node, Optional[ContextNode]]
...
def __iter__(self) -> Generator[DatasetQuad, None, None]:
...
I think that the alias that I suggested above for DatasetQuad
may even be able to be made more specific. The usage of URIRef
reflects the RDF specification that the properties of triples are all URI references. Analogous restrictions exist on the subjects (I believe that literals cannot be subjects, for example). It may be helpful to look into what the context field can be. I believe the context here can only be None
, a BNode
, or a URIRef
, but it's been a little bit since I looked around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend putting a type hint here
Yes please! Our (the maintainers') unofficial policy is that type hinting should be present for all new RDFlib additions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Type aliases to make unpacking what's going on a little more human friendly
Cool!
Thanks @kvjrhall: I have added your type hinting suggestions to this PR as well as a test. If it passes all tests still - it passes all tests with Py3.7 & Py3.9 on my machine - I'll merge. |
OK, everything passes, as expected, so merging. Thanks @juanjosegzl and @kvjrhall |
Fixes #1351
Proposed Changes