From 9e70b591b027dfbedbe538fe287ecc616aa1affb Mon Sep 17 00:00:00 2001 From: Sushanth012 <146400678+Sushanth012@users.noreply.github.com> Date: Sat, 10 Oct 2026 13:38:41 -0700 Subject: [PATCH] Fix database saves mutating site detectors (#3184) Co-authored-by: Soxoj --- maigret/sites.py | 8 ++++---- tests/test_sites.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/maigret/sites.py b/maigret/sites.py index 62786997..0d8f0a1c 100644 --- a/maigret/sites.py +++ b/maigret/sites.py @@ -357,10 +357,10 @@ class MaigretSite: if not self.engine_obj: return self - self.request_future = None - self.url_regexp = None - - self_copy = copy.deepcopy(self) + self_copy = copy.copy(self) + self_copy.request_future = None + self_copy.url_regexp = None + self_copy = copy.deepcopy(self_copy) engine_data = self_copy.engine_obj and self_copy.engine_obj.site or {} site_data_keys = list(self_copy.__dict__.keys()) diff --git a/tests/test_sites.py b/tests/test_sites.py index be1a09c2..b4b96f54 100644 --- a/tests/test_sites.py +++ b/tests/test_sites.py @@ -119,6 +119,19 @@ def test_site_strip_engine_data(): assert amperka_stripped.json == EXAMPLE_DB['sites']['Amperka'] +def test_saving_database_preserves_site_url_detector(tmp_path): + db = MaigretDatabase() + db.load_from_json(EXAMPLE_DB) + amperka = db.sites[0] + + db.save_to_file(str(tmp_path / 'data.json')) + + assert ( + amperka.detect_username('http://forum.amperka.ru/members/?username=test') + == 'test' + ) + + def test_site_strip_engine_data_with_site_prior_updates(): db = MaigretDatabase() UPDATED_EXAMPLE_DB = dict(EXAMPLE_DB)