MCPcopy
hub / github.com/pytest-dev/pytest / load_config_dict_from_file

Function load_config_dict_from_file

src/_pytest/config/findpaths.py:58–155  ·  view source on GitHub ↗

Load pytest configuration from the given file path, if supported. Return None if the file does not contain valid pytest configuration.

(
    filepath: Path,
)

Source from the content-addressed store, hash-verified

56
57
58def load_config_dict_from_file(
59 filepath: Path,
60) -> ConfigDict | None:
61 """Load pytest configuration from the given file path, if supported.
62
63 Return None if the file does not contain valid pytest configuration.
64 """
65 # Configuration from ini files are obtained from the [pytest] section, if present.
66 if filepath.suffix == ".ini":
67 iniconfig = _parse_ini_config(filepath)
68
69 if "pytest" in iniconfig:
70 return {
71 k: ConfigValue(v, origin="file", mode="ini")
72 for k, v in iniconfig["pytest"].items()
73 }
74 else:
75 # "pytest.ini" files are always the source of configuration, even if empty.
76 if filepath.name in {"pytest.ini", ".pytest.ini"}:
77 return {}
78
79 # '.cfg' files are considered if they contain a "[tool:pytest]" section.
80 elif filepath.suffix == ".cfg":
81 iniconfig = _parse_ini_config(filepath)
82
83 if "tool:pytest" in iniconfig.sections:
84 return {
85 k: ConfigValue(v, origin="file", mode="ini")
86 for k, v in iniconfig["tool:pytest"].items()
87 }
88 elif "pytest" in iniconfig.sections:
89 # If a setup.cfg contains a "[pytest]" section, we raise a failure to indicate users that
90 # plain "[pytest]" sections in setup.cfg files is no longer supported (#3086).
91 fail(CFG_PYTEST_SECTION.format(filename="setup.cfg"), pytrace=False)
92
93 # '.toml' files are considered if they contain a [tool.pytest] table (toml mode)
94 # or [tool.pytest.ini_options] table (ini mode) for pyproject.toml,
95 # or [pytest] table (toml mode) for pytest.toml/.pytest.toml.
96 elif filepath.suffix == ".toml":
97 if sys.version_info >= (3, 11):
98 import tomllib
99 else:
100 import tomli as tomllib
101
102 toml_text = filepath.read_text(encoding="utf-8")
103 try:
104 config = tomllib.loads(toml_text)
105 except tomllib.TOMLDecodeError as exc:
106 raise UsageError(f"{filepath}: {exc}") from exc
107
108 # pytest.toml and .pytest.toml use [pytest] table directly.
109 if filepath.name in ("pytest.toml", ".pytest.toml"):
110 pytest_config = config.get("pytest", {})
111 if pytest_config:
112 # TOML mode - preserve native TOML types.
113 return {
114 k: ConfigValue(v, origin="file", mode="toml")
115 for k, v in pytest_config.items()

Calls 7

_parse_ini_configFunction · 0.85
ConfigValueClass · 0.85
UsageErrorClass · 0.85
make_scalarFunction · 0.85
read_textMethod · 0.80
formatMethod · 0.45
getMethod · 0.45