Skip to content

Implement parsing of default command configuration from pyproject.toml - #1290

Merged
akx merged 9 commits into
python-babel:masterfrom
max-moser:mm/pyproject-config
Nov 24, 2026
Merged

akx merged 9 commits into
python-babel:masterfrom
max-moser:mm/pyproject-config

Conversation

@max-moser

Copy link
Copy Markdown
Contributor

I believe this should close #777

This PR adds support of pyproject.toml as a source for default configuration values for the various CLI commands (compile, extract, init, update).
The configuration will be read from pyproject.toml, under the [tool.babel.<command_name>] table, where <command_name> is one of compile_catalog, extract_messages, init_catalog, update_catalog (taken from frontend_setuptools).

The motivation for this PR here is that I've recently migrated most of the inveniosoftware packages from setuptools to hatchling and we included the old configuration from setup.cfg in pyproject.toml, only to find that pybabel doesn't actually read any command config from pyproject.toml 😅
(Luckily for us that was only a minor issue in one package that we fixed otherwise)


The mechanism for finding pyproject.toml is similar to that used in other tools, like isort.
I could also imagine adding a global CLI flag for disabling reading pyproject.toml altogether, or for letting users specify another TOML config file instead.

The parsed values are inserted as default values for the optparse parser, so user-provided CLI flags should still take precedence.

Please let me know if this goes in an acceptable direction, then I can polish up the implementation a bit 🙂

@max-moser
max-moser force-pushed the mm/pyproject-config branch from 65a5cdb to ceb1f4b Compare July 7, 2026 14:34
@akx akx self-assigned this Jul 7, 2026
@akx akx added the feature label Jul 7, 2026
@max-moser
max-moser force-pushed the mm/pyproject-config branch from ceb1f4b to 511f10d Compare July 11, 2026 08:47
@akx akx added this to the Babel 2.19 milestone Aug 14, 2026
@akx
akx force-pushed the mm/pyproject-config branch from 511f10d to da57261 Compare October 30, 2026 09:11

@akx akx 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.

This would require tests -- oh and what happens if this finds a pyproject.toml that doesn't have tool.babel?

@max-moser

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I'll spruce up the PR a bit. 🙂

@max-moser
max-moser force-pushed the mm/pyproject-config branch 2 times, most recently from a39395c to af58d33 Compare October 30, 2026 11:07

@max-moser max-moser left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've touched up the PR a bit to address your feedback (especially the case when the section is missing from pyproject.toml) and generally touched up the implementation a little bit (with type hints and such).

There's now a test case for the config parsing itself, and one for the extract_messages CLI command that basically emulates what another test did, but with the config coming from pyproject.toml instead of CLI args.

Comment thread babel/messages/frontend.py Resolved

@akx akx 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.

Thanks! Some comments -- this is looking pretty good.

I think we might want a BABEL_NO_PYPROJECT_TOML escape hatch for now?

Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread tests/messages/frontend/test_cli.py Resolved
Comment thread tests/messages/test_toml_config.py Resolved
Comment thread tests/messages/test_toml_config.py Resolved
@max-moser
max-moser force-pushed the mm/pyproject-config branch 7 times, most recently from 2ed7648 to f3ffa42 Compare November 5, 2026 13:28

@akx akx 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.

Some comments -- not sure if you're still actively pushing here rn :)

Comment thread tests/messages/frontend/test_cli.py Resolved
Comment thread tests/messages/frontend/test_cli.py Resolved
Comment thread babel/messages/frontend.py Resolved
@max-moser
max-moser force-pushed the mm/pyproject-config branch from 6d4fc79 to 326d912 Compare November 5, 2026 13:49
Comment thread babel/messages/frontend.py Resolved
@max-moser
max-moser force-pushed the mm/pyproject-config branch from 326d912 to 44946e2 Compare November 5, 2026 13:57
@max-moser

Copy link
Copy Markdown
Contributor Author

Done pushing now :)

@max-moser
max-moser force-pushed the mm/pyproject-config branch from b2df682 to 247344d Compare November 5, 2026 15:24
@max-moser
max-moser requested a review from akx November 5, 2026 15:25

@akx akx 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.

Non-exhaustive review, some notes.. 😅

Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
print(f" {name:<{cmd_width}} {description}")

