Repository navigation
Preserve configuration sources when normalizing legacy keys - #2142
Conversation
|
Please add a news fragment. And for next time: use the pull request template. |
|
Added the news fragment and updated the description to use the PR template. Sorry I missed those. The full-tox checkbox is left unchecked because only the documented checks were run. |
|
My own opinion: change looks good, but the test looks overwhelming for that tiny fix. I'm lazy, so here's the AI review: The change in I do have one suggestion about the tests. They catch the bug, but they're more involved than the fix needs:
Something like this would cover the same ground: Suggested tests def _load_config_isolated(self, config=None, rc=None):
missing_file = f"{self.test_dir}/missing.conf"
with (
unittest.mock.patch.object(can, "rc", rc or {}),
unittest.mock.patch.dict("os.environ", {}, clear=True),
):
return can.util.load_config(path=missing_file, config=config)
def test_legacy_bustype_does_not_modify_config_argument(self):
for extra in ({}, {"interface": ""}, {"interface": "virtual"}):
with self.subTest(interface=extra.get("interface", "<missing>")):
original = {"bustype": "virtual", "channel": "0", **extra}
supplied = original.copy()
result = self._load_config_isolated(config=supplied)
self.assertEqual(result, {"interface": "virtual", "channel": 0})
self.assertEqual(supplied, original)
def test_legacy_bustype_does_not_modify_can_rc(self):
original = {"bustype": "virtual", "channel": "0"}
rc = original.copy()
for _ in range(2):
result = self._load_config_isolated(rc=rc)
self.assertEqual(result, {"interface": "virtual", "channel": 0})
self.assertEqual(rc, original)Both tests fail without your fix and pass with it. Feel free to adapt them. |
|
Thanks, that is a better scope. Adopted the two focused tests in 41b6024, removing the loader mocks and unrelated precedence case. Both still catch the original mutation (including all three argument variants) and pass with the fix. The related suite passes: 54 tests and 3 subtests, with 6 skips. Updated the description with the smaller tests and current validation. Update: CI and the documentation build have now passed on this head. |
Summary of Changes
Copy each configuration source before normalizing the legacy
bustypekey. This preserves caller-owned dictionaries andcan.rcwithout changing returned values or source precedence.Related Issues / Pull Requests
Type of Change
Checklist
tox).Additional Notes
The two regression tests cover the caller's
config=dictionary (missing, empty and populatedinterface) and repeated use ofcan.rc. They use an empty environment and a nonexistent configuration-file path; environment/file loader mocks and the unrelated precedence test have been removed following review.Validation on Python 3.12, with the revised tests:
can/util.pywith the exact pre-fix version from2aec6d66d28c24559cc1961d901c864d2f5b9e4efails thecan.rctest and all three argument subcases because the supplied dictionaries are mutated.python -m pytest -o addopts= -q test/test_load_config.py test/test_load_file_config.py test/test_util.py test/test_detect_available_configs.py test/test_interface.py.black --check .), configured lint scope (ruff check can examples doc) andgit diff --checkpass.The full local tox matrix, other Python versions and physical CAN hardware were not tested, so the full-tox checklist item remains unchecked. This follow-up changes tests only. AI-assisted implementation and testing.