Mypy doesn't recognize invalid Argument value
Describe the bug When i run mypy on my code i don't get error only success
Success: no issues found in 1 source file
To Reproduce
- add the code on file
blah.py - run
mypy:mypy blah.py
output:
$ mypy blah.py
blah.py:30: note: Revealed type is "Any"
Success: no issues found in 1 source file
from typing import reveal_type
from multimethod import multimethod
@multimethod
def func(value: int | list [int]) -> None:
raise NotImplemented
@func.register
def _(value: list[int]) -> list[int]:
return list(map(lambda x: x +1, value))
@func.register
def _(value: int) -> int:
return int(func([value])[0])
if __name__ == "__main__":
print(func(1))
print(func([3]))
print(func("3"))
print(func(["ff"]))
reveal_type(func(["ff"]))
note:
func(["ff"]) and func("3") are invalid value
Expected behavior expected to get
error: Argument 1 to "func" has incompatible type "str"; expected " int | list [int]"
Version
- python: 3.11.6
- mypy: 1.11.2 (compiled: yes)
- multimethod: 1.12
This has come up before; I think it's a mypy limitation. AFAICT, static checkers special-case symbols like overload and singledispatch. For example, replacing
singledispatch = multimethod
@singledispatch
...
gives your expected error.
i still dont't Get ERROR. i get SUCCESS
from typing import reveal_type
from multimethod import multimethod
singledispatch = multimethod
@singledispatch
def func(value: int | list [int]) -> None:
raise NotImplemented
@func.register
def _(value: list[int]) -> list[int]:
return list(map(lambda x: x +1, value))
@func.register
def _(value: int) -> int:
return int(func([value])[0])
if __name__ == "__main__":
print(func(1))
print(func([3]))
print(func("3"))
print(func(["ff"]))
reveal_type(func(["ff"]))
mypy blah.py
Success: no issues found in 1 source file
Try adding from functools import singledispatch.
that now working