Skip to content

Commit ba8bb80

Browse files
committed
Duplicate IniSection to TomlSection
1 parent 80795f0 commit ba8bb80

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

src/tox/config/loader/section.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from abc import ABC
34
from typing import TYPE_CHECKING
45

56
if TYPE_CHECKING:
@@ -11,7 +12,7 @@
1112
from typing_extensions import Self
1213

1314

14-
class Section: # noqa: PLW1641
15+
class Section(ABC): # noqa: PLW1641
1516
"""tox configuration section."""
1617

1718
SEP = ":" #: string used to separate the prefix and the section in the key

src/tox/config/source/toml.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
from tox.config.loader.ini.factor import find_envs
1515
from tox.config.loader.memory import MemoryLoader
16-
from tox.config.loader.section import Section
1716

1817
from .api import Source
19-
from .ini_section import CORE, PKG_ENV_PREFIX, TEST_ENV_PREFIX, IniSection
18+
from .toml_section import CORE, PKG_ENV_PREFIX, TEST_ENV_PREFIX, TomlSection
2019

2120
if TYPE_CHECKING:
2221
from pathlib import Path
2322

2423
from tox.config.loader.api import OverrideMap
24+
from tox.config.loader.section import Section
2525
from tox.config.sets import ConfigSet
2626

2727

@@ -45,13 +45,13 @@ def __init__(self, path: Path, content: str | None = None) -> None:
4545
def __repr__(self) -> str:
4646
return f"{type(self).__name__}(path={self.path})"
4747

48-
def transform_section(self, section: Section) -> Section: # noqa: PLR6301
49-
return IniSection(section.prefix, section.name)
48+
def transform_section(self, section: Section) -> Section:
49+
return TomlSection(section.prefix, section.name)
5050

5151
def get_loader(self, section: Section, override_map: OverrideMap) -> MemoryLoader | None:
5252
# look up requested section name in the generative testenv mapping to find the real config source
5353
for key in self._section_mapping.get(section.name) or []:
54-
if section.prefix is None or Section.from_key(key).prefix == section.prefix:
54+
if section.prefix is None or TomlSection.from_key(key).prefix == section.prefix:
5555
break
5656
else:
5757
# if no matching section/prefix is found, use the requested section key as-is (for custom prefixes)
@@ -66,14 +66,14 @@ def get_loader(self, section: Section, override_map: OverrideMap) -> MemoryLoade
6666

6767
def get_base_sections(self, base: list[str], in_section: Section) -> Iterator[Section]: # noqa: PLR6301
6868
for a_base in base:
69-
section = IniSection.from_key(a_base)
69+
section = TomlSection.from_key(a_base)
7070
yield section # the base specifier is explicit
7171
if in_section.prefix is not None: # no prefix specified, so this could imply our own prefix
72-
yield IniSection(in_section.prefix, a_base)
72+
yield TomlSection(in_section.prefix, a_base)
7373

74-
def sections(self) -> Iterator[IniSection]:
74+
def sections(self) -> Iterator[Section]:
7575
for key in self._raw:
76-
yield IniSection.from_key(key)
76+
yield TomlSection.from_key(key)
7777

7878
def envs(self, core_config: ConfigSet) -> Iterator[str]:
7979
seen = set()
@@ -102,7 +102,7 @@ def register_factors(envs: Iterable[str]) -> None:
102102
for section in self.sections():
103103
yield from self._discover_from_section(section, known_factors)
104104

105-
def _discover_from_section(self, section: IniSection, known_factors: set[str]) -> Iterator[str]:
105+
def _discover_from_section(self, section: Section, known_factors: set[str]) -> Iterator[str]:
106106
for value in self._raw[section.key].values():
107107
if isinstance(value, bool):
108108
# It's not a value with env definition.
@@ -114,9 +114,9 @@ def _discover_from_section(self, section: IniSection, known_factors: set[str]) -
114114
yield env
115115

116116
def get_tox_env_section(self, item: str) -> tuple[Section, list[str], list[str]]: # noqa: PLR6301
117-
return IniSection.test_env(item), [TEST_ENV_PREFIX], [PKG_ENV_PREFIX]
117+
return TomlSection.test_env(item), [TEST_ENV_PREFIX], [PKG_ENV_PREFIX]
118118

119-
def get_core_section(self) -> Section:
119+
def get_core_section(self) -> TomlSection:
120120
return self.CORE_SECTION
121121

122122

src/tox/config/source/toml_section.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from __future__ import annotations
2+
3+
from tox.config.loader.ini.factor import extend_factors
4+
from tox.config.loader.section import Section
5+
6+
# TODO: Rename to 'env'
7+
TEST_ENV_PREFIX = "testenv"
8+
PKG_ENV_PREFIX = "pkgenv"
9+
10+
11+
# TODO: Duplicates IniSection
12+
class TomlSection(Section):
13+
@classmethod
14+
def test_env(cls, name: str) -> TomlSection:
15+
return cls(TEST_ENV_PREFIX, name)
16+
17+
@property
18+
def is_test_env(self) -> bool:
19+
return self.prefix == TEST_ENV_PREFIX
20+
21+
@property
22+
def names(self) -> list[str]:
23+
return list(extend_factors(self.name))
24+
25+
26+
CORE = TomlSection(None, "tox")

0 commit comments

Comments
 (0)