Skip to content

Commit

Permalink
Raise exception where original is not in the __dict__ of an object
Browse files Browse the repository at this point in the history
This prevents problems where attempts are made to replace an attribute on a subclass instead of the base class.
  • Loading branch information
cjw296 committed May 1, 2024
1 parent 6085dd3 commit 55fcbb9
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 25 deletions.
14 changes: 14 additions & 0 deletions testfixtures/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,23 @@ def __call__(

if resolved.setter is None:
raise ValueError('target must contain at least one dot!')

if resolved.found is not_there and strict:
raise AttributeError('Original %r not found' % resolved.name)

if (
hasattr(resolved.container, '__dict__') and
resolved.setter is setattr and
resolved.name not in resolved.container.__dict__
):
if strict:
raise AttributeError(
f'{resolved.container!r} has __dict__ but {resolved.name!r} is not in it'
)
else:
resolved.found = not_there


replacement_to_use = replacement

if isinstance(resolved.container, type):
Expand Down
Loading

0 comments on commit 55fcbb9

Please sign in to comment.