Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config file support #940

Closed

Conversation

chrisbouchard
Copy link

@chrisbouchard chrisbouchard commented Apr 2, 2024

Add support for configuration files in pyupgrade using ConfigArgParse.

By default, we'll look for a tool.pyupgrade section in pyupgrade.toml or pyproject.toml (in that order), and read the configuration from there. Command line arguments will override configuration file settings, which will override command line argument defaults. We also add a --config-file option to override the config file path.

Fixes #511.

Example Output

% pyupgrade -h
usage: pyupgrade [-h] [--config-file CONFIG_FILE] [--exit-zero-even-if-changed] [--keep-percent-format] [--keep-mock] [--keep-runtime-typing] [--py3-plus]
                 [--py36-plus] [--py37-plus] [--py38-plus] [--py39-plus] [--py310-plus] [--py311-plus] [--py312-plus]
                 [filenames ...]

positional arguments:
  filenames

options:
  -h, --help            show this help message and exit
  --config-file CONFIG_FILE
                        config file path
  --exit-zero-even-if-changed
  --keep-percent-format
  --keep-mock
  --keep-runtime-typing
  --py3-plus, --py3-only
  --py36-plus
  --py37-plus
  --py38-plus
  --py39-plus
  --py310-plus
  --py311-plus
  --py312-plus

Args that start with '--' can also be set in a config file (pyupgrade.toml or pyproject.toml or specified via --config-file). Config file syntax is Tom's
Obvious, Minimal Language. See https://github.com/toml-lang/toml/blob/v0.5.0/README.md for details. In general, command-line values override config file
values which override defaults.

TODO

  • Add ConfigArgParse dependency
  • Switch from argparse to configargparse
  • Add unit tests for config file parsing
  • Add documentation for config file handling

ConfigArgParse [1] is "[a] drop-in replacement for argparse that allows options
to also be set via config files and/or environment variables." We can use it to
provide config file support while reusing our existing argument parsing code.
No functional change, but just replace argparse.ArgumentParser with the drop-in
from configargparse. I've also extracted the parser to a separate function to
keep it contained.
It will look for pyupgrade.toml, falling back to pyproject.toml. In both files,
it will read the tool.upgrade section.
@asottile
Copy link
Owner

asottile commented Apr 2, 2024

no thanks, this has been rejected before please search next time

@asottile asottile closed this Apr 2, 2024
@asottile
Copy link
Owner

asottile commented Apr 2, 2024

oh you did find the duplicate and decided to waste both of our time anyway

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.

Use pyproject.toml as alternative to command line args
3 participants