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

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

Merged
merged 2 commits into from Oct 25, 2018

Conversation

Projects
None yet
3 participants
@ilevkivskyi
Copy link
Collaborator

ilevkivskyi commented Oct 24, 2018

Fixes #5667

@ilevkivskyi ilevkivskyi requested a review from JukkaL Oct 24, 2018

----------------------------------------------------------

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

This comment has been minimized.

@JelleZijlstra

JelleZijlstra Oct 24, 2018

Collaborator

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

This comment has been minimized.

@JukkaL

JukkaL Oct 24, 2018

Collaborator

"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

This comment has been minimized.

@JukkaL

JukkaL Oct 24, 2018

Collaborator

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

from typing import TYPE_CHECKING
if TYPE_CHECKING:
BaseQueue = Queue[str]

This comment has been minimized.

@JukkaL

JukkaL Oct 24, 2018

Collaborator

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

if TYPE_CHECKING:
BaseQueue = Queue[str]
else:
BaseQueue = Queue

This comment has been minimized.

@JukkaL

JukkaL Oct 24, 2018

Collaborator

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

Ivan Levkivskyi
@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator

ilevkivskyi commented Oct 24, 2018

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

@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator

ilevkivskyi commented Oct 25, 2018

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

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@ilevkivskyi ilevkivskyi deleted the ilevkivskyi:doc-non-generic branch Oct 25, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment