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
Update after Erik's feedback #1
Update after Erik's feedback #1
Changes from 6 commits
f5205c6
00d5247
79720d5
4c4a0e9
4f90e9c
faa5768
f00e7a2
c895a0a
5305af4
b6335f0
f24ca7c
cbf907a
dea607f
e5a71ea
a32efa4
708f60c
662341f
24b88ba
a682219
6287be5
a2e0b22
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.
This is true even though
val
isint | str
(i.e. it doesn't includePosInt
)?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.
Yes that's true. This works because PosInt is an int.
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.
You should be able to try it yourself in the latest pylance.
Create a pyrightconfig.json with this in it:
Then the TypeGuard will behave as I described 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.
I believe that that's how Pyright works. I guess I'm wondering:
a) Is this correct?
b) What would be the behavior if
PosInt
was unrelated to any of the types in theval
union? Consider the following example. Within theif
block, isval
considered to bedatetime
becauseis_datetime
returnedtrue
? Or is itNever
because the intersection ofdatetime
andint | str
is empty? Oh, I guess this might be the multiple inheritance thing again. Maybe theval
passed intofunc
is derived from bothint
anddatetime
?c) Should the PEP call more attention to these issues?
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 believe the TypeGuard pep says that if the TypeGuard returns something nonsensical, that's the user's fault. It will just happily presume that's the actual type if the TypeGuard returns true.
This is exactly what Pyright does at the moment.
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.
It's mentioned here:
https://peps.python.org/pep-0647/#enforcing-strict-narrowing