mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False positive: No overload variant of "asdict" matches argument type "Type[DataclassInstance]" [call-overload]

Open brianmedigate opened this issue 7 months ago • 13 comments

Bug Report

Mypy 1.11.0 gives me a false positive that 1.10.1 didn't.

Looks like it thinks that is_dataclass() narrows to Type[DataclassInstance], whereas in reality it should probably be something more like Type[DataclassInstance] | DataclassInstance.

Not sure if this is a typeshed issue or a mypy issue, but it might be related to the recent overload changes.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.8&gist=a561e9787a7710733b09df23d2fc5c04

from dataclasses import dataclass, asdict, is_dataclass
from typing import Any


@dataclass
class Foo:
    a: int


foo: Any = Foo(a=1)

if is_dataclass(foo):
    asdict(foo)

Expected Behavior

No errors

Actual Behavior

main.py:13: error: No overload variant of "asdict" matches argument type "Type[DataclassInstance]"  [call-overload]
main.py:13: note: Possible overload variants:
main.py:13: note:     def asdict(obj: DataclassInstance) -> Dict[str, Any]
main.py:13: note:     def [_T] asdict(obj: DataclassInstance, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) -> _T
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.0
  • Python version used: 3.8.19

brianmedigate avatar Jul 21 '24 07:07 brianmedigate