Skip to content
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

add asyncio.__all__ #13038

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

add asyncio.__all__ #13038

wants to merge 3 commits into from

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Nov 18, 2024

This passes the stubtest checks for __all__ in the master branch of mypy.

Runtime just sums over the __all__ imported from each of the submodules. I tried that, but mypy didn't pick it up properly, so here's the explicit version, in all its messy glory.

@tungol
Copy link
Contributor Author

tungol commented Nov 18, 2024

Neither pyright nor pytype like this very much.

This comment has been minimized.

@tungol
Copy link
Contributor Author

tungol commented Nov 18, 2024

It seems that pyright and pytype gained support for += on __all__ in #7865 if __all__ is a list, but not if it's a tuple. I could work around that here, but that seems like it'd be even worse than this version.

@tungol
Copy link
Contributor Author

tungol commented Nov 18, 2024

The pytype error is slightly different. mypy complained about changing the length of the tuple type, which I worked around with the annotation of it as tuple[str, ...]. That's making pytype complain that __all__ isn't a literal. I guess with a list __all__, using += preserves the Literal status?

I'm not certain what, if any, annotation would satisfy both needs.

@tungol tungol marked this pull request as draft November 18, 2024 21:20

This comment has been minimized.

@tungol
Copy link
Contributor Author

tungol commented Nov 19, 2024

Well, as Eric pointed out, this isn't in the typing spec.

Technically this doesn't go beyond the typing spec:

__all__ = ('a', 'b')
__all__ += submodule.__all__

where submodule is also a tuple __all__. But mypy doesn't support this and I suspect neither does pyright.

@tungol tungol marked this pull request as ready for review November 20, 2024 09:26
@tungol
Copy link
Contributor Author

tungol commented Nov 20, 2024

I updated the MR to just be one giant platform x version matrix. It's not pretty and updating it won't be very ergonomic, but I don't see what other choice we have.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

I'm honestly not a fan of duplicating all items in each branch instead of using __all__ += ... inside the branches.

@tungol
Copy link
Contributor Author

tungol commented Nov 20, 2024

Me neither, but __all__ in these files is a tuple at runtime. Mypy and pyright don't support using __all__ += ... with tuple __all__.

If we want to diverge from runtime and use a list for __all__, then this could be a lot nicer. I didn't do that because all the submodules here match runtime at the cost of this sort of thing already.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

I'm unsure what the better option is, but your logic makes sense.

@Akuli
Copy link
Collaborator

Akuli commented Nov 20, 2024

Can't we just use lists if they work better with type checkers? In basically every use case of __all__, it doesn't matter whether it's a list or a tuple.

@tungol
Copy link
Contributor Author

tungol commented Nov 20, 2024

If we do that, we should probably get stubtest updated to ignore the type discrepancy on __all__. If we have to add asyncio.*.__all__ to the allowlist for the tuple/list mismatch, it will also hide the more important names exported from the stub do not correspond to the names exported at runtime error.

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.

3 participants