Skip to content

Callable used as condition silently disables type checks #11164

Description

@selimb

Bug Report

If the condition of an if block is a Callable (or not Callable), the body of the else (or if) is not analyzed by mypy.

To Reproduce

Run mypy on:

def func() -> bool:
    return True

if not func:
    a: str = 4
    reveal_type(a)
else:
    b: str = 4
    reveal_type(b)

if func:
    c: str = 4
    reveal_type(c)
else:
    d: str = 4
    reveal_type(d)

Expected Behavior

Either:

test.py:5: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:6: note: Revealed type is "builtins.str"
test.py:8: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:9: note: Revealed type is "builtins.str"
test.py:12: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:13: note: Revealed type is "builtins.str"
test.py:15: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:16: note: Revealed type is "builtins.str"

or maybe statement-unreachable on lines 5 and 15?

Actual Behavior

test.py:8: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:9: note: Revealed type is "builtins.str"
test.py:12: error: Incompatible types in assignment (expression has type "int", variable has type "str")
test.py:13: note: Revealed type is "builtins.str"

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: Just mypy test.py.
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.3
  • Operating system and version: Windows 10 Pro 21H1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions