join is not symmetric for type[X] vs class object X
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
In theory, join(a, b) should always give the same result as join(b, a) (permutation invariance). However, this is not always the case. One example I found is the following (related to the testAbstractTypeInADict unit test)
https://mypy-play.net/?mypy=1.17.0&python=3.12&gist=e4df640a72c4f61c1a33a50db60fd6e5
class Class: ...
def join[T](x: T, y: T) -> T: ...
klass: type[Class]
reveal_type(klass) # N: Revealed type is "type[__main__.Class]"
reveal_type(Class) # N: "def () -> __main__.Class"
reveal_type(join(klass, Class)) # N: Revealed type is "builtins.type"
reveal_type(join(Class, klass)) # N: Revealed type is "builtins.object"
Moreover, when debugging this, mypy actually evaluates both is_subtype(left, right) and is_subtype(right, left) to true.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the mypy-play reproduction and the related testAbstractTypeInADict unit test. Trace the join calls involving type[Class] and the class object, then inspect the is_subtype checks noted in the report. Done means join produces the same result for both argument orders in this case, with the relevant test passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100