Skip to content

Document what to do if a class is not generic at runtime #5833

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 2 commits into from
Oct 25, 2018

Conversation

ilevkivskyi
Copy link
Member

Fixes #5667

@ilevkivskyi ilevkivskyi requested a review from JukkaL October 24, 2018 12:08
----------------------------------------------------------

In some cases a class may be declared as generic in stubs, while
it is not generic at runtime. For example this is the case for some
Copy link
Member

Choose a reason for hiding this comment

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

Suggested rewording: "Some classes are declared as generic in stubs, but not at runtime. Examples in the standard library include os.PathLike and queue.Queue. Subscripting such a class will result in a runtime error:"

results: Queue[int] = Queue() # TypeError: 'type' object is not subscriptable

To avoid these errors while still having precise types one can either use
string literals or ``typing.TYPE_CHECKING``:
Copy link
Collaborator

Choose a reason for hiding this comment

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

"string literals" is a bit vague. Maybe reword as "string literal types".


results: Queue[int] = Queue() # TypeError: 'type' object is not subscriptable

To avoid these errors while still having precise types one can either use
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style nit: use "you" instead of "one" for consistency.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
BaseQueue = Queue[str]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add descriptive comment here -- this is only processed by mypy.

if TYPE_CHECKING:
BaseQueue = Queue[str]
else:
BaseQueue = Queue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Another descriptive comment could be helpful here -- this is not seen by mypy but will be executed at runtime.

@ilevkivskyi
Copy link
Member Author

@JelleZijlstra @JukkaL thanks for review! I pushed an update.

@ilevkivskyi
Copy link
Member Author

As I understand there are no more comments, so I am going to merge this soon.

@ilevkivskyi ilevkivskyi merged commit f256f86 into python:master Oct 25, 2018
@ilevkivskyi ilevkivskyi deleted the doc-non-generic branch October 25, 2018 08:38
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