python / python/mypy

valid-type not reported for variables imported in other modules

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

Nobody has claimed this yet.

bug topic-error-reporting
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy reports [valid-type] for a variable defined in the same file but not when the variable is imported in other files.

To Reproduce

Create 3 files

# app/example_a.py

def dynamic_type(x):
    return type("some_type", () , x)

# app/example_b.py

from app.example_a import dynamic_type

SOME_FAKE_TYPE = dynamic_type({"a": "a"})

x: SOME_FAKE_TYPE

# app/example_c.py
from app.example_b import SOME_FAKE_TYPE


x: SOME_FAKE_TYPE

Expected Behavior

Mypy should raise [valid-type] errors in both app/example_b.py and app/example_c.py

Actual Behavior

It only reports [valid-type] errors in app/example_b.py

app/example_b.py:6: error: Variable "app.example_b.SOME_FAKE_TYPE" is not valid as a type  [valid-type]
app/example_b.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases

Your Environment

$ uv run mypy --version
mypy 1.16.0 (compiled: yes
  • Mypy version used: 1.16.0
  • Mypy command-line flags: mypy
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
check_untyped_defs = True
follow_imports = normal
disallow_untyped_defs = False
files = app/
  • Python version used: 3.11.8

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 reproducing the report with app/example_a.py, app/example_b.py, and app/example_c.py using mypy 1.16.0, then compare the [valid-type] diagnostics for the local and imported variables. The fix is complete when mypy reports the expected error in both example_b.py and example_c.py without changing the reproduction’s intent.

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.