-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix negative type narrowing for isinstance/issubclass with type[T] variable or tuple #11616
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # This sample tests type narrowing for isinstance and issubclass when | ||
| # the class argument is passed as a type[T] variable or tuple of type[T]. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The sample and PR claim coverage for both |
||
|
|
||
| # pyright: reportMissingModuleSource=false | ||
|
|
||
| from typing import final | ||
| from typing_extensions import reveal_type | ||
|
|
||
|
|
||
| class A: | ||
| pass | ||
|
|
||
|
|
||
| class B: | ||
| pass | ||
|
|
||
|
|
||
| @final | ||
| class FinalClass: | ||
| pass | ||
|
|
||
|
|
||
| def test_positive_narrowing(x: A | B, cls: type[A]): | ||
| if isinstance(x, cls): | ||
| reveal_type(x, expected_text="A") | ||
|
|
||
|
|
||
| def test_positive_tuple_param(x: A | B, types: tuple[type[A], type[B]]): | ||
| if isinstance(x, types): | ||
| reveal_type(x, expected_text="A | B") | ||
|
|
||
|
|
||
| def test_final_class_param(x: FinalClass | B, cls: type[FinalClass]): | ||
| if isinstance(x, cls): | ||
| reveal_type(x, expected_text="FinalClass") | ||
| else: | ||
| reveal_type(x, expected_text="B") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Please add a non-final negative case.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Add regression coverage for |
||
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.
The instantiable
typeArgbranch assigns the filter directly, while the instance-form branch converts it and thereby preservesincludeSubclasses. Normalize the instantiable branch as well so any future representation reaching it cannot bypass the conservative non-final negative-narrowing behavior. [verified]