-
-
Notifications
You must be signed in to change notification settings - Fork 133
Closed
Description
- cattrs version: 1.1.1
- Python version: 3.8.6, mypy 0.790
- Operating System: macOS 10.15.7
Description
After upgrading from cattrs 1.0, mypy has started rejecting Union types as the type argument to Converter.register_structure_hook.
I think the docs at https://cattrs.readthedocs.io/en/latest/structuring.html#manual-disambiguation say that this is the supported way to structure a union type. It executes fine, but mypy rejects it.
What I Did
from typing import Any, ClassVar, List, Literal, Union, Type
import attr
import cattr
@attr.s(auto_attribs=True)
class Foo:
kind: ClassVar[Literal["foo"]] = "foo"
value: int
@attr.s(auto_attribs=True)
class Bar:
kind: ClassVar[Literal["bar"]] = "bar"
value: int
FooBar = Union[Foo, Bar]
def discriminate_foobar(value: Any, _klass: Type) -> FooBar:
kind, int_value = value["kind"], value["value"]
return Foo(int_value) if kind == "foo" else Bar(int_value)
cattr.register_structure_hook(FooBar, discriminate_foobar)
print(cattr.structure([{"kind": "foo", "value": 3}, {"kind": "bar", "value": 42}], List[FooBar]))$ python demo2.py
[Foo(value=3), Bar(value=42)]
$ mypy demo2.py
demo2.py:22: error: Argument 1 has incompatible type "object"; expected "Type[Any]"
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels