Skip to content

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

Closed

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

Activity

  1. JelleZijlstra commented on Mar 9, 2024

    @JelleZijlstra
    Member

    Probably related to python/typeshed#11223. I suspect some of these need to be updated:

    % git grep builtins.ellipsis
    mypy/checkexpr.py:        return self.named_type("builtins.ellipsis")
    mypy/stubtest.py:                and proper_type.type.fullname == "builtins.ellipsis"
    mypy/types.py:            or self.type.fullname == "builtins.ellipsis"
    mypy/typeshed/stdlib/marshal.pyi:    | builtins.ellipsis
    test-data/unit/check-unions.test:        reveal_type(x) # N: Revealed type is "builtins.ellipsis"
    test-data/unit/typexport-basic.test:EllipsisExpr(2) : builtins.ellipsis
    
  2. added a commit that references this issue on Mar 9, 2024
    63d491a
  3. added a commit that references this issue on Mar 10, 2024
    16abf5c
  4. hauntsaninja commented on Mar 10, 2024

    @hauntsaninja
    Collaborator

    @dhirving Thank you for the issue. I'll add https://github.com/lsst/daf_butler to mypy_primer so we detect regressions against it

  5. dhirving commented on Mar 11, 2024

    @dhirving
    Author

    Thanks for the prompt fix! I replied to the ticket on mypy_primer as well -- we'd love to be included in the regression suite.

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions