Skip to content

refactor config file finding - #8358

Closed
RonnyPfannschmidt wants to merge 8 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-3523-deprecate-setup-cfg
Closed

refactor config file finding#8358
RonnyPfannschmidt wants to merge 8 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-3523-deprecate-setup-cfg

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Feb 20, 2021

Copy link
Copy Markdown
Member

addresses #3523

setup.cfg

also a structural refactor and cleanup of the config file finding/loading

  • changelog
  • writeup in deprecated docs/links

@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-3523-deprecate-setup-cfg branch from fe3b062 to cbee643 Compare February 20, 2021 21:25
@nicoddemus

Copy link
Copy Markdown
Member

The discussion in #3523 is interesting, seems this will bring a lot of pain to our users that still keep their configuration in setup.cfg, because those that have working suites today are not being affected by the issues in #3523, so I wonder if there's other path than dropping setup.cfg support completely?

What would be the downsides of keeping the existing support, even if for some options it doesn't work so well (filterwarnings seems to be an example of that)?

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

@nicoddemus in that case lets turn setup.cfg support into something undocumented, and keep support based on the basic testing

in future we might want to try using config-parser to load setup.cfg data

based on recent developments of config-parser it may even be possible o drop inconfig and use different config-parser configurations for different config-file setups as part of a breaking release

@nicoddemus

Copy link
Copy Markdown
Member

@nicoddemus in that case lets turn setup.cfg support into something undocumented, and keep support based on the basic testing

I would be fine with that. 👍

We can remove examples of setup.cfg usage, and just leave a note stating that while it "works", there are situations where it doesn't and is not really recommended. This way if any bug reports come along because of setup.cfg support, we can just point that to users and suggest they change the configuration format.

in future we might want to try using config-parser to load setup.cfg data

based on recent developments of config-parser it may even be possible o drop inconfig and use different config-parser configurations for different config-file setups as part of a breaking release

I'm not keen on the details, but the overall idea seems good. 😁

Base automatically changed from master to main March 9, 2021 20:40
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-3523-deprecate-setup-cfg branch from cbee643 to 015fcf4 Compare June 6, 2021 20:39
@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

reviewed the docs and decided the warnings @nicoddemus did put in way earlier are good enough

@RonnyPfannschmidt RonnyPfannschmidt changed the title refactor config file finding and deprecate setup.cfg refactor config file finding Jun 6, 2021
@RonnyPfannschmidt
RonnyPfannschmidt marked this pull request as ready for review June 6, 2021 20:51
@RonnyPfannschmidt
RonnyPfannschmidt marked this pull request as draft October 3, 2021 18:44
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-3523-deprecate-setup-cfg branch 2 times, most recently from ed2ac09 to 7129605 Compare July 12, 2022 08:08
@RonnyPfannschmidt
RonnyPfannschmidt marked this pull request as ready for review July 12, 2022 08:08
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-3523-deprecate-setup-cfg branch from 8ebc068 to c6f6e59 Compare July 12, 2022 08:44
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-3523-deprecate-setup-cfg branch from c6f6e59 to 4ac06da Compare July 29, 2022 19:51

@nicoddemus nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall the changes look good, please take a look at my comments. 👍


# tox.ini
[pytest]
[tool:pytest]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't seem right, AFAIK for tox.ini files we use the [pytest] section.

down problems.
When possible, it is recommended to use the latter files, or ``pyproject.toml``, to hold your
pytest configuration.
``setup.cfg`` file usage for pytest has been deprecated, its recommended to use ``tox.ini`` or ``pyproject.toml``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
``setup.cfg`` file usage for pytest has been deprecated, its recommended to use ``tox.ini`` or ``pyproject.toml``
``setup.cfg`` file usage for pytest is supported but is not recommended for new projects. Instead, use ``tox.ini`` or ``pyproject.toml`` files.