def _parse_pyproject_toml_config(self, cmdinst) -> dict:
pyproject_toml_path_override = os.getenv("BABEL_PYPROJECT_TOML", None)

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.

Is this meant to be the TOML filename, or the directory? I think we're handling it as a directory right now.

@max-moser max-moser Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes right, if somebody specifies a file then it won't be matched in the first iteration of the search.
If the desired file is called pyproject.toml, it will still be found (effectively checking the path.parent / "pyproject.toml"), but if it has a different name (e.g. anothername.toml), that won't be picked up:

In [3]: find_pyproject_toml(Path(".."))

In [4]: find_pyproject_toml(Path("."))
Out[4]: PosixPath('/home/mmoser/git/babel/pyproject.toml')

In [5]: find_pyproject_toml(Path("pyproject.toml"))
Out[5]: PosixPath('/home/mmoser/git/babel/pyproject.toml')

In [6]: find_pyproject_toml(Path("anothername.toml"))
Out[6]: PosixPath('/home/mmoser/git/babel/pyproject.toml')

Given that the filename for pyproject.toml is kind of fixed here, I don't think that latter case matters, though.
Would you prefer another name for the env var?


As a side note, checking this, I actually found that a path.resolve() was missing to make path.parent work as expected:

In [7]: Path(".").parent
Out[7]: PosixPath('.')

In [8]: Path(".").parent.parent
Out[8]: PosixPath('.')

In [9]: Path(".").resolve().parent.parent
Out[9]: PosixPath('/home/mmoser')

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.

Upon further pondering: If the user explicitly sets BABEL_PYPROJECT_TOML, we should honor it as a filename as-is, and just try to load from there, without trying to be fancy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added back an early escape in _find_pyproject_toml(), right after path = path.resolve():

    if path.is_file():
        return path

