-
-
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 1 commit
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
Prev
Previous commit
Add union destructuring test
- Loading branch information
commit aca05c7d4e271686f11335a9157a8db18e525587
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1443,6 +1443,28 @@ else: | |
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubclasDestructuringUnions] | ||
from typing import Union, List, Tuple, Dict, Type | ||
def f(x: Union[Type[int], Type[str], Type[List]]) -> None: | ||
if issubclass(x, (str, (int,))): | ||
reveal_type(x) # E: Revealed type is 'Union[Type[builtins.int], Type[builtins.str]]' | ||
reveal_type(x()) # E: Revealed type is 'Union[builtins.int, builtins.str]' | ||
x()[1] # E: Value of type "Union[int, str]" is not indexable | ||
else: | ||
reveal_type(x) # E: Revealed type is 'Type[builtins.list]' | ||
reveal_type(x()) # E: Revealed type is 'builtins.list[<uninhabited>]' | ||
x()[1] | ||
reveal_type(x) # E: Revealed type is 'Union[Type[builtins.int], Type[builtins.str], Type[builtins.list]]' | ||
reveal_type(x()) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.list[<uninhabited>]]' | ||
if issubclass(x, (str, (list,))): | ||
reveal_type(x) # E: Revealed type is 'Union[Type[builtins.str], Type[builtins.list[Any]]]' | ||
reveal_type(x()) # E: Revealed type is 'Union[builtins.str, builtins.list[<uninhabited>]]' | ||
x()[1] | ||
reveal_type(x) # E: Revealed type is 'Union[Type[builtins.int], Type[builtins.str], Type[builtins.list[Any]]]' | ||
reveal_type(x()) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.list[<uninhabited>]]' | ||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIssubc 10000 lass] | ||
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, ClassVar | ||
|
||
|
Submodule typeshed
updated
53 files
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.
@pkch Is it possible to use
Type[Union[int, str, List]]
here?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.
Wow, it is, but I thought it's illegal? I thought
Type
requires only simple types inside, not constructs likeUnion
etc.?But it 50% works... Here's the output. The first couple lines
int
andstr
get swapped; then a couple lines nothing changed; and then it completely breaks down and becomesAny
: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.
@pkch
Union
is legal as a special case, see for example typing docs (emphasis my):Hmm, these two
Type[Union[int, str]]
andUnion[Type[int], Type[str]]
should be equivalent I think. Is it easy to fix?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.
Wow
reveal_type
changes from early to late in the same scope:Uh oh!
There was an error while loading. Please reload this page.
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.
@pkch
I think this should be fixed. Without this PR I get:
A type should not be "destroyed" after an
issubclass()
check.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.
@pkch
This time for sure :-)
Uh oh!
There was an error while loading. Please reload this page.
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.
Done!
Interesting, when I
git reset --hard @~
andgit push -f
, I effectively remove the last commit from this PR. Then AppVeyor decided to rebuild, while Travis decided not to. At first I thought that AppVeyor was smarter because it noticed that master branch changed so it figured the new merge with master will be different, and it's worth rebuilding.It turns out that AppVeyor rebuilds regardless of whether master changed since the last time it built the commit; in fact, it even rebuilt on my own branch in the forked repo.
So, when they already built for the commit that just became the head of the PR or branch:
Edit: Travis is perfectly reasonable, actually. Its green mark means that the branch merged into master without errors at some point in the past - not that it would do the same right this moment. And it maintains the same meaning in the situation I just described.
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.
@pkch
Something strange happened with typeshed. Could you please fix this?
(And don't forget to open an issue about
Type[Union[...]]
vsUnion[Type[...]]
.)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.
@ilevkivskyi what do you see happening? everything seems fine in my forked repo, when I merged this branch into master.
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.
@pkch
Hm, this is strange, GitHub shows me that your PR makes changes to typeshed, but I checked the commit hash, and it is the correct one. I think it is safe to merge.