coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
284 stars 23 forks source link

Mypy doesn't recognize invalid Argument value #125

Open Ben621 opened 2 weeks ago

Ben621 commented 2 weeks ago

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

  1. add the code on file blah.py
  2. 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

coady commented 2 weeks ago

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.

Ben621 commented 2 weeks ago

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
coady commented 2 weeks ago

Try adding from functools import singledispatch.