8000 `Callable` used as condition silently disables type checks · Issue #11164 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Callable used as condition silently disables type checks #11164

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

Closed
selimb opened this issue Sep 21, 2021 · 3 comments
Closed

Callable used as condition silently disables type checks #11164

selimb opened this issue Sep 21, 2021 · 3 comments
Labels
bug mypy got something wrong

Comments

@selimb
Copy link
selimb commented Sep 21, 2021

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
@selimb selimb added the bug mypy got something wrong label Sep 21, 2021
@sobolevn
Copy link
Member

Hi, @selimb! Thanks a lot for raising this!

Forgetting to actually call a callable is a common mistake. In the next version it will be supporting truthy-bool error code, see #10666

For now, you can use --warn-unreachable that kinda helps in this case as well:
Снимок экрана 2021-09-22 в 14 00 56

@selimb
Copy link
Author
selimb commented Sep 22, 2021

Thanks @sobolevn for the quick response!

Ah, I overlooked --warn-unreachable. I also see I'm far from the first one to submit a bug for this; looks like I wasn't using the right keywords XD. Apologies.

Is there any reason not to set --warn-unreachable with --strict?
If so, will the upcoming truthy-bool error be on by default or enabled with --strict?

@ilevkivskyi
Copy link
Member

truthy-function is now on by default so the original example is handled safely.

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

3 participants
0