python / python/mypy

Identical, anonymous intersection types causes incompatible import [assignment] error when imported via *

Open
#16,262 1 comment 1 reaction 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

Bug Report

If you import * from two modules that infer an identical intersection type, mypy will report an error along the lines of

Incompatible import of "<intersection>" (imported name has type "type[a.<intersection>]", local name has type "type[b.<intersection>]")  [assignment]

To Reproduce

This repro requires multiple files. They are delineated by comments.

# shared.py
class Foo: ...
class Bar: ...

# a.py
from shared import Foo, Bar
a_foos: list[Foo]
a_bars = [f for f in a_foos if isinstance(f, Bar)]

# b.py
from shared import Foo, Bar
b_foos: list[Foo]
b_bars = [f for f in b_foos if isinstance(f, Bar)]

# c.py
from a import *
from b import *

The offending mypy invocation is as follows:

$ mypy -m c

Expected Behavior

This should not emit an error because no runtime locals are actually assigned, or at the very least, because the intersections are identical.

Actual Behavior

c.py:2: error: Incompatible import of "<subclass of "Foo" and "Bar">" (imported name has type "type[b.<subclass of "Foo" and "Bar">]", local name has type "type[a.<subclass of "Foo" and "Bar">]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

This bug was reproduced in a fresh venv on Debian GNU/Linux 10 (buster):

$ python --version
Python 3.10.13
$ mypy --version
mypy 1.6.0 (compiled: yes)

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

Create the four files shown in the report: shared.py, a.py, b.py, and c.py, then run mypy -m c to reproduce the incompatible-import error. Trace star-import handling and the inferred anonymous intersection types; done means the identical intersections no longer produce an error while the repro still type-checks correctly.

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.