From 388e1cf458d5b5eea993055d16ac53a5b6b0a1b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Mon, 13 May 2024 10:10:32 -0700 Subject: [PATCH] Fix table ordering for sub tables and do not version strip on the ~ operator (#210) --- .readthedocs.yml | 16 ++++--------- README.md | 12 ++-------- docs/index.rst | 50 ++++++++++++++++++++++++---------------- pyproject.toml | 2 +- src/pyproject_fmt/cli.py | 19 +++++---------- tests/test_main.py | 5 ++-- tox.ini | 6 +++-- 7 files changed, 50 insertions(+), 60 deletions(-) diff --git a/.readthedocs.yml b/.readthedocs.yml index 6e293f0..98529f0 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -2,14 +2,8 @@ version: 2 build: os: ubuntu-22.04 tools: - python: "3.11" -python: - install: - - method: pip - path: . - extra_requirements: - - docs -sphinx: - builder: html - configuration: docs/conf.py - fail_on_warning: true + python: "3.12" + commands: + - pip install tox-uv + - tox r -e docs -vv --notest + - tox r -e docs --skip-pkg-install -- "${READTHEDOCS_OUTPUT}"/html diff --git a/README.md b/README.md index c9a27e1..a2c7f01 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,6 @@ [![PyPI - License](https://img.shields.io/pypi/l/pyproject-fmt?style=flat-square)](https://opensource.org/licenses/MIT) [![check](https://github.com/tox-dev/pyproject-fmt/actions/workflows/check.yml/badge.svg)](https://github.com/tox-dev/pyproject-fmt/actions/workflows/check.yml) -Apply a consistent format to `pyproject.toml` files. -[Read the full documentation here](https://pyproject-fmt.readthedocs.io/en/latest/). +Apply a consistent format to the `pyproject.toml` files. -## add to pre-commit - -```yaml -- repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.0.3" - hooks: - - id: pyproject-fmt -``` +[For more information on how to use or configure read the documentation here](https://pyproject-fmt.readthedocs.io/en/latest/). diff --git a/docs/index.rst b/docs/index.rst index 4944d71..8bafabe 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,7 +12,7 @@ As a CLI tool Use `pipx `_ to install the project: -.. code-block:: bash +.. code-block:: shell pipx install pyproject-fmt @@ -25,18 +25,32 @@ See :gh:`pre-commit/pre-commit` for instructions, sample ``.pre-commit-config.ya .. code-block:: yaml - repo: https://github.com/tox-dev/pyproject-fmt - rev: "2.0.0" + rev: "2.0.4" hooks: - id: pyproject-fmt +Configuration via file +---------------------- -Calculating max supported Python version ----------------------------------------- +The ``tool.pyproject-fmt`` table is used when present in the ``pyproject.toml`` file: -This tool will automatically generate the ``Programming Language :: Python :: 3.X`` classifiers for you. To do so it -needs to know the range of Python interpreter versions you support. The lower bound can be deduced by looking -at the ``requires-python`` key in the ``pyproject.toml`` configuration file. The upper bound, by default, will -assume the latest stable release but can be changed via CLI flag or config. +.. code-block:: toml + + [tool.pyproject-fmt] + + # after how many column width split arrays/dicts into multiple lines, 1 will force always + column_width = 1 + + # how many spaces use for indentation + indent = 2 + + # if false will remove unnecessary trailing ``.0``'s from version specifiers + keep_full_version = false + + # maximum Python version to use when generating version specifiers + max_supported_python = "3.12" + +If not set they will default to values from the CLI, the example above shows the defaults. Command line interface ---------------------- @@ -46,17 +60,13 @@ Command line interface :prog: pyproject-fmt :title: -Configuration file ------------------- +Python version classifiers +-------------------------- -The ``tool.pyproject-fmt`` table is used when present in any of the ``pyproject.toml`` files - -.. code-block:: toml +This tool will automatically generate the ``Programming Language :: Python :: 3.X`` classifiers for you. To do so it +needs to know the range of Python interpreter versions you support: - # pyproject.toml - [tool.pyproject-fmt] - column_width = 120 - indent = 4 - keep_full_version = false - min_supported_python = "3.7" - max_supported_python = "3.10" +- The lower bound can be set via the ``requires-python`` key in the ``pyproject.toml`` configuration file (defaults to + the oldest non end of line CPython version at the time of the release). +- The upper bound, by default, will assume the latest stable release of CPython at the time of the release, but can be + changed via CLI flag or the config file. diff --git a/pyproject.toml b/pyproject.toml index 1f8c793..c944392 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ dynamic = [ "version", ] dependencies = [ - "pyproject-fmt-rust==1.0.4", + "pyproject-fmt-rust==1.0.6", "tomli>=2.0.1; python_version<'3.11'", ] optional-dependencies.docs = [ diff --git a/src/pyproject_fmt/cli.py b/src/pyproject_fmt/cli.py index aaa686b..273936b 100644 --- a/src/pyproject_fmt/cli.py +++ b/src/pyproject_fmt/cli.py @@ -34,7 +34,6 @@ class PyProjectFmtNamespace(Namespace): indent: int keep_full_version: bool max_supported_python: tuple[int, int] - min_supported_python: tuple[int, int] @dataclass(frozen=True) @@ -106,32 +105,29 @@ def _build_cli() -> ArgumentParser: group.add_argument("-s", "--stdout", action="store_true", help=msg) msg = "check and fail if any input would be formatted, printing any diffs" group.add_argument("--check", action="store_true", help=msg) - msg = "keep full dependency versions. For example do not change version 1.0.0 to 1" - parser.add_argument("--keep-full-version", action="store_true", help=msg) parser.add_argument( "--column-width", type=int, default=1, help="max column width in the file", + metavar="count", ) parser.add_argument( "--indent", type=int, default=2, help="number of spaces to indent", - ) - parser.add_argument( - "--min-supported-python", - type=_version_argument, - default=(3, 8), - help="latest Python version the project supports (e.g. 3.8)", + metavar="count", ) parser.add_argument( "--max-supported-python", + metavar="minor.major", type=_version_argument, default=(3, 12), help="latest Python version the project supports (e.g. 3.13)", ) + msg = "keep full dependency versions - do not remove redundant .0 from versions" + parser.add_argument("--keep-full-version", action="store_true", help=msg) msg = "pyproject.toml file(s) to format" parser.add_argument("inputs", nargs="+", type=pyproject_toml_path_creator, help=msg) return parser @@ -153,7 +149,6 @@ def cli_args(args: Sequence[str]) -> list[Config]: indent = opt.indent keep_full_version = opt.keep_full_version max_supported_python = opt.max_supported_python - min_supported_python = opt.min_supported_python with pyproject_toml.open("rb") as file_handler: config = tomllib.load(file_handler) if "tool" in config and "pyproject-fmt" in config["tool"]: @@ -166,8 +161,6 @@ def cli_args(args: Sequence[str]) -> list[Config]: keep_full_version = bool(entry) elif key == "max_supported_python": max_supported_python = _version_argument(entry) - elif key == "min_supported_python": # pragma: no branch - min_supported_python = _version_argument(entry) res.append( Config( pyproject_toml=pyproject_toml, @@ -178,7 +171,7 @@ def cli_args(args: Sequence[str]) -> list[Config]: indent=indent, keep_full_version=keep_full_version, max_supported_python=max_supported_python, - min_supported_python=min_supported_python, + min_supported_python=(3, 8), # default for when the user did not specify via requires-python ), ) ) diff --git a/tests/test_main.py b/tests/test_main.py index d87ccd1..59dec1a 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -212,8 +212,8 @@ def test_pyproject_toml_config(tmp_path: Path, capsys: pytest.CaptureFixture[str column_width = 20 indent = 4 keep_full_version = true - min_supported_python = "3.7" max_supported_python = "3.10" + ignore_extra = true """ filename = tmp_path / "pyproject.toml" filename.write_text(dedent(txt)) @@ -226,7 +226,6 @@ def test_pyproject_toml_config(tmp_path: Path, capsys: pytest.CaptureFixture[str ] classifiers = [ "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", @@ -242,8 +241,8 @@ def test_pyproject_toml_config(tmp_path: Path, capsys: pytest.CaptureFixture[str column_width = 20 indent = 4 keep_full_version = true - min_supported_python = "3.7" max_supported_python = "3.10" + ignore_extra = true """ got = filename.read_text() assert got == dedent(expected) diff --git a/tox.ini b/tox.ini index ad829bf..b574b8c 100644 --- a/tox.ini +++ b/tox.ini @@ -64,9 +64,11 @@ commands = description = build documentation extras = docs +set_env = + DOCS_OUT = {posargs:{toxworkdir}{/}docs_out} commands = - sphinx-build -d "{envtmpdir}{/}doctree" docs "{toxworkdir}{/}docs_out" --color -b html {posargs} - python -c 'print(r"documentation available under file://{toxworkdir}{/}docs_out{/}index.html")' + sphinx-build -d "{envtmpdir}{/}doctree" docs "{env:DOCS_OUT}" --color -b html + python -c 'print(r"documentation available under file://{env:DOCS_OUT}{/}index.html")' [testenv:dev] description = generate a DEV environment