-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Don't special-case class instances in binary expression inference #15161
Open
dcreager
wants to merge
14
commits into
main
Choose a base branch
from
dcreager/infer-binary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+555
−60
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c8c99f6
Regroup literal cases together
dcreager 58ada96
Add Todo and Never cases
dcreager 029932d
General Instance fallback
dcreager 3b1622f
Add a slew of tests
dcreager 33409ba
Linter
dcreager 9fb3013
Add mis-typed test
dcreager f4a65c2
More recursive class definitions cause panics
dcreager a5f0dad
Add test case for class union
dcreager 5239fa9
Add test case for class union < 3.10
dcreager a52486f
Special-case ClassLiteral | ClassLiteral
dcreager dffd23d
Merge branch 'main' into dcreager/infer-binary
dcreager fe73357
Revert "Special-case ClassLiteral | ClassLiteral"
dcreager a70fc31
Move subclass tests to be closer to similar class tests
dcreager 0acdbe4
Add reflected dunder test cases
dcreager 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
27 changes: 27 additions & 0 deletions
27
crates/red_knot_python_semantic/resources/mdtest/binary/classes.md
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Binary operations on classes | ||
|
||
## Union of two classes | ||
|
||
Unioning two classes via the `|` operator is only available in Python 3.10 and later. | ||
|
||
```toml | ||
[environment] | ||
python-version = "3.10" | ||
``` | ||
|
||
```py | ||
class A: ... | ||
class B: ... | ||
|
||
reveal_type(A | B) # revealed: UnionType | ||
``` | ||
|
||
## Union of two classes (prior to 3.10) | ||
|
||
```py | ||
class A: ... | ||
class B: ... | ||
|
||
# error: "Operator `|` is unsupported between objects of type `Literal[A]` and `Literal[B]`" | ||
reveal_type(A | B) # revealed: Unknown | ||
``` |
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 comes from the typeshed. We might consider special-casing the
BitOr
of twoClassLiteral
s to return a more specificType::Union
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.
(Note that this is not a type annotation, so none of the logic in
Type::in_type_expression
applies)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 went ahead and added this special case, but retaining the behavior that it's only available in Python ≥3.10
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.
Oh, I commented on this in my review; I think the added special case is not correct, the type from typeshed is :)
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.
👍 Sounds good! I wasn't sure about which way to go, so I added the special case in a separate commit that would be easier to revert