Skip to content

Commit 38205da

Browse files
committed
Flesh out type annotations
Closes #281
1 parent b1a681a commit 38205da

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

HISTORY.rst

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ History
1616
* Fix ``typing.Set`` applying too broadly when used with the ``GenConverter.unstruct_collection_overrides`` parameter on Python versions below 3.9. Switch to ``typing.AbstractSet`` on those versions to restore the old behavior.
1717
(`#264 <https://github.com/python-attrs/cattrs/issues/264>`_)
1818
* Uncap the required Python version, to avoid problems detailed in https://iscinumpy.dev/post/bound-version-constraints/#pinning-the-python-version-is-special (`#275 <https://github.com/python-attrs/cattrs/issues/275>`_)
19+
* Fix `Converter.register_structure_hook_factory` and `cattrs.gen.make_dict_unstructure_fn` type annotations.
20+
(`#281 <https://github.com/python-attrs/cattrs/issues/281>`_)
1921

2022
22.1.0 (2022-04-03)
2123
-------------------

src/cattrs/converters.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def register_structure_hook_func(
264264
def register_structure_hook_factory(
265265
self,
266266
predicate: Callable[[Any], bool],
267-
factory: Callable[[Any], Callable[[Any], Any]],
267+
factory: Callable[[Any], Callable[[Any, Any], Any]],
268268
) -> None:
269269
"""
270270
Register a hook factory for a given predicate.
@@ -750,7 +750,9 @@ def gen_structure_annotated(self, type):
750750
h = self._structure_func.dispatch(origin)
751751
return h
752752

753-
def gen_unstructure_attrs_fromdict(self, cl: Type[T]) -> Dict[str, Any]:
753+
def gen_unstructure_attrs_fromdict(
754+
self, cl: Type[T]
755+
) -> Callable[[T], Dict[str, Any]]:
754756
origin = get_origin(cl)
755757
attribs = fields(origin or cl)
756758
if attrs_has(cl) and any(isinstance(a.type, str) for a in attribs):
@@ -767,7 +769,9 @@ def gen_unstructure_attrs_fromdict(self, cl: Type[T]) -> Dict[str, Any]:
767769
)
768770
return h
769771

770-
def gen_structure_attrs_fromdict(self, cl: Type[T]) -> T:
772+
def gen_structure_attrs_fromdict(
773+
self, cl: Type[T]
774+
) -> Callable[[Mapping[str, Any], Any], T]:
771775
attribs = fields(get_origin(cl) if is_generic(cl) else cl)
772776
if attrs_has(cl) and any(isinstance(a.type, str) for a in attribs):
773777
# PEP 563 annotations - need to be resolved.

src/cattrs/gen.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def override(omit_if_default=None, rename=None, omit: bool = False):
4646

4747

4848
def make_dict_unstructure_fn(
49-
cl,
50-
converter,
49+
cl: Type[T],
50+
converter: "BaseConverter",
5151
_cattrs_omit_if_default: bool = False,
5252
_cattrs_use_linecache: bool = True,
5353
**kwargs,
54-
) -> Callable[[Any], Dict]:
54+
) -> Callable[[T], Dict[str, Any]]:
5555
"""
5656
Generate a specialized dict unstructuring function for an attrs class or a
5757
dataclass.

0 commit comments

Comments
 (0)