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.
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
PEP 724: Stricter TypeGuard #3266
PEP 724: Stricter TypeGuard #3266
Changes from 28 commits
f5205c6
9da9233
00d5247
79720d5
4c4a0e9
4f90e9c
faa5768
f00e7a2
c895a0a
5305af4
b6335f0
f24ca7c
cbf907a
dea607f
e5a71ea
a32efa4
708f60c
662341f
24b88ba
a682219
6287be5
a2e0b22
66bdf14
5850d66
31fb9c7
641b761
1ec2207
ce63130
6184c24
00e028c
7ab28d9
dddc7cb
7e51159
68e73a3
a9b0407
106f10a
e44d060
07af8f7
81bc56c
535ab81
7b69948
ddc4ec3
dd4bf80
5eab931
c5449b8
6ff5be9
487a47e
4f9753a
3c37ec3
27bac54
b90df2c
3263b63
dcf3d49
598d0da
a580f4a
95d4616
b267456
6b246f2
0e24a6f
457ea0e
5ff0408
b91a5ca
d8ea2cc
671df1f
d3048de
4a7039e
e3fd558
bccb5fb
e5f4749
25fc119
44a31ae
93b1503
499639f
b181ef9
5963e45
867c541
2a2336a
dd5fdee
92768e2
159c519
8f28e7a
678ce5f
f817e07
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Doesn't type checker behavior change on some code? From previous GH discussion it seems that we generally think it doesn't matter in any realistic use cases, but the PEP should make an explicit argument for why it's OK to break compatibility with the previously specified behavior.
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.
Hmm, not sure how to word that at the moment. Existing code should continue to behave the same way it does now, but yes type checkers will have different behavior. Which might cause more problems if people assume type checkers are always right. Guido's example comes to mind: python/typing#996 (comment)
Perhaps this should say something like:
`Type checkers will now be making more assumptions about the results of TypeGuard function. This can potentially cause problems like in this example:
def is_positive_int(a: int|str) -> TypeGuard[int]:
return isinstance(a, int) and a >= 0
However, there's no practical reason why anybody would assume the negative case in this situation wasn't an int. Users can still create TypeGuards that are invalid, but in the majority case, the TypeGuard will now behave in a more consistent manor.`
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.
I'm not sure I follow completely. This PEP's only proposed change is to type checker behavior, so that's the backward compatibility we're concerned about.
Somebody who used a TypeGuard like in Guido's linked example is following the PEP 647 spec. With this PEP's proposed change, type checkers might not work correctly on their codebase any more:
With PEP 647 semantics, type checkers would catch the mistake in the else block. With this PEP's semantics, they would not.
Now, you can make the argument that such code is contrived and unlikely to appear in practice, as @erictraut did in python/typing#996 (comment).
But the PEP needs to acknowledge that it does break compatibility, and discuss why that is OK and how we can mitigate the possible harm. (For example, could type checkers temporarily raise a warning on encountering a TypeGuard that will now behave differently?)
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.
I don't think type checkers should be trying to check for all possible scenarios where this might happen. As in PEP 647, people can make invalid TypeGuards.
In Guido's example, pyright at least couldn't tell that something might happen. It doesn't understand the logic of the TypeGuard.
I think there is no mitigation other than people that write TypeGuards being smart enough to know when they're doing something that a typechecker can't possibly catch.