So if a user specifies an existing file, it will be taken as is (even if it has a name different from pyproject.toml).
If the user only specified a directory (or a file that doesn't exist), the search logic kicks in.

Could of course also add e.g. a kwarg to _parse_pyproject_toml_config(..., search=False) to opt out of the _find_pyproject_toml() call.

Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread tests/messages/frontend/test_cli.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
@max-moser
max-moser force-pushed the mm/pyproject-config branch from 247344d to 0476dce Compare November 9, 2026 22:59
Comment thread babel/messages/frontend.py Resolved
Comment thread babel/messages/frontend.py Resolved
@akx
akx requested a balanced review from Copilot November 11, 2026 15:06
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.34146% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.69%. Comparing base (aeca9fa) to head (7f06a48).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
babel/messages/frontend.py 96.34% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1290      +/-   ##
==========================================
+ Coverage   93.67%   93.69%   +0.01%     
==========================================
  Files          27       27              
  Lines        4983     5046      +63     
==========================================
+ Hits         4668     4728      +60     
- Misses        315      318       +3     
Flag Coverage Δ
macos-26-3.10 ?
macos-26-3.11 92.72% <92.68%> (+0.03%) ⬆️
macos-26-3.12 92.98% <92.68%> (+0.02%) ⬆️
macos-26-3.13 92.98% <92.68%> (+0.02%) ⬆️
macos-26-3.14 92.96% <92.68%> (+0.02%) ⬆️
macos-26-3.15 92.96% <92.68%> (+0.02%) ⬆️
macos-26-3.9 ?
macos-26-pypy3.11 92.72% <92.68%> (+0.03%) ⬆️
macos-26-pypy3.12 92.98% <92.68%> (+0.02%) ⬆️
ubuntu-24.04-3.10 92.80% <96.34%> (+0.03%) ⬆️
ubuntu-24.04-3.11 92.74% <92.68%> (+0.03%) ⬆️
ubuntu-24.04-3.12 93.00% <92.68%> (+0.02%) ⬆️
ubuntu-24.04-3.13 93.00% <92.68%> (+0.02%) ⬆️
ubuntu-24.04-3.14 92.98% <92.68%> (+0.02%) ⬆️
ubuntu-24.04-3.15 92.98% <92.68%> (+0.02%) ⬆️
ubuntu-24.04-3.9 92.74% <96.34%> (+0.03%) ⬆️
ubuntu-24.04-pypy3.11 92.74% <92.68%> (+0.03%) ⬆️
ubuntu-24.04-pypy3.12 93.00% <92.68%> (+0.02%) ⬆️
windows-2025-3.10 92.79% <96.34%> (+0.03%) ⬆️
windows-2025-3.11 92.73% <92.68%> (+0.03%) ⬆️
windows-2025-3.12 92.99% <92.68%> (+0.02%) ⬆️
windows-2025-3.13 92.99% <92.68%> (+0.02%) ⬆️
windows-2025-3.14 92.97% <92.68%> (+0.02%) ⬆️
windows-2025-3.15 92.97% <92.68%> (+0.02%) ⬆️
windows-2025-3.9 92.73% <96.34%> (+0.03%) ⬆️
windows-2025-pypy3.11 92.73% <92.68%> (+0.03%) ⬆️
windows-2025-pypy3.12 92.99% <92.68%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing max-moser:mm/pyproject-config (7f06a48) with master (7768653)

Open in CodSpeed

@max-moser

max-moser commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

I've factored in the ruff linter complaints and added a test for the search logic for pyproject.toml in the hopes of increasing the test coverage a bit more.
Not sure if adding tests for the remaining 7 or so uncovered lines from this PR is actually worth it or if they would become more of a potential maintenance burden in the future?
Especially a test for the ImportError handling would feel very artificial.

@akx

akx commented Nov 24, 2026

Copy link
Copy Markdown
Member

Don't worry about perfect test coverage, it's generally not worth it.

@akx

akx commented Nov 24, 2026

Copy link
Copy Markdown
Member

@max-moser Do you mind if I force-push onto your branch, etc.? I'd like to get this over the finish line for Babel 2.19 (imminent, I hope), but I'd like to limit the pyproject.toml finding behavior to the cwd only for the time being (plus some other fixes).

@max-moser

Copy link
Copy Markdown
Contributor Author

Sure thing, please do go ahead!

max-moser and others added 8 commits November 24, 2026 18:02
If present, use a "pyproject.toml" file for default configuration of
`pybabel` commands.

Co-authored-by: Aarni Koskela <akx@iki.fi>
The environment variable `BABEL_PYPROJECT_TOML` can be used to set
an alternative search path for a "pyproject.toml" file for configuration
parsing by setting it to a path.
If set to "skip" or "none", the config parsing from pyproject.toml will
be disabled altogether.

Co-authored-by: Aarni Koskela <akx@iki.fi>
If the user explicitly specified a path for searching pyproject.toml, we
assume that they know what they're doing, and we crash loudly.
Relative paths in the configuration are resolved relative to the current working directory,
so picking up a pyproject.toml from a parent directory could make them point to the wrong place.
* If unreadable or malformed, don't crash
* Skip parsing pyproject.toml files that can't contain Babel configuration altogether
A path that isn't a file is now an error instead of silently falling back to searching from it.
E.g. `output-file` is now treated the same as `output_file`,
matching the command line option names.
@akx
akx requested review from akx and a balanced review from Copilot November 24, 2026 16:32

This comment was marked as off-topic.

@akx
akx force-pushed the mm/pyproject-config branch from 6836330 to 6472a70 Compare November 24, 2026 16:33
@akx

akx commented Nov 24, 2026

Copy link
Copy Markdown
Member

@max-moser Okay, pushed a pile of commits on top of yours (and removed the search-logic test one, since it's no longer a thing 😄):

  • pyproject.toml is searched only in the cwd, since if it contains relative paths, those would be wonky. Babel would need a "base directory" wiring for that to work reliably.
  • bad (e.g. non-UTF-8, invalid TOML, etc.) pyproject.tomls don't crash the CLI, unless you explicitly ask to read one
  • pyproject.tomls without babel mentions aren't even parsed
  • kebab-case option names are allowed in addition to snake_case ones
  • tiny docs

There are likely corner cases about this that no one knows about, but we'll get there later.

@akx akx 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.

Let's do this 😄

Thanks for putting up with the very sluggish review.

@akx
akx enabled auto-merge November 24, 2026 17:09
@max-moser

Copy link
Copy Markdown
Contributor Author

No worries, I know how busy life can get, and there's still only 24h in a day!
Thanks a lot for making this work mate 😄

@akx
akx disabled auto-merge November 24, 2026 17:45
@akx
akx merged commit 1a0763a into python-babel:master Nov 24, 2026
34 of 36 checks passed
@max-moser
max-moser deleted the mm/pyproject-config branch November 24, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parse settings from pyproject.toml

3 participants