-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Implement TypeIs (PEP 742) #16898
Merged
Merged
Implement TypeIs (PEP 742) #16898
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
8a30073
Basic work on TypeNarrower
JelleZijlstra 58e8403
Initial tests (many failing)
JelleZijlstra c8d2af8
Fewer test failures
JelleZijlstra f205910
Fix the remaining tests
JelleZijlstra 75c9dec
Did not actually need TypeNarrowerType
JelleZijlstra 4666486
Error for bad narrowing
JelleZijlstra 25a9c79
temp change typeshed
JelleZijlstra faa4a07
Fixes
JelleZijlstra c0e0210
Merge branch 'master' into typenarrower
JelleZijlstra f107e5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 34700bb
doc
JelleZijlstra 065ec92
fix self check
JelleZijlstra aef3036
like this maybe
JelleZijlstra 4b19c77
Merge remote-tracking branch 'upstream/master' into typenarrower
JelleZijlstra 6b0e749
Fix and add tests
JelleZijlstra c9e53e6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 909e53c
Use TypeIs
JelleZijlstra eb88371
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1b1e368
Apply suggestions from code review
JelleZijlstra 84c69d2
Code review feedback, new test case, fix incorrect constraints
JelleZijlstra ae294bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7fedbcf
Merge remote-tracking branch 'upstream/master' into typenarrower
JelleZijlstra dbc229d
Rename error code
JelleZijlstra 8b2fb0b
Quote name
JelleZijlstra 816fd1a
unxfail
JelleZijlstra d6fcc35
add elif test
JelleZijlstra ef825ce
type context test
JelleZijlstra d32956d
Add test
JelleZijlstra a36a16a
Add error code test case
JelleZijlstra b32ba80
update docs
JelleZijlstra 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 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 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 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 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 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 |
---|---|---|
|
@@ -1018,10 +1018,22 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]: | |
param_spec = template.param_spec() | ||
|
||
template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type | ||
if template.type_guard is not None: | ||
if template.type_guard is not None and cactual.type_guard is not None: | ||
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. Nice, thanks! I think this should fix a thing I ran into while testing #16939 |
||
template_ret_type = template.type_guard | ||
if cactual.type_guard is not None: | ||
cactual_ret_type = cactual.type_guard | ||
elif template.type_guard is not None: | ||
template_ret_type = AnyType(TypeOfAny.special_form) | ||
elif cactual.type_guard is not None: | ||
cactual_ret_type = AnyType(TypeOfAny.special_form) | ||
|
||
if template.type_is is not None and cactual.type_is is not None: | ||
template_ret_type = template.type_is | ||
cactual_ret_type = cactual.type_is | ||
elif template.type_is is not None: | ||
template_ret_type = AnyType(TypeOfAny.special_form) | ||
elif cactual.type_is is not None: | ||
cactual_ret_type = AnyType(TypeOfAny.special_form) | ||
|
||
res.extend(infer_constraints(template_ret_type, cactual_ret_type, self.direction)) | ||
|
||
if param_spec is None: | ||
|
This file contains 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 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 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 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 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 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 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
This is the crucial part of the change, which implements the new type narrowing behavior. It's the same as
isinstance()
, a little way up in the same method.