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.
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
Activity
JelleZijlstra commented on Mar 9, 2024
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.ellipsisFix type narrowing for types.EllipsisType
Fix type narrowing for types.EllipsisType (#17003)
hauntsaninja commented on 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 commented on Mar 11, 2024
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.
Add mypy GitHub Action workflow
Dogfood the just-built image for mypy
Dogfood the just-built image for mypy
Pin mypy below 2.0
Pin mypy below 2.0
Bake CI dev tools (mypy, pytest, stubs, xdist) into the image
Bake CI dev tools (mypy, pytest, stubs, xdist) into the image