-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add basic type annotations for YAML load functions #1657
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
Conversation
third_party/2and3/yaml/__init__.pyi
Outdated
def load_all(stream, Loader=...): ... | ||
def safe_load(stream): ... | ||
def safe_load_all(stream): ... | ||
def load(stream, Loader=...): ... # type: (Any, Loader) -> Any |
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.
It seems weird to use comment-based annotations here. Why not just
def load(stream: Any, Loader: Loader = ...) -> Any: ...
?
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.
Also, for the stream, could something like Union[str, IO[str]]
work?
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.
Updated.
d6907d7
to
9a36414
Compare
These are probably the most common YAML functions, so this should be useful even without type declarations on the rest of the package.
9a36414
to
e784ef1
Compare
@matthiaskramm @JelleZijlstra do I need to make any further changes to get this merged? |
Looks good to me. Merging. |
def load_all(stream, Loader=...): ... | ||
def safe_load(stream): ... | ||
def safe_load_all(stream): ... | ||
def load(stream: Union[str, IO[str]], Loader: Loader=...) -> Any: ... |
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 discovered a problem with the annotation for Loader
here. IIUC it's intended that this argument can be an instance of BaseLoader
or SafeLoader
as well. But given the inheritance structure in loader.pyi
that doesn't work -- if I pass an instance of SafeLoader
I get a type error. Maybe you can fix this by declaring this argument (here and below) as Union[BaseLoader, SafeLoader, Loader]
? (Do double check that I got this right -- I'm not a user of yaml myself, but I found this caused a regression in an internal Dropbox codebase that uses it.)
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 fixed this in #1695.
There were two problems AFAICT: - There are multiple Loader classes without a common base class, so this should really be a union or a protocol. - The argument is supposed to be a class, not an instance. Rather than figuring out a fix (which would probably require some nasty refactoring) I'm just going back to the old situation where these arguments are implicitly annotated with `Any`. See also #1657 (review)
There were two problems AFAICT: - There are multiple Loader classes without a common base class, so this should really be a union or a protocol. - The argument is supposed to be a class, not an instance. Rather than figuring out a fix (which would probably require some nasty refactoring) I'm just going back to the old situation where these arguments are implicitly annotated with `Any`. See also #1657 (review)
These are probably the most common YAML functions, so this should be
useful even without type declarations on the rest of the package.
This is my first contribution to typeshed, so please let me know if this patch makes sense. I tried to find a more specific type for the
stream
argument (I know it supports both strings and file objects) but I couldn't find a definitive list of the types supported.