Skip to content
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

Type check error with asdict #987

Closed
dbertouille opened this issue Jul 28, 2022 · 6 comments
Closed

Type check error with asdict #987

dbertouille opened this issue Jul 28, 2022 · 6 comments
Labels
Typing Typing/stub/Mypy/PyRight related bugs.

Comments

@dbertouille
Copy link

Starting with version attrs 22.1.0, I started running into a type checking issue when running mypy. In particular, this occurs when calling asdict in parent class where that parent class does not use attrs but the child might.

This is a bit of an odd scenario so I'm not sure if the bug belongs here in attrs, mypy, or should just be an error. However, it did not produce any type checking errors using attrs 21.4.0.

import abc
import attr
from typing import Any, Dict


class BaseModel(abc.ABC):
    def to_dict(self) -> Dict[str, Any]:
        o = {}  # type: Dict[str, Any]
        if attr.has(type(self)):
            return attr.asdict(self)
        return vars(self).copy()


@attr.s(auto_attribs=True)
class Model(BaseModel):
    id: int = 0


model = Model(id=1)

if attr.has(type(model)):
    data = attr.asdict(model)
@hynek
Copy link
Member

hynek commented Jul 29, 2022

Given that your asdict is protected by a has, I suspect that belongs into mypy because it's dynamic logic we can't express in attrs. But @Tinche might know more.

The problem is that we introduced an AttrsInstance Protocol. It looks like we forgot to add a newsfragment.

@hynek hynek added the Typing Typing/stub/Mypy/PyRight related bugs. label Jul 29, 2022
@1oglop1
Copy link

1oglop1 commented Aug 5, 2022

@hynek I notice that AttrsInstance is not imported in attrs.__init__.pyi only defined in attr.__init__.pyi is that intentional?

@hynek
Copy link
Member

hynek commented Aug 5, 2022

@Tinche did we just forget it?

@Tinche
Copy link
Member

Tinche commented Aug 5, 2022

Yeah I think so ;/

@layday
Copy link
Contributor

layday commented Aug 8, 2022

Probably worth nothing that has is a perfect match for TypeGuard from Python 3.10 (or its backport from typing-extensions). In attrs/__init__.pyi:

def has(cls: type) -> TypeGuard[type[AttrsInstance]]: ...

Elsewhere:

[...]
        if attr.has(type(self)):
            # `self` is now an `AttrsInstance`
            return attr.asdict(self)  # Passes!

But I suspect that attrs would not want to depend on typing-extensions.

@hynek
Copy link
Member

hynek commented Apr 3, 2023

fixed by #997

@hynek hynek closed this as completed Apr 3, 2023
shcheklein added a commit to iterative/dvc that referenced this issue Mar 15, 2024
fixes AttrsInstance import failing, fixed by python-attrs/attrs#987
skshetry pushed a commit to iterative/dvc that referenced this issue Mar 16, 2024
fixes AttrsInstance import failing, fixed by python-attrs/attrs#987
BradyJ27 pushed a commit to BradyJ27/dvc that referenced this issue Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing Typing/stub/Mypy/PyRight related bugs.
Projects
None yet
Development

No branches or pull requests

5 participants