Skip to content

Commit

Permalink
Better module reloader fixture. #341
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Jun 27, 2024
1 parent 6a456ef commit 5c2d722
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ def __init__(self, monkeypatch):
self.monkeypatch = monkeypatch

def __call__(self, module):
self.modules.append(module)
self.modules.append((module, module.__dict__.copy()))
return importlib.reload(module)

def undo(self):
# undo monkeypatches before reloading again,
# to ensure modules are reloaded from a "clean" environment
self.monkeypatch.undo()

# previously, this would reload the module again,
# creating *new* versions of the old classes,
# which breaks some modules (e.g. pathlib).
# restoring the original attributes seems to work better.
while self.modules:
importlib.reload(self.modules.pop())
module, module_dict = self.modules.pop()
module.__dict__.clear()
module.__dict__.update(module_dict)


@pytest.fixture
Expand Down

0 comments on commit 5c2d722

Please sign in to comment.