It is not really deprecated in the sense that we will remove it in the future.

) -> Optional[Dict[str, Union[str, List[str]]]]:
"""Load pytest configuration from the given file path, if supported.
def _parse_pytest_ini(path: Path) -> PARSE_RESULT:
"""Parse the legacy pytest.ini and return the contents of the pytest section

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"""Parse the legacy pytest.ini and return the contents of the pytest section
"""Parse the pytest.ini file and return the contents of the pytest section

I don't think we should consider pytest.ini files "legacy" in any way. 😁


def locate_config(
args: Iterable[Path],
args: List[Path],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor, but for non-mutable parameters I think Sequence is more adequate.

Comment thread testing/test_config.py
"section, name",
[
("tool:pytest", "setup.cfg"),
pytest.param("tool:pytest", "setup.cfg"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems unnecessary?

"""Parse the legacy pytest.ini and return the contents of the pytest section

Return None if the file does not contain valid pytest configuration.
if the file exists and lacks a pytest section, consider it empty"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if the file exists and lacks a pytest section, consider it empty"""
if the file exists and lacks a pytest section, consider it empty."""



def _parse_ini_file(path: Path) -> PARSE_RESULT:
"""Parses .ini files with expected pytest.ini sections

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"""Parses .ini files with expected pytest.ini sections
"""Parses .ini files with expected pytest.ini sections.

def _parse_ini_file(path: Path) -> PARSE_RESULT:
"""Parses .ini files with expected pytest.ini sections

todo: investigate if tool:pytest should be added

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
todo: investigate if tool:pytest should be added
TODO: Investigate if tool:pytest should be added.

However I think it would be best to either create a new issue, or write more details about what you mean with this "TODO" (or is this something you still plan to work on this PR?).

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

Superseded by #14807.

This PR bundled two things: the structural refactor of config file finding, and the setup.cfg deprecation from #3523. The deprecation was abandoned after @nicoddemus' feedback here and reverted within the branch — and the refactor, which had already been reviewed favourably, quietly went down with it. It has been sitting here since February 2021.

#14807 redoes only the structural half against current main. Nothing about setup.cfg support changes.

It could not be rebased: this branch predates ConfigValue, native TOML mode, ini option aliases, pytest.toml/.pytest.toml and ignored_config_files, so the new PR is a re-implementation of the same design rather than a cherry-pick. The shape is unchanged — CONFIG_LOADERS keyed by file name and doubling as the discovery order, CONFIG_SUFFIXES keyed by suffix for files passed via -c, and per-format parse functions instead of nested conditionals.

Closing this one to keep the queue honest.

RonnyPfannschmidt added a commit to RonnyPfannschmidt/pytest that referenced this pull request Jul 31, 2026
Knowledge about config files was spread across three places that had to be
kept in sync by hand: load_config_dict_from_file dispatched on suffix with
filename checks nested inside those branches, locate_config re-declared the
discovery order in its own hardcoded list, and adding a format meant editing
both in the right order.

Introduce two tables instead. CONFIG_LOADERS maps a config file *name* to its
loader and doubles as the discovery order used by locate_config, so order and
parsing can no longer drift apart. CONFIG_SUFFIXES maps a *suffix* to a loader
for files passed explicitly via -c/--config-file, which may be named anything.
load_config_dict_from_file now looks up the name first and falls back to the
suffix.

The per-format rules move out of nested conditionals into named functions --
_parse_pytest_ini, _parse_ini_file, _parse_cfg_file, _parse_pytest_toml and
_parse_pyproject_toml -- each documenting the rule it implements.

This is a pure refactor: no test needed changing, and running
load_config_dict_from_file over a matrix of every supported name crossed with
present/absent/empty/malformed sections (plus unsupported and extension-less
files) yields identical values, modes, origins and exceptions before and after.

Two pre-existing warts are deliberately preserved rather than fixed here: the
CFG_PYTEST_SECTION message still names setup.cfg for any .cfg file, and a
scalar `pytest` key still raises AttributeError.

This redoes the structural half of pytest-dev#8358 on top of current main; that PR
bundled it with the abandoned setup.cfg deprecation from pytest-dev#3523.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

2 participants