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

False Positive: MyPy fails to infer type of logging handlers in "a = x if c else y" construct #13652

Open
bennyrowland opened this issue Sep 12, 2022 · 0 comments
Labels
bug mypy got something wrong topic-ternary-expression a if b else c

Comments

@bennyrowland
Copy link

To Reproduce

mypy -c "import logging; b=False; a:logging.Handler = logging.StreamHandler() if b else logging.FileHandler('test.log')"

Expected Behavior

Should accept that a is a logging.Handler

Actual Behavior

<string>:1: error: Incompatible types in assignment (expression has type "object", variable has type "Handler")

Similar examples that do work

Hard-coding the condition

mypy -c "import logging; a:logging.Handler = logging.StreamHandler() if False else logging.FileHandler('test.log')"
Success: no issues found in 1 source file

Using integers instead of logging.Handlers

mypy -c "b=False; a:int = 0 if b else 1"                                                                                 
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 0.971 (compiled: no)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.13
  • Operating system and version: Windows 10
@bennyrowland bennyrowland added the bug mypy got something wrong label Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-ternary-expression a if b else c
Projects
None yet
Development

No branches or pull requests

2 participants