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

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

Closed
dhirving opened this issue Mar 8, 2024 · 3 comments · Fixed by #17003
Closed

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

dhirving opened this issue Mar 8, 2024 · 3 comments · Fixed by #17003
Labels
bug mypy got something wrong

Comments

@dhirving
Copy link

dhirving commented Mar 8, 2024

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
@dhirving dhirving added the bug mypy got something wrong label Mar 8, 2024
@JelleZijlstra
Copy link
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

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 10, 2024

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

@dhirving
Copy link
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
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants