-
-
Notifications
You must be signed in to change notification settings - Fork 400
Closed
Description
Possibly related issues:
- Use of
AttrsInstance
protocol is incompatible with type checkers other than mypy #996 - attr.asdict -> attrs.asdict regression for sets of instances #1180
Related PRs:
- Use
TypeGuard
forhas
in Python 3.10 and above #997 - Add AttrsInstance protocol to forgotten attrs.asdict and attrs.astuple #1090
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
Labels
No labels