Skip to content

Commit 1acaa9b

Browse files
authored
Add more ruff checks and use defaults (#2318)
* More ruff Signed-off-by: Bernát Gábor <[email protected]> * Handle test suite Signed-off-by: Bernát Gábor <[email protected]> * Format tests Signed-off-by: Bernát Gábor <[email protected]> * Fix config Signed-off-by: Bernát Gábor <[email protected]> * Automatic fixes Signed-off-by: Bernát Gábor <[email protected]> * Manual test fixes Signed-off-by: Bernát Gábor <[email protected]> * Batch 1 src fixes Signed-off-by: Bernát Gábor <[email protected]> * Fix type annotations Signed-off-by: Bernát Gábor <[email protected]> * Finish type checks Signed-off-by: Bernát Gábor <[email protected]> * Restore 3.8 support Signed-off-by: Bernát Gábor <[email protected]> * Add future imports Signed-off-by: Bernát Gábor <[email protected]> --------- Signed-off-by: Bernát Gábor <[email protected]>
1 parent 66e5876 commit 1acaa9b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+8710
-9293
lines changed

.github/workflows/test.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- tox_env: py3.12-black24
2727
- tox_env: py3.12-black23
2828
- tox_env: py3.12-black22
29-
- tox_env: py3.9-black19
29+
- tox_env: py3.8-black19
3030
- tox_env: py3.8-pydantic18
3131
- tox_env: py3.8-isort4
3232
runs-on: ${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os }}
@@ -38,6 +38,9 @@ jobs:
3838
fetch-depth: 0
3939
- name: Install the latest version of uv
4040
uses: astral-sh/setup-uv@v5
41+
with:
42+
enable-cache: true
43+
cache-suffix: "${{ matrix.py || matrix.tox_env }}"
4144
- name: Install tox
4245
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
4346
- name: Setup Python test environment

.pre-commit-config.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ repos:
1616
- repo: https://github.com/astral-sh/ruff-pre-commit
1717
rev: 'v0.9.6'
1818
hooks:
19-
- id: ruff
20-
files: "^datamodel_code_generator|^tests"
21-
exclude: "^tests/data"
22-
args: [ --fix ]
2319
- id: ruff-format
24-
files: "^datamodel_code_generator|^tests"
2520
exclude: "^tests/data"
21+
- id: ruff
22+
exclude: "^tests/data"
23+
args: ["--exit-non-zero-on-fix"]
2624
- repo: https://github.com/codespell-project/codespell
27-
# Configuration for codespell is in pyproject.toml
2825
rev: v2.4.1
2926
hooks:
3027
- id: codespell

pyproject.toml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test = [
8383
"pytest-cov>=5",
8484
"pytest-mock>=3.14",
8585
"pytest-xdist>=3.3.1",
86-
"setuptools; python_version<'3.9'",
86+
"setuptools; python_version<'3.10'", # PyCharm debugger needs it
8787
{ include-group = "coverage" },
8888
]
8989
type = [
@@ -123,13 +123,44 @@ version.source = "vcs"
123123
[tool.ruff]
124124
line-length = 120
125125
extend-exclude = [ "tests/data" ]
126-
format.indent-style = "space"
127-
format.quote-style = "single"
128-
format.line-ending = "auto"
129-
format.skip-magic-trailing-comma = false
130-
lint.extend-select = [ "C4", "I", "Q", "RUF100", "UP" ]
131-
lint.ignore = [ "E501", "Q000", "Q003", "UP006", "UP007" ]
132-
lint.flake8-quotes = { inline-quotes = 'single', multiline-quotes = 'double' }
126+
format.preview = true
127+
format.docstring-code-format = true
128+
lint.select = [
129+
"ALL",
130+
]
131+
lint.ignore = [
132+
"ANN401", # Any as type annotation is allowed
133+
"C901", # complex structure
134+
"COM812", # Conflict with formatter
135+
"CPY", # No copyright statements
136+
"D", # limited documentation
137+
"DOC", # limited documentation
138+
"FIX002", # line contains to do
139+
"ISC001", # Conflict with formatter
140+
"S101", # can use assert
141+
"TD002", # missing to do author
142+
"TD003", # missing to do link
143+
"TD004", # missing colon in to do
144+
]
145+
lint.per-file-ignores."tests/**/*.py" = [
146+
"FBT", # don't care about booleans as positional arguments in tests
147+
"INP001", # no implicit namespace
148+
"PLC2701", # private import is fine
149+
"PLR0913", # as many arguments as want
150+
"PLR0915", # can have longer test methods
151+
"PLR0917", # as many arguments as want
152+
"PLR2004", # Magic value used in comparison, consider replacing with a constant variable
153+
"S", # no safety concerns
154+
"SLF001", # can test private methods
155+
]
156+
lint.isort = { known-first-party = [
157+
"datamodel_code_generator",
158+
"tests",
159+
], required-imports = [
160+
"from __future__ import annotations",
161+
] }
162+
163+
lint.preview = true
133164

134165
[tool.codespell]
135166
skip = '.git,*.lock,tests'
@@ -150,6 +181,7 @@ filterwarnings = [
150181
"ignore:^.*`experimental string processing` has been included in `preview` and deprecated. Use `preview` instead..*",
151182
]
152183
norecursedirs = "tests/data/*"
184+
verbosity_assertions = 2
153185

154186
[tool.coverage]
155187
html.skip_covered = false
@@ -168,7 +200,7 @@ paths.other = [
168200
]
169201
run.dynamic_context = "none"
170202
run.omit = [ "tests/data/*" ]
171-
report.fail_under = 94
203+
report.fail_under = 88
172204
run.parallel = true
173205
run.plugins = [
174206
"covdefaults",
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1+
from __future__ import annotations # noqa: INP001
2+
13
import io
24
import os
35
import re
46
import sys
57
from pathlib import Path
68

7-
from datamodel_code_generator.__main__ import Exit, arg_parser
9+
from datamodel_code_generator.__main__ import Exit, arg_parser # noqa: PLC2701
810

9-
os.environ['COLUMNS'] = '94'
10-
os.environ['LINES'] = '24'
11+
os.environ["COLUMNS"] = "94"
12+
os.environ["LINES"] = "24"
1113

12-
START_MARK: str = '<!-- start command help -->'
13-
END_MARK: str = '<!-- end command help -->'
14-
BASH_CODE_BLOCK: str = '```bash'
15-
CODE_BLOCK_END: str = '```'
14+
START_MARK: str = "<!-- start command help -->"
15+
END_MARK: str = "<!-- end command help -->"
16+
BASH_CODE_BLOCK: str = "```bash"
17+
CODE_BLOCK_END: str = "```"
1618

1719
CURRENT_DIR = Path(__file__).parent
1820
PROJECT_DIR = CURRENT_DIR.parent
19-
DOC_DIR = PROJECT_DIR / 'docs'
21+
DOC_DIR = PROJECT_DIR / "docs"
2022

2123
TARGET_MARKDOWN_FILES: list[Path] = [
22-
DOC_DIR / 'index.md',
23-
PROJECT_DIR / 'README.md',
24+
DOC_DIR / "index.md",
25+
PROJECT_DIR / "README.md",
2426
]
2527

26-
REPLACE_MAP = {'(default: UTF-8)': '(default: utf-8)', "'": r"''"}
28+
REPLACE_MAP = {"(default: UTF-8)": "(default: utf-8)", "'": r"''"}
2729

2830

29-
def get_help():
31+
def get_help() -> str:
3032
with io.StringIO() as f:
3133
arg_parser.print_help(file=f)
3234
output = f.getvalue()
3335
for k, v in REPLACE_MAP.items():
3436
output = output.replace(k, v)
3537
# Remove any terminal codes
36-
return re.sub(r'\x1b\[[0-?]*[ -/]*[@-~]', '', output)
38+
return re.sub(r"\x1b\[[0-?]*[ -/]*[@-~]", "", output)
3739

3840

3941
def inject_help(markdown_text: str, help_text: str) -> str:
@@ -43,43 +45,40 @@ def inject_help(markdown_text: str, help_text: str) -> str:
4345
start_pos = markdown_text.find(START_MARK)
4446
end_pos = markdown_text.find(END_MARK)
4547
if start_pos == -1 or end_pos == -1:
46-
raise ValueError(f'Could not find {START_MARK} or {END_MARK} in markdown_text')
48+
msg = f"Could not find {START_MARK} or {END_MARK} in markdown_text"
49+
raise ValueError(msg)
4750
return (
4851
markdown_text[: start_pos + len(START_MARK)]
49-
+ '\n'
52+
+ "\n"
5053
+ BASH_CODE_BLOCK
51-
+ '\n'
54+
+ "\n"
5255
+ help_text
5356
+ CODE_BLOCK_END
54-
+ '\n'
57+
+ "\n"
5558
+ markdown_text[end_pos:]
5659
)
5760

5861

5962
def main() -> Exit:
6063
help_text = get_help()
6164
arg_parser.add_argument(
62-
'--validate',
63-
action='store_true',
64-
help='Validate the file content is up to date',
65+
"--validate",
66+
action="store_true",
67+
help="Validate the file content is up to date",
6568
)
6669
args = arg_parser.parse_args()
6770
validate: bool = args.validate
6871

6972
for file_path in TARGET_MARKDOWN_FILES:
70-
with file_path.open('r') as f:
73+
with file_path.open("r") as f:
7174
markdown_text = f.read()
7275
new_markdown_text = inject_help(markdown_text, help_text)
7376
if validate and new_markdown_text != markdown_text:
74-
print(
75-
f'{file_path} is not up to date. Run `python update_command_help_on_markdown.py`',
76-
file=sys.stderr,
77-
)
7877
return Exit.ERROR
79-
with file_path.open('w') as f:
78+
with file_path.open("w") as f:
8079
f.write(new_markdown_text)
8180
return Exit.OK
8281

8382

84-
if __name__ == '__main__':
83+
if __name__ == "__main__":
8584
sys.exit(main())

0 commit comments

Comments
 (0)