Skip to content

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

Merged
merged 1 commit into from
Oct 20, 2017

Conversation

Wilfred
Copy link
Contributor

@Wilfred Wilfred commented Oct 9, 2017

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.

def load_all(stream, Loader=...): ...
def safe_load(stream): ...
def safe_load_all(stream): ...
def load(stream, Loader=...): ... # type: (Any, Loader) -> Any
Copy link
Contributor

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: ...

?

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@Wilfred Wilfred force-pushed the yaml_type_annotations branch 3 times, most recently from d6907d7 to 9a36414 Compare October 10, 2017 15:49
These are probably the most common YAML functions, so this should be
useful even without type declarations on the rest of the package.
@Wilfred Wilfred force-pushed the yaml_type_annotations branch from 9a36414 to e784ef1 Compare October 11, 2017 09:40
@Wilfred
Copy link
Contributor Author

Wilfred commented Oct 20, 2017

@matthiaskramm @JelleZijlstra do I need to make any further changes to get this merged?

@matthiaskramm
Copy link
Contributor

Looks good to me. Merging.

@matthiaskramm matthiaskramm merged commit 72e24b8 into python:master Oct 20, 2017
def load_all(stream, Loader=...): ...
def safe_load(stream): ...
def safe_load_all(stream): ...
def load(stream: Union[str, IO[str]], Loader: Loader=...) -> Any: ...
Copy link
Member

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.)

Copy link
Member

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.

@Wilfred Wilfred deleted the yaml_type_annotations branch October 27, 2017 12:10
gvanrossum pushed a commit that referenced this pull request Oct 27, 2017
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)
gvanrossum added a commit that referenced this pull request Oct 27, 2017
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants