Skip to content

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

Description

@bennyrowland

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions