Skip to content

Commit

Permalink
Flesh out type annotations
Browse files Browse the repository at this point in the history
Closes #281
  • Loading branch information
Tinche committed Jul 4, 2022
1 parent b1a681a commit 38205da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/python-attrs/cattrs/issues/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 <https://github.com/python-attrs/cattrs/issues/275>`_)
* Fix `Converter.register_structure_hook_factory` and `cattrs.gen.make_dict_unstructure_fn` type annotations.
(`#281 <https://github.com/python-attrs/cattrs/issues/281>`_)

22.1.0 (2022-04-03)
-------------------
Expand Down
10 changes: 7 additions & 3 deletions src/cattrs/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/cattrs/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 38205da

Please sign in to comment.