Skip to content
This repository was archived by the owner on Oct 17, 2024. It is now read-only.

Commit 7f3b782

Browse files
committed
Pass configuration via separate class (#2)
Signed-off-by: Bernát Gábor <[email protected]>
1 parent f25874e commit 7f3b782

File tree

8 files changed

+121
-75
lines changed

8 files changed

+121
-75
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ repos:
2020
- id: tox-ini-fmt
2121
args: [ "-p", "fix" ]
2222
- repo: https://github.com/tox-dev/pyproject-fmt
23-
rev: "1.8.0"
23+
rev: "2.0.1"
2424
hooks:
2525
- id: pyproject-fmt
26-
additional_dependencies: [ "tox>=4.15" ]
2726
- repo: https://github.com/astral-sh/ruff-pre-commit
2827
rev: "v0.4.4"
2928
hooks:

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyproject-fmt-rust"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "Format pyproject.toml files"
55
repository = "https://github.com/tox-dev/pyproject-fmt"
66
readme = "README.md"

pyproject.toml

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ requires = [
66

77
[project]
88
name = "pyproject-fmt-rust"
9-
version = "1.0.0"
9+
version = "1.0.1"
1010
description = "Format your pyproject.toml file"
1111
readme = "README.md"
1212
keywords = [
@@ -15,7 +15,7 @@ keywords = [
1515
]
1616
license.file = "LICENSE.txt"
1717
authors = [
18-
{ name = "Bernat Gabor", email = "[email protected]" },
18+
{ name = "Bernat Gabor", email = "[email protected]" },
1919
]
2020
requires-python = ">=3.8"
2121
classifiers = [
@@ -51,35 +51,35 @@ module-name = "pyproject_fmt_rust._lib"
5151
python-source = "src"
5252
strip = true
5353
include = [
54-
"rust-toolchain.toml",
54+
"rust-toolchain.toml",
5555
]
5656

5757
[tool.cibuildwheel]
5858
skip = [
59-
"pp*",
60-
"*musl*",
59+
"pp*",
60+
"*musl*",
6161
]
6262

6363
[tool.ruff]
6464
line-length = 120
6565
target-version = "py38"
6666
lint.isort = { known-first-party = [
67-
"pyproject_fmt_rust",
67+
"pyproject_fmt_rust",
6868
], required-imports = [
69-
"from __future__ import annotations",
69+
"from __future__ import annotations",
7070
] }
7171
lint.select = [
72-
"ALL",
72+
"ALL",
7373
]
7474
lint.ignore = [
75-
"ANN101", # no type annotation for self
76-
"ANN401", # allow Any as type annotation
77-
"COM812", # Conflict with formatter
78-
"CPY", # No copyright statements
79-
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
80-
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
81-
"ISC001", # Conflict with formatter
82-
"S104", # Possible binding to all interface
75+
"ANN101", # no type annotation for self
76+
"ANN401", # allow Any as type annotation
77+
"COM812", # Conflict with formatter
78+
"CPY", # No copyright statements
79+
"D203", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
80+
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
81+
"ISC001", # Conflict with formatter
82+
"S104", # Possible binding to all interface
8383
]
8484
lint.preview = true
8585
format.preview = true
@@ -88,15 +88,15 @@ format.docstring-code-line-length = 100
8888

8989
[tool.ruff.lint.per-file-ignores]
9090
"tests/**/*.py" = [
91-
"D", # don"t care about documentation in tests
92-
"FBT", # don"t care about booleans as positional arguments in tests
93-
"INP001", # no implicit namespace
94-
"PLC2701", # private import
95-
"PLR0913", # any number of arguments in tests
96-
"PLR0917", # any number of arguments in tests
97-
"PLR2004", # Magic value used in comparison, consider replacing with a constant variable
98-
"S101", # asserts allowed in tests...
99-
"S603", # `subprocess` call: check for execution of untrusted input
91+
"D", # don"t care about documentation in tests
92+
"FBT", # don"t care about booleans as positional arguments in tests
93+
"INP001", # no implicit namespace
94+
"PLC2701", # private import
95+
"PLR0913", # any number of arguments in tests
96+
"PLR0917", # any number of arguments in tests
97+
"PLR2004", # Magic value used in comparison, consider replacing with a constant variable
98+
"S101", # asserts allowed in tests...
99+
"S603", # `subprocess` call: check for execution of untrusted input
100100
]
101101

102102
[tool.codespell]
@@ -105,25 +105,25 @@ count = true
105105

106106
[tool.pytest]
107107
ini_options.testpaths = [
108-
"tests",
108+
"tests",
109109
]
110110

111111
[tool.coverage]
112112
html.show_contexts = true
113113
html.skip_covered = false
114114
paths.source = [
115-
"src",
116-
".tox/*/.venv/lib/*/site-packages",
117-
".tox\\*\\.venv\\Lib\\site-packages",
118-
".tox/*/lib/*/site-packages",
119-
".tox\\*\\Lib\\site-packages",
120-
"**/src",
121-
"**\\src",
115+
"src",
116+
".tox/*/.venv/lib/*/site-packages",
117+
".tox\\*\\.venv\\Lib\\site-packages",
118+
".tox/*/lib/*/site-packages",
119+
".tox\\*\\Lib\\site-packages",
120+
"**/src",
121+
"**\\src",
122122
]
123123
report.fail_under = 100
124124
run.parallel = true
125125
run.plugins = [
126-
"covdefaults",
126+
"covdefaults",
127127
]
128128

129129
[tool.mypy]

rust/src/main.rs

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::string::String;
22

33
use pyo3::prelude::PyModule;
4-
use pyo3::{pyfunction, pymodule, wrap_pyfunction, Bound, PyResult};
4+
use pyo3::{pyclass, pyfunction, pymethods, pymodule, wrap_pyfunction, Bound, PyResult};
55
use taplo::formatter::{format_syntax, Options};
66
use taplo::parser::parse;
77

@@ -16,46 +16,69 @@ mod project;
1616
mod global;
1717
mod helpers;
1818

19-
/// Format toml file
20-
#[pyfunction]
21-
#[must_use]
22-
pub fn format_toml(
23-
content: &str,
19+
#[pyclass(frozen, get_all)]
20+
pub struct Settings {
2421
column_width: usize,
2522
indent: usize,
2623
keep_full_version: bool,
2724
max_supported_python: (u8, u8),
2825
min_supported_python: (u8, u8),
29-
) -> String {
26+
}
27+
28+
#[pymethods]
29+
impl Settings {
30+
#[new]
31+
#[pyo3(signature = (*, column_width, indent, keep_full_version, max_supported_python, min_supported_python ))]
32+
const fn new(
33+
column_width: usize,
34+
indent: usize,
35+
keep_full_version: bool,
36+
max_supported_python: (u8, u8),
37+
min_supported_python: (u8, u8),
38+
) -> Self {
39+
Self {
40+
column_width,
41+
indent,
42+
keep_full_version,
43+
max_supported_python,
44+
min_supported_python,
45+
}
46+
}
47+
}
48+
49+
/// Format toml file
50+
#[must_use]
51+
#[pyfunction]
52+
pub fn format_toml(content: &str, opt: &Settings) -> String {
3053
let root_ast = parse(content).into_syntax().clone_for_update();
3154
let mut tables = Tables::from_ast(&root_ast);
3255

33-
fix_build(&mut tables, keep_full_version);
56+
fix_build(&mut tables, opt.keep_full_version);
3457
fix_project_table(
3558
&mut tables,
36-
keep_full_version,
37-
max_supported_python,
38-
min_supported_python,
59+
opt.keep_full_version,
60+
opt.max_supported_python,
61+
opt.min_supported_python,
3962
);
4063
reorder_tables(&root_ast, &mut tables);
4164

4265
let options = Options {
43-
align_entries: false, // do not align by =
44-
align_comments: true, // align inline comments
45-
align_single_comments: true, // align comments after entries
46-
array_trailing_comma: true, // ensure arrays finish with trailing comma
47-
array_auto_expand: true, // arrays go to multi line for easier diffs
48-
array_auto_collapse: false, // do not collapse for easier diffs
49-
compact_arrays: false, // do not compact for easier diffs
50-
compact_inline_tables: false, // do not compact for easier diffs
51-
compact_entries: false, // do not compact for easier diffs
52-
column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390
66+
align_entries: false, // do not align by =
67+
align_comments: true, // align inline comments
68+
align_single_comments: true, // align comments after entries
69+
array_trailing_comma: true, // ensure arrays finish with trailing comma
70+
array_auto_expand: true, // arrays go to multi line for easier diffs
71+
array_auto_collapse: false, // do not collapse for easier diffs
72+
compact_arrays: false, // do not compact for easier diffs
73+
compact_inline_tables: false, // do not compact for easier diffs
74+
compact_entries: false, // do not compact for easier diffs
75+
column_width: opt.column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390
5376
indent_tables: false,
5477
indent_entries: false,
5578
inline_table_expand: true,
5679
trailing_newline: true,
5780
allowed_blank_lines: 1, // one blank line to separate
58-
indent_string: " ".repeat(indent),
81+
indent_string: " ".repeat(opt.indent),
5982
reorder_keys: false, // respect custom order
6083
reorder_arrays: false, // for natural sorting we need to this ourselves
6184
crlf: false,
@@ -71,6 +94,7 @@ pub fn format_toml(
7194
#[cfg(not(tarpaulin_include))]
7295
pub fn _lib(m: &Bound<'_, PyModule>) -> PyResult<()> {
7396
m.add_function(wrap_pyfunction!(format_toml, m)?)?;
97+
m.add_class::<Settings>()?;
7498
Ok(())
7599
}
76100

@@ -79,7 +103,7 @@ mod tests {
79103
use indoc::indoc;
80104
use rstest::rstest;
81105

82-
use crate::format_toml;
106+
use crate::{format_toml, Settings};
83107

84108
#[rstest]
85109
#[case::simple(
@@ -160,7 +184,16 @@ mod tests {
160184
#[case] keep_full_version: bool,
161185
#[case] max_supported_python: (u8, u8),
162186
) {
163-
let got = format_toml(start, 1, indent, keep_full_version, max_supported_python, (3, 8));
187+
let got = format_toml(
188+
start,
189+
&Settings {
190+
column_width: 1,
191+
indent,
192+
keep_full_version,
193+
max_supported_python,
194+
min_supported_python: (3, 8),
195+
},
196+
);
164197
assert_eq!(got, expected);
165198
}
166199
}

src/pyproject_fmt_rust/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from __future__ import annotations
44

5-
from ._lib import format_toml
5+
from ._lib import Settings, format_toml
66

77
__all__ = [
8+
"Settings",
89
"format_toml",
910
]

src/pyproject_fmt_rust/_lib.pyi

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
def format_toml( # noqa: PLR0913
2-
content: str,
3-
*,
4-
column_width: int,
5-
indent: int,
6-
keep_full_version: bool,
7-
max_supported_python: tuple[int, int],
8-
min_supported_python: tuple[int, int],
9-
) -> str: ...
1+
class Settings:
2+
def __init__( # noqa: PLR0913
3+
self,
4+
*,
5+
column_width: int,
6+
indent: int,
7+
keep_full_version: bool,
8+
max_supported_python: tuple[int, int],
9+
min_supported_python: tuple[int, int],
10+
) -> None: ...
11+
@property
12+
def column_width(self) -> int: ...
13+
@property
14+
def indent(self) -> int: ...
15+
@property
16+
def keep_full_version(self) -> bool: ...
17+
@property
18+
def max_supported_python(self) -> tuple[int, int]: ...
19+
@property
20+
def min_supported_python(self) -> tuple[int, int]: ...
21+
22+
def format_toml(content: str, settings: Settings) -> str: ...

tests/test_main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from textwrap import dedent
44

5-
from pyproject_fmt_rust import format_toml
5+
from pyproject_fmt_rust import Settings, format_toml
66

77

88
def test_format_toml() -> None:
@@ -22,14 +22,14 @@ def test_format_toml() -> None:
2222
]
2323
"""
2424

25-
res = format_toml(
26-
dedent(txt),
25+
settings = Settings(
2726
column_width=120,
2827
indent=4,
2928
keep_full_version=True,
3029
min_supported_python=(3, 7),
3130
max_supported_python=(3, 8),
3231
)
32+
res = format_toml(dedent(txt), settings)
3333

3434
expected = """\
3535
[project]

0 commit comments

Comments
 (0)