Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type invalid due to import, only on alternating runs #17202

Open
jvanasco opened this issue May 1, 2024 · 1 comment
Open

Type invalid due to import, only on alternating runs #17202

jvanasco opened this issue May 1, 2024 · 1 comment
Labels
bug mypy got something wrong

Comments

@jvanasco
Copy link

jvanasco commented May 1, 2024

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
@jvanasco jvanasco added the bug mypy got something wrong label May 1, 2024
@lukecivantos
Copy link

Something similar is happening to me. Every other time I run mypy it runs on my .venv even with exclude = ^\.venv/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants