python / python/mypy

Type invalid due to import, only on alternating runs

Open
#17,202 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

Bug Report

I think this is a bug, as I get inconsistent results across multiple invocations.

Depending on how I define a Type Alias, it can or can not be used as a valid type in another file - on every other invocation of mypy.

from typing import Literal
from typing import Optional
from typing import Union

TYPE_FOO = Optional[str]
TYPE_BAR = Union[str, None]
    
def biz(a: TYPE_FOO, b: TYPE_BAR) -> Literal[True]:
    return True

However, if the type aliases are split into another file, TYPE_FOO (using Optional) is sometimes invalid in mypy and triggers "Variable "TYPE_FOO" is not valid as a type [valid-type]" – while the functionally equivalent TYPE_BAR (using Union) is always valid. The error from Optional seems to appear on every-other invocation of mypy. I can't seem to recreate the error reliably with the small test case alone, but dropping it into larger projects seems to trigger it on every-other run.

To Reproduce

custom_types.py

from typing import Optional
from typing import Union

TYPE_FOO = Optional[str]
TYPE_BAR = Union[str, None]

project.py

from typing import Literal
from .custom_types import TYPE_FOO
from .custom_types import TYPE_BAR

def biz(a: TYPE_FOO, b: TYPE_BAR) -> Literal[True]:
    return True

Expected Behavior

I expected no errors for either usage.
I expected consistent behavior from mypy across runs when it thinks there is an issue.

Actual Behavior

MyPy alternates an error for the imported type that uses Optional on every-other run:

Variable "TYPE_FOO" is not valid as a type [valid-type]

i.e. The first run has no errors, the second has the error, the third has no errors, the fourth has the error, etc

The type using Union never triggers an error.

Your Environment

  • Mypy version used:
    1.10
  • Mypy command-line flags:
    NA
  • Mypy configuration options from mypy.ini (and other config files):
    check_untyped_defs = True
  • Python version used:
    3.10

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 mypy 1.10 on the two-file example in custom_types.py and project.py repeatedly with the stated Python 3.10 configuration. Compare runs where TYPE_FOO is accepted with runs producing the valid-type error, then broaden the reproduction to a larger project as described. Done means imported Optional-based aliases behave consistently and the false error no longer appears.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.