-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Extend tuple __len__ and __bool__ special casing to also cover tuple subclasses
#19289
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
46943b4
Add failing tests
AlexWaygood 9735929
make the tests pass!
AlexWaygood 2756b11
do the same for `__bool__`
AlexWaygood e6ddfa5
cleanup
AlexWaygood 6afb514
Add more tests and fix things for mixed tuples
AlexWaygood 856a437
Unify with existing truthiness calculations for tuples
AlexWaygood aa5a47c
pre-commit
AlexWaygood 5eb8dd0
Update crates/ty_python_semantic/resources/mdtest/expression/boolean.md
AlexWaygood 6451518
Update crates/ty_python_semantic/resources/mdtest/expression/len.md
AlexWaygood a92ea02
clearer names in tests
AlexWaygood 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
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
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
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.
Should this not also be a Liskov violation?
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.
Okay i guess its not a Liskov violation, but should we not complain at this?
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.
Ah sorry I retract this, my bad.
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.
Okay changed my mind slightly
In rust if you have a
lenfunction clippy tells you to have anis_emptyfunction and auto generates toself.len() == 0.This maybe isn't relevant, but it seems like we shouldn't allow override of just
__bool__. I'm not sure where else to go with this to be honest.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.
No, I think that's a great point! It seems like in general, as part of our Liskov implementation, we should probably enforce that
__bool__and__len__are never overridden to be inconsistent with each other on anySequencesubtype. I think otherwise this could indeed cause us to make incorrect assumptions, not just regarding tuplesThere 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.
Cool thanks, so in this case we could emit a diagnostic saying that there should be a ’len’ method that returns ’int & ~Literal[0]’ (or something like that).
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 about that specifically -- I guess I'm not sure we should emit an error on this specific tuple subclass. It seems a bit pedantic -- just because the
__len__method is annotated as returningintdoesn't mean that the class is violating the contract ofSequences. It just means that we haven't been given enough information to verify; I think it's probably better to steer clear of false positives in that situation and assume the user knows what they're doing. It's not the goal of ty to catch every possible error that a user could make.But I do think it's worth emitting errors on these classes, because here we do have enough information to say that
__len__and__bool__are inconsistent with each other:Even here, though, it's questionable whether this is a typing error or more something that's in the domain of a type-aware linter to check. (That doesn't mean that it wouldn't be a useful rule -- I think it would! But it might not be in the core purview of ty right now -- it might be more something for the future, when we start to use ty to power type-aware lint rules in Ruff.)
So I guess I feel like you're raising a great point in general, but that for these specific cases we probably shouldn't emit errors, and even if we should it maybe shouldn't be ty itself that emits these errors but maybe a type-aware linter built on top of ty :-)