python / python/mypy

join is not symmetric for type[X] vs class object X

Open
#19,551 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.