A 'type only' module import should not count as importing a module
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
given:
my-app/
├─ package/
│ ├─ __init__.py
│ ├─ things.py
├─ entry.py
├─ mod.py
entry.py:
import mod
import package
mod.func(package.things.Thing())
mod.py:
def func(it):
...
In typeshed/bundled stubs: mod.pyi
from package.things import Thing
def func(it: Thing) -> None: ...
OR, if this is fake imported in the .py module: mod.py
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING
# do a type import so doesn't cause circular imports or slow down loading
from package.things import Thing
def func(it: Thing) -> None:
...
package.things.py
class Thing:
...
When mypy --strict entry.py is executed, no error is generated.
When python entry.py is run: AttributeError: module 'package' has no attribute 'things'
Mypy treats the 'type import' as importing package.things, when in reality it isn't. I understand this is consistent with what TYPE_CHECKING should do, but seems really sus if you ask me.
This more seriously affects typeshed, where all imports are fake and mypy will think all fakely imported modules are really imported.
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
Reproduce the behavior with entry.py, mod.py, package/init.py, and package/things.py using the provided mypy --strict and Python commands. Start by tracing how the type-only import in mod.pyi or the TYPE_CHECKING block is treated, then add a regression case showing that it must not make package.things available at runtime.
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
- Clearly specified
- Newbie friendliness
- 35/100