Skip to content

Add type stubs document #844

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
Aug 23, 2021
Merged

Add type stubs document #844

merged 1 commit into from
Aug 23, 2021

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Aug 23, 2021

This was originally intended to become a stub and was written by:

Sebastian Rittau sebastian.rittau@zfutura.de
Rebecca Chen rechen@google.com
Teddy Sudol tsudol@google.com
Jelle Zijlstra jelle.zijlstra@gmail.com

Also, remove unnecessary modindex link from main index.

This was originally intended to become a stub and was written by:

Sebastian Rittau <sebastian.rittau@zfutura.de>
Rebecca Chen <rechen@google.com>
Teddy Sudol <tsudol@google.com>
Jelle Zijlstra <jelle.zijlstra@gmail.com>

Also, remove unnecessary modindex link from main index.
@srittau
Copy link
Collaborator Author

srittau commented Aug 23, 2021

At the moment, this is a 1:1 copy of the proposed PEP, minus the header. I want to iterate on that in future PRs, but for now I wanted to have a pristine copy of our last version in the history.

@srittau
Copy link
Collaborator Author

srittau commented Aug 23, 2021


import X
from Y import X
from Y import X as OtherX
Copy link

Choose a reason for hiding this comment

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

I thought from Y import X as OtherX would re-export. What is the rationale for not re-exporting in this case? If you don't want to re-export, you can always do from Y import X as _OtherX, which most stubs in typeshed already do in this case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It used to re-export, this was changed / clarified in PEP 484 somewhat recently (python/peps#1630). The reason is more relevant for .py than it is for .pyi — basically to better match the people's reexport intentions in the wild — but the discussion was here: https://mail.python.org/archives/list/typing-sig@python.org/thread/YLJPWECBNPD2K4TRIBRIPISNUZJCRREY/#FPTLFORIBPKY36UO7PLO47HJDCLG2KD5

Copy link

Choose a reason for hiding this comment

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

So the rule is now simpler than ever before, as always re-exports?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

as only re-exports, if the alias is the same as the original name.

Comment on lines +154 to +155
Then ``foo`` will export ``bar`` when one of the following constructs is used in
``__init__.pyi``::
Copy link

Choose a reason for hiding this comment

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

It might be good to emphasize that there is no good way to avoid re-exporting. For example, tkinter/__init__.py has some methods that accept tkinter.font.Font objects, even though import tkinter does not import tkinter/font.py. As far as I know, there's no way to express this in the type system, and mypy thinks that import tkinter also loads tkinter.font even though it doesn't.

Comment on lines +472 to +476
Currently, positional-only argument syntax (PEP 570 [#pep570]_),
unions using the pipe operator (``|``) (PEP 604 [#pep604]_),
``ParamSpec`` (PEP 612 [#pep612]_), and ``TypeAlias`` (PEP 613 [#pep613]_)
are not supported by all type
checkers and should not be used in stubs.
Copy link

Choose a reason for hiding this comment

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

Even though documentation shouldn't be used when it is outdated, I don't think it's great if the documentation is outdated the moment it is created :)

I think it would be better to either explain when | unions should be used, or wait until we can just always use them in typeshed. Not all stubs go to typeshed, but it just doesn't make much sense to me to intentionally have more or less official documentation disagreeing with typeshed's practices.

Use ``float`` instead of ``Union[int, float]``.
Use ``None`` instead of ``Literal[None]``.
For argument types,
use ``bytes`` instead of ``Union[bytes, memoryview, bytearray]``.
Copy link

Choose a reason for hiding this comment

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

I think this needs a bit more explaining, or a link to somewhere where it's documented, since it does not match runtime behaviour:

>>> isinstance(bytearray(), bytes)
False

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This should probably be more prominent in the rest of the typing documentation (it is explained in the typing module docs), but the style guide is not really the correct place to explain it, just as float == float | int is not explained.

Comment on lines +950 to +951
Avoid ``Union`` return types, since they require ``isinstance()`` checks.
Use ``Any`` if necessary.
Copy link

@Akuli Akuli Aug 23, 2021

Choose a reason for hiding this comment

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

Maybe this part should mention the str | Any trick. A return type like str | Any means that it is not an error to treat the return value as a string, and you always have to allow it to be a string, but it could be something else too.

@srittau
Copy link
Collaborator Author

srittau commented Aug 23, 2021

Thank you for your comments. I will revisit them once this gets merged. For the time being, I want to merge this "as is" to have a "pristine" version in the git history.

@Akuli
Copy link

Akuli commented Aug 23, 2021

This was originally intended to become a stub

Become a PEP maybe?

What happened to this PEP? Did it never become a PEP? I'm mostly worried about having outdated information in a PEP that shows up in google searches.

@JelleZijlstra
Copy link
Member

Yes, we intended to submit it as a PEP, but never did because we couldn't find a sponsor. The typing docs are now probably a better fit. The PEP is unlikely to show up in Google searches because it just existed in @srittau's repo.

@JelleZijlstra JelleZijlstra merged commit d7f07b7 into python:master Aug 23, 2021
@srittau srittau deleted the stubs-doc branch August 23, 2021 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants