Skip to content

Possible regression with attrs.asdict related to type guarding? #1185

@dougthor42

Description

@dougthor42

Possibly related issues:

Related PRs:

Summary

It looks like there's a regression with types guards and asdict from 21.4.0 to 23.1.0, and that they TypeGuard on attrs.has should be

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

Or attrs.asdict should accept inst: Type[AttrsInstance]

Steps to Reproduce:

In 21.4.0, this worked:

# attrs-issue.py
from typing import Any
import attrs

@attrs.frozen
class Foo:
    a: int

obj: Any = Foo(0)

if attrs.has(obj):
    print(attrs.asdict(obj))
$ python attrs-issue.py 
{'a': 0}
$ mypy attrs-issue.py 
Success: no issues found in 1 source file

In 23.1.0, that same file fails mypy:

$ pip install attrs==23.1.0
Looking in indexes: https://pypi.org/simple
Collecting attrs==23.1.0
  Using cached attrs-23.1.0-py3-none-any.whl (61 kB)
Installing collected packages: attrs
  Attempting uninstall: attrs
    Found existing installation: attrs 21.4.0
    Uninstalling attrs-21.4.0:
      Successfully uninstalled attrs-21.4.0
Successfully installed attrs-23.1.0
$ python attrs-issue.py 
{'a': 0}
$ mypy attrs-issue.py 
attrs-issue.py:11: error: Argument 1 to "asdict" has incompatible type "type[AttrsInstance]"; expected "AttrsInstance"  [arg-type]
attrs-issue.py:11: note: ClassVar protocol member AttrsInstance.__attrs_attrs__ can never be matched by a class object
Found 1 error in 1 file (checked 1 source file)

Other Info

  • Python version: 3.10.10
  • Mypy version: 1.5.1 (compiled: yes)
  • Operating system: Debian GNU/Linux rodete, 64-bit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions