Skip to content

Commit

Permalink
Fix table ordering for sub tables and do not version strip on the ~ o…
Browse files Browse the repository at this point in the history
…perator (#210)
  • Loading branch information
gaborbernat authored May 13, 2024
1 parent 012e046 commit 388e1cf
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 60 deletions.
16 changes: 5 additions & 11 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
50 changes: 30 additions & 20 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ As a CLI tool

Use `pipx <https://pypa.github.io/pipx/installation/>`_ to install the project:

.. code-block:: bash
.. code-block:: shell
pipx install pyproject-fmt
Expand All @@ -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
----------------------
Expand All @@ -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.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
19 changes: 6 additions & 13 deletions src/pyproject_fmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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"]:
Expand All @@ -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,
Expand All @@ -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
),
)
)
Expand Down
5 changes: 2 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 388e1cf

Please sign in to comment.