diff --git a/HISTORY.rst b/HISTORY.rst index e20be42d..3480ac25 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,6 +16,8 @@ History * 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. (`#264 `_) * Uncap the required Python version, to avoid problems detailed in https://iscinumpy.dev/post/bound-version-constraints/#pinning-the-python-version-is-special (`#275 `_) +* Fix `Converter.register_structure_hook_factory` and `cattrs.gen.make_dict_unstructure_fn` type annotations. + (`#281 `_) 22.1.0 (2022-04-03) ------------------- diff --git a/src/cattrs/converters.py b/src/cattrs/converters.py index 0b1bf94f..6dd4d3e9 100644 --- a/src/cattrs/converters.py +++ b/src/cattrs/converters.py @@ -264,7 +264,7 @@ def register_structure_hook_func( def register_structure_hook_factory( self, predicate: Callable[[Any], bool], - factory: Callable[[Any], Callable[[Any], Any]], + factory: Callable[[Any], Callable[[Any, Any], Any]], ) -> None: """ Register a hook factory for a given predicate. @@ -750,7 +750,9 @@ def gen_structure_annotated(self, type): h = self._structure_func.dispatch(origin) return h - def gen_unstructure_attrs_fromdict(self, cl: Type[T]) -> Dict[str, Any]: + def gen_unstructure_attrs_fromdict( + self, cl: Type[T] + ) -> Callable[[T], Dict[str, Any]]: origin = get_origin(cl) attribs = fields(origin or cl) 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]: ) return h - def gen_structure_attrs_fromdict(self, cl: Type[T]) -> T: + def gen_structure_attrs_fromdict( + self, cl: Type[T] + ) -> Callable[[Mapping[str, Any], Any], T]: attribs = fields(get_origin(cl) if is_generic(cl) else cl) if attrs_has(cl) and any(isinstance(a.type, str) for a in attribs): # PEP 563 annotations - need to be resolved. diff --git a/src/cattrs/gen.py b/src/cattrs/gen.py index fbe53464..7509b3d9 100644 --- a/src/cattrs/gen.py +++ b/src/cattrs/gen.py @@ -46,12 +46,12 @@ def override(omit_if_default=None, rename=None, omit: bool = False): def make_dict_unstructure_fn( - cl, - converter, + cl: Type[T], + converter: "BaseConverter", _cattrs_omit_if_default: bool = False, _cattrs_use_linecache: bool = True, **kwargs, -) -> Callable[[Any], Dict]: +) -> Callable[[T], Dict[str, Any]]: """ Generate a specialized dict unstructuring function for an attrs class or a dataclass.