python / python/mypy

Inconsistent module import behavior when `follow_imports=skip` and __init__ files are omitted from check list

Open
#10,009 0 comments 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

Bug Report

Inconsistent module import behavior when follow_imports=skip and files are passed explicitly on the command line

To Reproduce

mypy.ini

[mypy]
follow_imports = skip

a.py

import mod.b
reveal_type(mod.b.UserId)
from mod.b import UserId
reveal_type(UserId)
import sib
reveal_type(sib.Sib)
from sib import Sib
reveal_type(Sib)

sib.py

class Sib:
    pass

mod/b.py

class UserId:
    pass

and a blank mod/__init__.py

nipunn-mbp:mypy_repro nipunn$ mypy a.py sib.py mod/b.py
a.py:2: note: Revealed type is 'Any'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'

nipunn-mbp:mypy_repro nipunn$ tree .
.
├── a.py
├── mod
│   ├── __init__.py
│   └── b.py
├── mypy.ini
└── sib.py
1 directory, 5 files

notably, you get the expected behavior when __init__.py is passed explicitly

nipunn-mbp:mypy_repro nipunn$ mypy .
a.py:2: note: Revealed type is 'def () -> mod.b.UserId'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'
nipunn-mbp:mypy_repro nipunn$ mypy a.py sib.py mod/b.py mod/__init__.py
a.py:2: note: Revealed type is 'def () -> mod.b.UserId'
a.py:4: note: Revealed type is 'def () -> mod.b.UserId'
a.py:7: note: Revealed type is 'def () -> sib.Sib'
a.py:9: note: Revealed type is 'def () -> sib.Sib'

Expected Behavior

It appears inconsistent that mypy would believe in mod.b.UserId while not believing in mod.b under these circumstances - giving different behavior for UserId depending on how it is imported. Even with follow_imports=skip - I would expect __init__.py files to be followed when a file within is explicitly declared.

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 by running the reproduction with mypy.ini, a.py, sib.py, mod/b.py, and mod/init.py, then compare the explicit file lists shown in the report. Trace how explicitly checked modules and package init.py files are handled with follow_imports=skip. Done means the revealed types are consistent whether mod/init.py is listed explicitly or not.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
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.