Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Closed
Description
Bug Report
mypy failed to narrow down union types includingSelf withisinstance.
To Reproduce
Consider the following code:
fromdataclassesimportdataclassfromtypingimportSelf,reveal_type@dataclassclassFoo:i:intdefbar(self,other:Self|int)->int:ifisinstance(other,Foo):returnself.i*other.ielse:reveal_type(other)# "int" expected, gets "Self | int"returnself.i*other# Unsupported operand types for * ("int" and "Self") [operator]mypy(error)
Expected Behavior
Other should be infered as anint and mypy reports no error.
Actual Behavior
mypy failed to narrow type ofother
Revealed type is "Union[Self`0, builtins.int]"mypy(note)Also, mypy reports the following error.
Unsupported operand types for * ("int" and "Self") [operator]mypy(error)Your Environment
- Mypy version used: 1.1.1
- Mypy command-line flags:
--follow-imports=silent --ignore-missing-imports --show-column-numbers --no-pretty(VSCode defaults) - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.1