Skip to content

Preserve configuration sources when normalizing legacy keys - #2142

Merged
zariiii9003 merged 3 commits into
hardbyte:mainfrom
Solarboaters:fix/preserve-config-sources
Nov 22, 2026
Merged

zariiii9003 merged 3 commits into
hardbyte:mainfrom
Solarboaters:fix/preserve-config-sources

Conversation

@Solarboaters

@Solarboaters Solarboaters commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

Copy each configuration source before normalizing the legacy bustype key. This preserves caller-owned dictionaries and can.rc without changing returned values or source precedence.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

The two regression tests cover the caller's config= dictionary (missing, empty and populated interface) and repeated use of can.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:

  • Replacing can/util.py with the exact pre-fix version from 2aec6d66d28c24559cc1961d901c864d2f5b9e4e fails the can.rc test and all three argument subcases because the supplied dictionaries are mutated.
  • Restoring the fix: both tests and all three subcases pass.
  • Configuration, file configuration, utility, interface discovery and interface tests: 54 passed, 6 skipped, 3 subtests passed. Command: 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.
  • Repository formatting (black --check .), configured lint scope (ruff check can examples doc) and git diff --check pass.

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.

@zariiii9003

Copy link
Copy Markdown
Collaborator

Please add a news fragment. And for next time: use the pull request template.

@Solarboaters

Copy link
Copy Markdown
Contributor Author

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.

@zariiii9003

Copy link
Copy Markdown
Collaborator

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 can/util.py looks good to me. It's minimal, doesn't change return values or which source wins, and also covers can.rc, which users populate themselves.

I do have one suggestion about the tests. They catch the bug, but they're more involved than the fix needs:

  • Only two inputs can actually be modified: the config= argument and can.rc. load_environment_config() and load_file_config() build a new dict on every call, so the environment and file cases can only fail because the mocks return the same dict.
  • test_config_precedence_with_legacy_interface mostly checks precedence, which this change doesn't affect. It would make a nice test in its own right, but I'd rather keep it out of this PR.
  • Instead of patching the loader functions, you can keep the test isolated from real config by clearing os.environ and passing a non-existent path=, like the other tests in this file do.

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.

@Solarboaters

Solarboaters commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@zariiii9003
zariiii9003 merged commit 1c3bba5 into hardbyte:main Nov 22, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid modifying the config dict passed to load_config()

2 participants