-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
False positive: No overload variant of "asdict" matches argument type "Type[DataclassInstance]" [call-overload] #17550
Comments
I also encountered this on Friday so thank you for filing this. We are using python 3.11. mypy 1.10 was fine. Example action failure: https://github.com/lsst/daf_butler/actions/runs/10014302029/job/27683746998 |
I'm not sure it's a false positive. if is_dataclass(foo) and not isinstance(foo, type):
asdict(foo) |
Hmm, good point. The error message confused me because it said Thanks! |
I tried changing my code to: 137 if dataclasses.is_dataclass(inMemoryDataset) and not isinstance(inMemoryDataset, type):
138 inMemoryDataset = dataclasses.asdict(inMemoryDataset) but whilst the code does work fine mypy is now complaining about a different thing:
so it looks like mypy doesn't really understand that that |
The suggested fix on python/mypy#17550 causes another problem that we ignore for now.
The suggested fix on python/mypy#17550 causes another problem that we ignore for now.
@timj not sure what your new problem is without more context. For me the isinstance did the job. |
Sorry. The context was the same lines from my previous comment pointing at my repo. To be concrete: import dataclasses
from typing import Any
def to_dict(in_memory_dataset: Any) -> dict:
as_dict: dict[str, Any]
if dataclasses.is_dataclass(in_memory_dataset) and not isinstance(in_memory_dataset, type):
as_dict = dataclasses.asdict(in_memory_dataset)
elif hasattr(in_memory_dataset, "_asdict"):
as_dict = in_memory_dataset._asdict()
else:
as_dict = {}
return as_dict gives me an error:
Note that my mypy.ini has |
hmm, you're right. Here's a playground reproducing it: https://mypy-play.net/?mypy=latest&python=3.12&flags=warn-unreachable&gist=9453a339138852b03bf0d8f16286baf4 If I add a Maybe this is still an issue. |
bump. encountered it too after an update to mypy 1.11.0 in python 3.12.4. i very much appreciate the warning about the |
The suggested fix on python/mypy#17550 causes another problem that we ignore for now.
Thanks for the mypy playground. It shows that in mypy 1.11 the type from the
but the type from 1.10.1 is still The bug seems to be that after |
related: python/typeshed#12401 Looks like it really did show up in this version of mypy because of the overload changes. Also looks like it might be a problem without an easy fix, because it's not clear there's a better alternative than the current implementation. @timj your case can be fixed by changing the order of the checks from |
@brianmedigate, thanks. Changing the order of the checks does fix my unreachable statement problem. |
I believe the relevant change included in mypy 1.11 is python/typeshed#11929 |
I just came across this problem in my own code. Not sure what the most correct resolution is, but here is a complete implementation of
|
I ran into a slight variation and found a different workaround. I'm doing some (fairly sketchy) metaprogramming with import dataclasses
from typing import Any
def create_any_dataclass[T](ty: type[T]) -> T:
assert dataclasses.is_dataclass(ty)
params: dict[str, Any] = {}
for field in dataclasses.fields(ty):
params[field.name] = 'asdf' # something more sophisticated checking field.type
return ty(**params) But this workaround works because just import dataclasses
from typing import Any
def create_any_dataclass[T](ty: type[T]) -> T:
ty2 = ty
assert dataclasses.is_dataclass(ty2)
params: dict[str, Any] = {}
for field in dataclasses.fields(ty2):
params[field.name] = 'asdf' # something more sophisticated checking field.type
return ty(**params) |
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 toType[DataclassInstance]
, whereas in reality it should probably be something more likeType[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
Expected Behavior
No errors
Actual Behavior
Your Environment
The text was updated successfully, but these errors were encountered: