Skip to content

Type narrowing for ellipsis via 'is ...' failing in 1.9.0 #17002

Closed
@dhirving

Description

@dhirving

Bug Report

As of mypy 1.9.0, narrowing of unions containing EllipsisType via the expressions if x is ... or if x is Ellipsis no longer works. In both branches of the if statement, the type of x still contains EllipsisType instead of being narrowed.

This narrowed as expected in 1.8.0.

As a workaround, it appears that if instanceof(x, EllipsisType) still works as expected.

To Reproduce

Run mypy against the following snippet:

from types import EllipsisType

x: int | EllipsisType

if x is ...:
    pass
else:
    y = x + 1

A more complicated reproduction of this issue can be seen in our repo here: https://github.com/lsst/daf_butler/actions/runs/8209898890/job/22456322445?pr=973

Expected Behavior

mypy 1.8.0 shows no errors because x was narrowed to int in the else clause:

Success: no issues found in 1 source file

Actual Behavior
mypy 1.9.0 reports an error instead:

tmp.py:8: error: Unsupported operand types for + ("EllipsisType" and "int")  [operator]
tmp.py:8: note: Left operand is of type "int | EllipsisType"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions