Skip to content

Type check error with asdict  #987

Closed
@dbertouille

Description

@dbertouille

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypingTyping/stub/Mypy/PyRight related bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions