Skip to content

Preliminary test on type not detected, leading to false error #19124

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

Open
mhooreman opened this issue May 21, 2025 · 1 comment
Open

Preliminary test on type not detected, leading to false error #19124

mhooreman opened this issue May 21, 2025 · 1 comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@mhooreman
Copy link

mhooreman commented May 21, 2025

Bug Report

Consider the following code:

def f(a: bool, x: int | None) -> None:
    if a:
        if x is None:
            raise ValueError

    if a:
        g(x)
    h()

def g(x: int) -> None:
    pass

def h() -> None:
    pass

It is clear that, when g(x) is called, x is an int, because None is already excluded.

Unfortunately, we have the following error:

demo.py: note: In function "f":
demo.py:7:11:7:11: error: Argument 1 to "g" has incompatible type "int | None"; expected "int"  [arg-type]
            g(x)
              ^
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line : None (execution via uv run)
  • Mypy configuration options from pyproject.toml :
[tool.mypy]
strict = true
show_error_context = true
show_column_numbers = true
show_error_end = true
show_error_code_links = true
pretty = true
mypy_path = "$MYPY_CONFIG_FILE_DIR/src"
packages = ['qbdseroconv', ]
  • Python version used: 3.12.3
@mhooreman mhooreman added the bug mypy got something wrong label May 21, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented May 21, 2025

Mypy doesn't associate types with other types, so this isn't something that is possible right now.

There's probably some better formulation of this already, but I can't remember it so I won't close this.

@A5rocks A5rocks added feature topic-type-narrowing Conditional type narrowing / binder and removed bug mypy got something wrong labels May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

2 participants