-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
docs/source/common_issues.rst
Outdated
---------------------------------------------------------- | ||
|
||
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 |
There was a problem hiding this comment.
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:"
docs/source/common_issues.rst
Outdated
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``: |
There was a problem hiding this comment.
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".
docs/source/common_issues.rst
Outdated
|
||
results: Queue[int] = Queue() # TypeError: 'type' object is not subscriptable | ||
|
||
To avoid these errors while still having precise types one can either use |
There was a problem hiding this comment.
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.
docs/source/common_issues.rst
Outdated
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
BaseQueue = Queue[str] |
There was a problem hiding this comment.
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.
docs/source/common_issues.rst
Outdated
if TYPE_CHECKING: | ||
BaseQueue = Queue[str] | ||
else: | ||
BaseQueue = Queue |
There was a problem hiding this comment.
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.
@JelleZijlstra @JukkaL thanks for review! I pushed an update. |
As I understand there are no more comments, so I am going to merge this soon. |
Fixes #5667