Skip to content

Commit d5e455a

Browse files
Fix include_subclass union_strategy typing (#431)
* Fix include_subclass union_strategy typing Using TypeVar instead of always BaseConverter as one of the arguments to union_strategy allows more strategies to be passed. For example, if one requires a Converter for their strategy, this changes allows that strategy to be based to include_subclasses. * Relax Converter type on configure_tagged_union * Fix lint --------- Co-authored-by: Tin Tvrtković <tinchester@gmail.com>
1 parent 946bb10 commit d5e455a

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python
5151
- The documentation has been significantly reworked.
5252
([#473](https://github.com/python-attrs/cattrs/pull/473))
5353
- The docs now use the Inter font.
54+
- Make type annotations for `include_subclasses` and `tagged_union` strategies more lenient.
55+
([#431](https://github.com/python-attrs/cattrs/pull/431))
5456

5557
## 23.2.3 (2023-11-30)
5658

src/cattrs/strategies/_subclasses.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from __future__ import annotations
33

44
from gc import collect
5-
from typing import Any, Callable, Union
5+
from typing import Any, Callable, TypeVar, Union
66

7-
from ..converters import BaseConverter, Converter
7+
from ..converters import BaseConverter
88
from ..gen import AttributeOverride, make_dict_structure_fn, make_dict_unstructure_fn
99
from ..gen._consts import already_generating
1010

@@ -28,11 +28,14 @@ def _get_union_type(cl: type, given_subclasses_tree: tuple[type]) -> type | None
2828
return Union[class_tree] if len(class_tree) >= 2 else None
2929

3030

31+
C = TypeVar("C", bound=BaseConverter)
32+
33+
3134
def include_subclasses(
3235
cl: type,
33-
converter: Converter,
36+
converter: C,
3437
subclasses: tuple[type, ...] | None = None,
35-
union_strategy: Callable[[Any, BaseConverter], Any] | None = None,
38+
union_strategy: Callable[[Any, C], Any] | None = None,
3639
overrides: dict[str, AttributeOverride] | None = None,
3740
) -> None:
3841
"""
@@ -79,7 +82,7 @@ def include_subclasses(
7982

8083
def _include_subclasses_without_union_strategy(
8184
cl,
82-
converter: Converter,
85+
converter: BaseConverter,
8386
parent_subclass_tree: tuple[type],
8487
overrides: dict[str, AttributeOverride] | None,
8588
):
@@ -153,9 +156,9 @@ def unstruct_hook(
153156

154157

155158
def _include_subclasses_with_union_strategy(
156-
converter: Converter,
159+
converter: C,
157160
union_classes: tuple[type, ...],
158-
union_strategy: Callable[[Any, BaseConverter], Any],
161+
union_strategy: Callable[[Any, C], Any],
159162
overrides: dict[str, AttributeOverride] | None,
160163
):
161164
"""

src/cattrs/strategies/_unions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from attrs import NOTHING
55

6-
from cattrs import BaseConverter, Converter
6+
from cattrs import BaseConverter
77
from cattrs._compat import get_newtype_base, is_literal, is_subclass, is_union_type
88

99
__all__ = [
@@ -20,7 +20,7 @@ def default_tag_generator(typ: Type) -> str:
2020

2121
def configure_tagged_union(
2222
union: Any,
23-
converter: Converter,
23+
converter: BaseConverter,
2424
tag_generator: Callable[[Type], str] = default_tag_generator,
2525
tag_name: str = "_type",
2626
default: Optional[Type] = NOTHING,

0 commit comments

Comments
 (0)