diff options
| author | Bernát Gábor <bgabor8@bloomberg.net> | 2021-02-14 09:33:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-14 09:33:41 +0000 |
| commit | e30fd7466c3bcf4ff3408b2cb6dc46da97ee7d5e (patch) | |
| tree | fb990e559225f13df26230122a240d45735d4675 | |
| parent | 44c135ef30589b1dbe4d7e28cda6fed60a0c7488 (diff) | |
| download | tox-git-e30fd7466c3bcf4ff3408b2cb6dc46da97ee7d5e.tar.gz | |
Do not show inherited keys as unused (#1913)
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
| -rw-r--r-- | docs/changelog/1833.bugfix.rst | 2 | ||||
| -rw-r--r-- | src/tox/config/loader/api.py | 1 | ||||
| -rw-r--r-- | src/tox/config/loader/ini/__init__.py | 7 | ||||
| -rw-r--r-- | src/tox/config/sets.py | 5 | ||||
| -rw-r--r-- | src/tox/config/source/tox_ini.py | 5 | ||||
| -rw-r--r-- | tests/session/cmd/test_show_config.py | 3 |
6 files changed, 14 insertions, 9 deletions
diff --git a/docs/changelog/1833.bugfix.rst b/docs/changelog/1833.bugfix.rst new file mode 100644 index 00000000..c04bfc3c --- /dev/null +++ b/docs/changelog/1833.bugfix.rst @@ -0,0 +1,2 @@ +Show config no longer marks as unused keys that are inherited (e.g. if the key is coming from ``testenv`` section and our +target is ``testenv:fix``) - by :user:`gaborbernat`. diff --git a/src/tox/config/loader/api.py b/src/tox/config/loader/api.py index 4c9fb405..d031ce63 100644 --- a/src/tox/config/loader/api.py +++ b/src/tox/config/loader/api.py @@ -51,6 +51,7 @@ class Loader(Convert[T]): def __init__(self, overrides: List[Override]) -> None: self.overrides = {o.key: o for o in overrides} + self.parent: Optional["Loader[Any]"] = None @abstractmethod def load_raw(self, key: str, conf: Optional["Config"], env_name: Optional[str]) -> T: # noqa: U100 diff --git a/src/tox/config/loader/ini/__init__.py b/src/tox/config/loader/ini/__init__.py index a8867c02..55361dfa 100644 --- a/src/tox/config/loader/ini/__init__.py +++ b/src/tox/config/loader/ini/__init__.py @@ -19,12 +19,7 @@ V = TypeVar("V") class IniLoader(StrConvert, Loader[str]): """Load configuration from an ini section (ini file is a string to string dictionary)""" - def __init__( - self, - section: str, - parser: ConfigParser, - overrides: List[Override], - ) -> None: + def __init__(self, section: str, parser: ConfigParser, overrides: List[Override]) -> None: self._section: SectionProxy = parser[section] self._parser = parser super().__init__(overrides) diff --git a/src/tox/config/sets.py b/src/tox/config/sets.py index 79ef8271..c5d13937 100644 --- a/src/tox/config/sets.py +++ b/src/tox/config/sets.py @@ -109,8 +109,11 @@ class ConfigSet: def unused(self) -> List[str]: """Return a list of keys present in the config source but not used""" found: Set[str] = set() + # keys within loaders (only if the loader is not a parent too) + parents = {id(i.parent) for i in self.loaders if i.parent is not None} for loader in self.loaders: - found.update(loader.found_keys()) + if id(loader) not in parents: + found.update(loader.found_keys()) found -= self._defined.keys() return sorted(found) diff --git a/src/tox/config/source/tox_ini.py b/src/tox/config/source/tox_ini.py index 8fd80422..02f97c01 100644 --- a/src/tox/config/source/tox_ini.py +++ b/src/tox/config/source/tox_ini.py @@ -53,7 +53,7 @@ class ToxIni(Source): except KeyError: loaders: List[IniLoader] = [] self._envs[section] = loaders - + loader: Optional[IniLoader] = None if self._parser.has_section(section): loader = IniLoader( section=section, @@ -73,11 +73,14 @@ class ToxIni(Source): for base in conf["base"]: for section in (base, f"{TEST_ENV_PREFIX}{base}"): if self._parser.has_section(section): + child = loader loader = IniLoader( section=section, parser=self._parser, overrides=override_map.get(section, []), ) + if child is not None: + child.parent = loader yield loader loaders.append(loader) break diff --git a/tests/session/cmd/test_show_config.py b/tests/session/cmd/test_show_config.py index 6cd90709..b44b43c8 100644 --- a/tests/session/cmd/test_show_config.py +++ b/tests/session/cmd/test_show_config.py @@ -62,7 +62,8 @@ def test_show_config_filter_keys(tox_project: ToxProjectCreator) -> None: def test_show_config_unused(tox_project: ToxProjectCreator) -> None: - outcome = tox_project({"tox.ini": "[testenv:py]\nmagical=yes\nmagic=yes"}).run("c", "-e", "py") + tox_ini = "[testenv]\nok=false\n[testenv:py]\nmagical=yes\nmagic=yes" + outcome = tox_project({"tox.ini": tox_ini}).run("c", "-e", "py") outcome.assert_success() assert "\n# !!! unused: magic, magical\n" in outcome.out |
