-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Infer types from issubclass() calls #3005
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
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
158c889
Make no inference when variables present in second argument to isinst…
pkch c5525bd
Add failing unit tests
pkch 1776e90
Infer type from issubclass
pkch 3334b8e
Apply CR comments
pkch 3587549
Fix 3.3 syntax, updated typeshed
pkch 81e1923
More CR fixes
pkch e4ffdc5
Fix testr
pkch 9440a52
Revert accidental format change
pkch c831401
Merge branch 'master' into issubclass
pkch a4a7214
Merge branch 'master' into issubclass
pkch f94ca45
Address CR
pkch a260b79
Rename type into typ
pkch fbd0b91
Merge branch 'master' into issubclass
pkch e48d983
Fix unreachable block crash
pkch b0ce683
Add back integration tests
pkch aca05c7
Add union destructuring test
pkch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1423,6 +1423,123 @@ def f(x: Union[int, A], a: Type[A]) -> None: | |
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclassUnreachable] | ||
from typing import Type, Sequence, Union | ||
x: Type[str] | ||
if issubclass(x, int): | ||
reveal_type(x) # unreachable block | ||
|
||
|
||
class X: pass | ||
class Y(X): pass | ||
class Z(X): pass | ||
|
||
a: Union[Type[Y], Type[Z]] | ||
if issubclass(a, X): | ||
reveal_type(a) # E: Revealed type is 'Union[Type[__main__.Y], Type[__main__.Z]]' | ||
else: | ||
reveal_type(a) # unreachable block | ||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclass] | ||
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. Is it possible to split this test into few smaller unit tests? |
||
from typing import Type | ||
|
||
class Goblin: | ||
pass | ||
|
||
class GoblinAmbusher(Goblin): | ||
pass | ||
|
||
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. One blanc line is enough here |
||
def test_issubclass(cls: Type[Goblin]) -> None: | ||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
else: | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]' | ||
|
||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclassDeepHierarchy] | ||
from typing import Type | ||
|
||
class Mob: | ||
pass | ||
|
||
class Goblin(Mob): | ||
pass | ||
|
||
class GoblinAmbusher(Goblin): | ||
pass | ||
|
||
def test_issubclass(cls: Type[Mob]) -> None: | ||
if issubclass(cls, Goblin): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]' | ||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
else: | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.Mob]' | ||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
|
||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclassTuple] | ||
from typing import Type | ||
|
||
class Mob: | ||
pass | ||
|
||
class Goblin(Mob): | ||
pass | ||
|
||
class GoblinAmbusher(Goblin): | ||
pass | ||
|
||
class GoblinDigger(Goblin): | ||
pass | ||
|
||
def test_issubclass(cls: Type[Mob]) -> None: | ||
if issubclass(cls, (Goblin, GoblinAmbusher)): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.Goblin]' | ||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
else: | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.Mob]' | ||
if issubclass(cls, GoblinAmbusher): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.GoblinAmbusher]' | ||
if issubclass(cls, (GoblinDigger, GoblinAmbusher)): | ||
reveal_type(cls) # E: Revealed type is 'Union[Type[__main__.GoblinDigger], Type[__main__.GoblinAmbusher]]' | ||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclassBuiltins] | ||
from typing import List, Type | ||
|
||
class MyList(List): pass | ||
class MyIntList(List[int]): pass | ||
|
||
def f(cls: Type[object]) -> None: | ||
if issubclass(cls, MyList): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.MyList]' | ||
cls()[0] | ||
else: | ||
reveal_type(cls) # E: Revealed type is 'Type[builtins.object]' | ||
cls()[0] # E: Value of type "object" is not indexable | ||
|
||
if issubclass(cls, MyIntList): | ||
reveal_type(cls) # E: Revealed type is 'Type[__main__.MyIntList]' | ||
cls()[0] + 1 | ||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
[case testIsinstanceTypeArgs] | ||
from typing import Iterable, TypeVar | ||
x = 1 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you are sure this is an unreachable branch, then you could put
assert False
here. If this could happen as a result of an error that is reported elsewhere, then adjust the comment correspondingly.