-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve debugability of deepcopy errors
- Loading branch information
Showing
4 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import sys | ||
from typing import Any, Dict, NoReturn | ||
|
||
from pytest import raises | ||
|
||
from dependency_injector.errors import NonCopyableArgumentError | ||
from dependency_injector.providers import ( | ||
Provider, | ||
deepcopy, | ||
deepcopy_args, | ||
deepcopy_kwargs, | ||
) | ||
|
||
|
||
class NonCopiable: | ||
def __deepcopy__(self, memo: Dict[int, Any]) -> NoReturn: | ||
raise NotImplementedError | ||
|
||
|
||
def test_deepcopy_streams_not_copied() -> None: | ||
l = [sys.stdin, sys.stdout, sys.stderr] | ||
assert deepcopy(l) == l | ||
|
||
|
||
def test_deepcopy_args() -> None: | ||
provider = Provider[None]() | ||
copiable = NonCopiable() | ||
memo: Dict[int, Any] = {id(copiable): copiable} | ||
|
||
assert deepcopy_args(provider, (1, copiable), memo) == (1, copiable) | ||
|
||
|
||
def test_deepcopy_args_non_copiable() -> None: | ||
provider = Provider[None]() | ||
copiable = NonCopiable() | ||
memo: Dict[int, Any] = {id(copiable): copiable} | ||
|
||
with raises( | ||
NonCopyableArgumentError, | ||
match=r"^Couldn't copy argument at index 3 for provider ", | ||
): | ||
deepcopy_args(provider, (1, copiable, object(), NonCopiable()), memo) | ||
|
||
|
||
def test_deepcopy_kwargs() -> None: | ||
provider = Provider[None]() | ||
copiable = NonCopiable() | ||
memo: Dict[int, Any] = {id(copiable): copiable} | ||
|
||
assert deepcopy_kwargs(provider, {"x": 1, "y": copiable}, memo) == { | ||
"x": 1, | ||
"y": copiable, | ||
} | ||
|
||
|
||
def test_deepcopy_kwargs_non_copiable() -> None: | ||
provider = Provider[None]() | ||
copiable = NonCopiable() | ||
memo: Dict[int, Any] = {id(copiable): copiable} | ||
|
||
with raises( | ||
NonCopyableArgumentError, | ||
match=r"^Couldn't copy keyword argument z for provider ", | ||
): | ||
deepcopy_kwargs(provider, {"x": 1, "y": copiable, "z": NonCopiable()}, memo) |