Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converter copy does not copy unstructure hooks #398

Closed
danielnelson opened this issue Jul 26, 2023 · 3 comments
Closed

Converter copy does not copy unstructure hooks #398

danielnelson opened this issue Jul 26, 2023 · 3 comments

Comments

@danielnelson
Copy link

  • cattrs version: 23.1.2
  • Python version: 3.9.17
  • Operating System: Gentoo

Description

I wanted to create a base Converter with some common unstructuring hooks, then copy it in other places and add extra hooks. After copying the Converter I found that the unstructuring hooks from the source Converter no longer work.

What I Did

from dataclasses import dataclass
from functools import partial
from typing import Any

import cattrs


@dataclass
class C:
    value: int


@dataclass
class D:
    child: Any


# https://github.com/python-attrs/cattrs/issues/320
def _unstructure_any(converter, value):
    return converter.unstructure(value, unstructure_as=value.__class__)


converter = cattrs.Converter()
converter.register_unstructure_hook_func(
    lambda t: t is Any, partial(_unstructure_any, converter)
)
d = D(child=C(value=42))
actual = converter.unstructure(d)
expected = {"child": {"value": 42}}
assert expected == actual

converter = converter.copy()
d = D(child=C(value=42))
actual = converter.unstructure(d)
expected = {"child": {"value": 42}}
assert expected == actual, "failure"
Traceback (most recent call last):
  File "tmp.py", line 36, in <module>
    assert expected == actual, "failure"
AssertionError: failure
@danielnelson
Copy link
Author

I think the head of the handler pairs needs to be copied, because we prepend new hooks to the front, but we are currently copying the tail. Maybe a change along these lines:

diff --git a/src/cattrs/dispatch.py b/src/cattrs/dispatch.py
index f8a5445..c6542ea 100644
--- a/src/cattrs/dispatch.py
+++ b/src/cattrs/dispatch.py
@@ -137,4 +137,4 @@ class FunctionDispatch:
         return len(self._handler_pairs)
 
     def copy_to(self, other: "FunctionDispatch", skip: int = 0):
-        other._handler_pairs.extend(self._handler_pairs[skip:])
+        other._handler_pairs = self._handler_pairs[:-skip] + other._handler_pairs

@Tinche
Copy link
Member

Tinche commented Jul 26, 2023

Yeah you're right. The tests only test copying of singledispatch hooks, oops. Looking into it at #399

@Tinche
Copy link
Member

Tinche commented Jul 26, 2023

Should be fixed on main, let me know!

@Tinche Tinche closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants