Skip to content

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

Closed as not planned
@mhooreman

Description

@mhooreman

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions