Skip to content

Conversation

@kaste
Copy link
Owner

@kaste kaste commented Jan 22, 2025

Fixes #89

Surprisingly deepcopy(mock()) returned None. (The reason was that
deepcopy(m) essentially is m.__deepcopy__() and unconfigured mocks
return None on all method calls.)

We generally change that: most but the basic dunder methods inherited
from object (and except __call__) now raise AttributeError if
unconfigured. This doesn't change a lot in practice but has the
side-effect that hasattr calls also answer correctly (False) in
these cases.

For dumb mocks this also means that __deepcopy__ now throws, and hence
Python kicks in the default, other implementation of the deepcopy
algorithm.

For strict mocks, we actually catch the __deepcopy__ call and raise
RuntimeError as the AttributeError is implicitly caught by
deepcopy.

Note that a deepcopy does not copy or even spin off the expectations
or the recorded usage of the mock. Users need to make that manually,
e.g. when(copy).deepcopy(m).thenReturn(n).

kaste added 3 commits January 22, 2025 23:00
Fixes #89

Surprisingly `deepcopy(mock())` returned `None`.  (The reason was that
`deepcopy(m)` essentially is `m.__deepcopy__()` and unconfigured mocks
return `None` on all method calls.)

We generally change that:  most but the basic dunder methods inherited
from `object` (and except `__call__`) now raise `AttributeError` if
unconfigured.  This doesn't change a lot in practice but has the
side-effect that `hasattr` calls also answer correctly (`False`) in
these cases.

For dumb mocks this also means that `__deepcopy__` now throws, and hence
Python kicks in the default, other implementation of the deepcopy
algorithm.

For strict mocks, we actually catch the `__deepcopy__` call and raise
`RuntimeError` as the `AttributeError` is implicitly caught by
`deepcopy`.

Note that a deepcopy does not copy or even spin off the expectations
or the recorded usage of the mock.  Users need to make that manually,
e.g. `when(copy).deepcopy(m).thenReturn(n)`.
@kaste kaste force-pushed the the-deepcopy-case branch from 80a7462 to 5bfcf6d Compare January 22, 2025 22:01
@kaste kaste merged commit b35bc1a into master Jan 22, 2025
44 checks passed
@kaste kaste deleted the the-deepcopy-case branch January 22, 2025 22:05
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

Successfully merging this pull request may close these issues.

deepcopy doesn't work with mock()

2 participants