From 2768023781fb6e0b23f4ad6784dfa8e02b3fc2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Tue, 8 Apr 2025 22:58:09 +0200 Subject: [PATCH] Converters: `copy()` returns Self --- HISTORY.md | 2 ++ src/cattrs/converters.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b453b9e4..7cc6e738 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,6 +28,8 @@ Our backwards-compatibility policy can be found [here](https://github.com/python ([#519](https://github.com/python-attrs/cattrs/issues/519) [#588](https://github.com/python-attrs/cattrs/pull/588)) - Generic PEP 695 type aliases are now supported. ([#611](https://github.com/python-attrs/cattrs/issues/611) [#618](https://github.com/python-attrs/cattrs/pull/618)) +- {meth}`Converter.copy` and {meth}`BaseConverter.copy` are correctly annotated as returning `Self`. + ([#644](https://github.com/python-attrs/cattrs/pull/644)) - Many preconf converters (_bson_, stdlib JSON, _cbor2_, _msgpack_, _msgspec_, _orjson_, _ujson_) skip unstructuring `int` and `str` enums, leaving them to the underlying libraries to handle with greater efficiency. ([#598](https://github.com/python-attrs/cattrs/pull/598)) diff --git a/src/cattrs/converters.py b/src/cattrs/converters.py index d5ec1250..fe1a7ba6 100644 --- a/src/cattrs/converters.py +++ b/src/cattrs/converters.py @@ -13,6 +13,7 @@ from attrs import Attribute, resolve_types from attrs import has as attrs_has +from typing_extensions import Self from ._compat import ( ANIES, @@ -981,7 +982,7 @@ def copy( unstruct_strat: UnstructureStrategy | None = None, prefer_attrib_converters: bool | None = None, detailed_validation: bool | None = None, - ) -> BaseConverter: + ) -> Self: """Create a copy of the converter, keeping all existing custom hooks. :param detailed_validation: Whether to use a slightly slower mode for detailed @@ -1367,7 +1368,7 @@ def copy( unstruct_collection_overrides: Mapping[type, UnstructureHook] | None = None, prefer_attrib_converters: bool | None = None, detailed_validation: bool | None = None, - ) -> Converter: + ) -> Self: """Create a copy of the converter, keeping all existing custom hooks. :param detailed_validation: Whether to use a slightly slower mode for detailed