Skip to content

Commit 3f8008b

Browse files
committed
ruff: create config file and run auto format & fixes
1 parent 972edd7 commit 3f8008b

Some content is hidden

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

43 files changed

+672
-261
lines changed

.github/workflows/unit-tests.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,29 @@ on:
99
branches: [ main ]
1010

1111
jobs:
12+
lint:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Install Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: "3.12"
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install ruff
24+
- name: Ruff linter
25+
run: ruff check --output-format=github .
26+
- name: Ruff formatter
27+
run: ruff format --check --diff .
1228
test:
1329
strategy:
1430
fail-fast: false
1531
matrix:
1632
include:
1733
- os: 'windows-latest'
1834
python-version: '3.9'
19-
rf-version: 'rf5'
20-
- os: 'ubuntu-latest'
21-
python-version: '3.8'
2235
rf-version: 'rf4'
2336
- os: 'ubuntu-latest'
2437
python-version: '3.9'

.pre-commit-config.yaml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
repos:
2-
- repo: https://github.com/pycqa/isort
3-
rev: 5.11.5
4-
hooks:
5-
- id: isort
6-
name: isort (python)
7-
8-
- repo: https://github.com/psf/black
9-
rev: 22.3.0
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.6.9
105
hooks:
11-
- id: black
6+
#- id: ruff
7+
- id: ruff-format

pyproject.toml

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
1-
[tool.black]
1+
[tool.ruff]
22
line-length = 120
3-
[tool.isort]
4-
profile = "black"
5-
line_length = 120
3+
show-fixes = true
4+
target-version = "py39" # TODO verify min Python version
5+
6+
lint.select = [
7+
"ALL", # include all the rules, including new ones
8+
]
9+
exclude = [
10+
"tests/utest/testdata/"
11+
]
12+
lint.ignore = [
13+
#### modules
14+
"ANN", # flake8-annotations
15+
"COM", # flake8-commas
16+
"C90", # mccabe complexity
17+
"DJ", # django
18+
"EXE", # flake8-executable
19+
"PTH", # flake8-use-pathlib
20+
"T10", # debugger
21+
"TID", # flake8-tidy-imports
22+
#### specific rules
23+
"D100", # ignore missing docs
24+
"D101",
25+
"D102",
26+
"D103",
27+
"D104",
28+
"D105",
29+
"D106",
30+
"D107",
31+
"D200",
32+
"D203", # blank line before class body
33+
"D205",
34+
"D212",
35+
"D400",
36+
"D401",
37+
"D415",
38+
"E402", # false positives for local imports
39+
"E722", # bare except
40+
"B904", # Raise from None
41+
"EM101", # No string-literal exceptions !?
42+
"EM102", # No Exceptions with f-strings
43+
"PLR0913", # too many arguments to function call
44+
"TRY003", # external messages in exceptions are too verbose
45+
"TD002",
46+
"TD003",
47+
"FIX002", # too verbose descriptions of todos
48+
"PLE1205", # too many arguments for logging
49+
"T201", # print in code
50+
]
51+
52+
[tool.ruff.format]
53+
quote-style = "double"
54+
skip-magic-trailing-comma = false
55+
line-ending = "auto"

robotidy/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Methods for transforming Robot Framework ast model programmatically.
33
"""
4+
45
from __future__ import annotations
56

67
from pathlib import Path
@@ -37,7 +38,8 @@ def transform_model(model, root_dir: str, output: str | None = None, **kwargs) -
3738
"""
3839
robotidy_class = get_robotidy(root_dir, output, **kwargs)
3940
disabler_finder = disablers.RegisterDisablers(
40-
robotidy_class.config.formatting.start_line, robotidy_class.config.formatting.end_line
41+
robotidy_class.config.formatting.start_line,
42+
robotidy_class.config.formatting.end_line,
4143
)
4244
disabler_finder.visit(model)
4345
if disabler_finder.is_disabled_in_file(disablers.ALL_TRANSFORMERS):

robotidy/app.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def transform_files(self):
6161
self.output_diff(model_path, old_model, new_model)
6262
changed_files += 1
6363
except DataError as err:
64-
click.echo(f"Failed to decode {source} with an error: {err}\nSkipping file", err=True)
64+
click.echo(
65+
f"Failed to decode {source} with an error: {err}\nSkipping file",
66+
err=True,
67+
)
6568
changed_files = previous_changed_files
6669
skipped_files += 1
6770
return self.formatting_result(all_files, changed_files, skipped_files, stdin)
@@ -109,7 +112,7 @@ def transform_until_stable(self, model, disabler_finder):
109112
def transform(self, model, disablers):
110113
old_model = misc.StatementLinesCollector(model)
111114
for transformer in self.config.transformers:
112-
setattr(transformer, "disablers", disablers) # set dynamically to allow using external transformers
115+
transformer.disablers = disablers # set dynamically to allow using external transformers
113116
if disablers.is_disabled_in_file(transformer.__class__.__name__):
114117
continue
115118
transformer.visit(model)
@@ -137,11 +140,15 @@ def get_line_ending(self, path: str):
137140
return os.linesep
138141
if isinstance(f.newlines, str):
139142
return f.newlines
140-
else:
141-
return f.newlines[0]
143+
return f.newlines[0]
142144
return self.config.formatting.line_sep
143145

144-
def output_diff(self, path: str, old_model: misc.StatementLinesCollector, new_model: misc.StatementLinesCollector):
146+
def output_diff(
147+
self,
148+
path: str,
149+
old_model: misc.StatementLinesCollector,
150+
new_model: misc.StatementLinesCollector,
151+
):
145152
if not self.config.show_diff:
146153
return
147154
old = [l + "\n" for l in old_model.text.splitlines()]

robotidy/cli.py

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

33
import sys
44
from pathlib import Path
5-
from typing import Pattern
5+
from re import Pattern
66

77
try:
88
import rich_click as click
@@ -13,12 +13,15 @@
1313

1414
RICH_PRESENT = False
1515

16-
from robotidy import app
16+
from robotidy import app, decorators, exceptions, files, skip, version
1717
from robotidy import config as config_module
18-
from robotidy import decorators, exceptions, files, skip, version
1918
from robotidy.config import RawConfig, csv_list_type, validate_target_version
2019
from robotidy.rich_console import console
21-
from robotidy.transformers import TransformConfigMap, TransformConfigParameter, load_transformers
20+
from robotidy.transformers import (
21+
TransformConfigMap,
22+
TransformConfigParameter,
23+
load_transformers,
24+
)
2225
from robotidy.utils import misc
2326

2427
CLI_OPTIONS_LIST = [
@@ -55,7 +58,10 @@
5558
"--endline",
5659
],
5760
},
58-
{"name": "File exclusion", "options": ["--exclude", "--extend-exclude", "--skip-gitignore"]},
61+
{
62+
"name": "File exclusion",
63+
"options": ["--exclude", "--extend-exclude", "--skip-gitignore"],
64+
},
5965
skip.option_group,
6066
{
6167
"name": "Other",
@@ -124,7 +130,11 @@ def print_transformer_docs(transformer):
124130
@decorators.optional_rich
125131
def print_description(name: str, target_version: int):
126132
# TODO: --desc works only for default transformers, it should also print custom transformer desc
127-
transformers = load_transformers(TransformConfigMap([], [], []), allow_disabled=True, target_version=target_version)
133+
transformers = load_transformers(
134+
TransformConfigMap([], [], []),
135+
allow_disabled=True,
136+
target_version=target_version,
137+
)
128138
transformer_by_names = {transformer.name: transformer for transformer in transformers}
129139
if name == "all":
130140
for transformer in transformers:
@@ -159,7 +169,11 @@ def print_transformers_list(global_config: config_module.MainConfig):
159169
table = Table(title="Transformers", header_style="bold red")
160170
table.add_column("Name", justify="left", no_wrap=True)
161171
table.add_column("Enabled")
162-
transformers = load_transformers(TransformConfigMap([], [], []), allow_disabled=True, target_version=target_version)
172+
transformers = load_transformers(
173+
TransformConfigMap([], [], []),
174+
allow_disabled=True,
175+
target_version=target_version,
176+
)
163177
transformers.extend(_load_external_transformers(transformers, config.transformers_config, target_version))
164178

165179
for transformer in transformers:
@@ -194,7 +208,11 @@ def generate_config(global_config: config_module.MainConfig):
194208
raise exceptions.MissingOptionalTomliWDependencyError()
195209
target_version = global_config.default.target_version
196210
config = global_config.default_loaded
197-
transformers = load_transformers(TransformConfigMap([], [], []), allow_disabled=True, target_version=target_version)
211+
transformers = load_transformers(
212+
TransformConfigMap([], [], []),
213+
allow_disabled=True,
214+
target_version=target_version,
215+
)
198216
transformers.extend(_load_external_transformers(transformers, config.transformers_config, target_version))
199217

200218
toml_config = {

robotidy/config.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import namedtuple
99
from dataclasses import dataclass, field
1010
from pathlib import Path
11-
from typing import Pattern
11+
from re import Pattern
1212

1313
try:
1414
from robot.api import Languages # RF 6.0
@@ -19,7 +19,12 @@
1919
from click.core import ParameterSource
2020

2121
from robotidy import exceptions, files, skip
22-
from robotidy.transformers import TransformConfig, TransformConfigMap, convert_transform_config, load_transformers
22+
from robotidy.transformers import (
23+
TransformConfig,
24+
TransformConfigMap,
25+
convert_transform_config,
26+
load_transformers,
27+
)
2328
from robotidy.utils import misc
2429

2530

@@ -60,12 +65,11 @@ def __init__(
6065
def get_line_sep(line_sep):
6166
if line_sep == "windows":
6267
return "\r\n"
63-
elif line_sep == "unix":
68+
if line_sep == "unix":
6469
return "\n"
65-
elif line_sep == "auto":
70+
if line_sep == "auto":
6671
return "auto"
67-
else:
68-
return os.linesep
72+
return os.linesep
6973

7074

7175
def validate_target_version(value: str | None) -> int | None:
@@ -98,7 +102,12 @@ def convert_transformers_config(
98102
is_config: bool = False,
99103
) -> list[TransformConfig]:
100104
return [
101-
TransformConfig(tr, force_include=force_included, custom_transformer=custom_transformer, is_config=is_config)
105+
TransformConfig(
106+
tr,
107+
force_include=force_included,
108+
custom_transformer=custom_transformer,
109+
is_config=is_config,
110+
)
102111
for tr in config.get(param_name, ())
103112
]
104113

@@ -180,13 +189,17 @@ def from_cli(cls, ctx: click.Context, **kwargs):
180189
defined_in_cli.add(option)
181190
return cls(**kwargs, defined_in_cli=defined_in_cli)
182191

183-
def from_config_file(self, config: dict, config_path: Path) -> "RawConfig":
184-
"""Creates new RawConfig instance from dictionary.
192+
def from_config_file(self, config: dict, config_path: Path) -> RawConfig:
193+
"""
194+
Creates new RawConfig instance from dictionary.
185195
186196
Dictionary key:values needs to be normalized and parsed to correct types.
187197
"""
188198
options_map = map_class_fields_with_their_types(self)
189-
parsed_config = {"defined_in_config": {"defined_in_config", "config_path"}, "config_path": config_path}
199+
parsed_config = {
200+
"defined_in_config": {"defined_in_config", "config_path"},
201+
"config_path": config_path,
202+
}
190203
for key, value in config.items():
191204
# workaround to be able to use two option names for same action - backward compatibility change
192205
if key == "load_transformers":
@@ -206,16 +219,20 @@ def from_config_file(self, config: dict, config_path: Path) -> "RawConfig":
206219
parsed_config[key] = [convert_transform_config(val, key) for val in value]
207220
elif key == "src":
208221
parsed_config[key] = tuple(value)
209-
elif value_type in ("Pattern", Pattern): # future typing for 3.8 provides type as str
222+
elif value_type in (
223+
"Pattern",
224+
Pattern,
225+
): # future typing for 3.8 provides type as str
210226
parsed_config[key] = misc.validate_regex(value)
211227
else:
212228
parsed_config[key] = value
213229
parsed_config["defined_in_config"].add(key)
214230
from_config = RawConfig(**parsed_config)
215231
return self.merge_with_config_file(from_config)
216232

217-
def merge_with_config_file(self, config: "RawConfig") -> "RawConfig":
218-
"""Merge cli config with the configuration file config.
233+
def merge_with_config_file(self, config: RawConfig) -> RawConfig:
234+
"""
235+
Merge cli config with the configuration file config.
219236
220237
Use configuration file parameter value only if it was not defined in the cli already.
221238
"""
@@ -253,7 +270,8 @@ def load_config_from_option(cli_config: RawConfig) -> RawConfig:
253270
return cli_config
254271

255272
def get_sources(self, sources: tuple[str, ...]) -> tuple[str, ...] | None:
256-
"""Get list of sources to be transformed by Robotidy.
273+
"""
274+
Get list of sources to be transformed by Robotidy.
257275
258276
If the sources tuple is empty, look for most common configuration file and load sources from there.
259277
"""
@@ -273,7 +291,10 @@ def get_sources(self, sources: tuple[str, ...]) -> tuple[str, ...] | None:
273291

274292
def get_sources_with_configs(self):
275293
sources = files.get_paths(
276-
self.sources, self.default.exclude, self.default.extend_exclude, self.default.skip_gitignore
294+
self.sources,
295+
self.default.exclude,
296+
self.default.extend_exclude,
297+
self.default.skip_gitignore,
277298
)
278299
for source in sources:
279300
if self.default.config:
@@ -350,7 +371,7 @@ def set_color_mode(color: bool) -> bool:
350371
return "NO_COLOR" not in os.environ
351372

352373
@classmethod
353-
def from_raw_config(cls, raw_config: "RawConfig"):
374+
def from_raw_config(cls, raw_config: RawConfig):
354375
skip_config = skip.SkipConfig(
355376
documentation=raw_config.skip_documentation,
356377
return_values=raw_config.skip_return_values,
@@ -416,8 +437,8 @@ def load_transformers(self, transformers_config: TransformConfigMap, force_order
416437
)
417438
for transformer in transformers:
418439
# inject global settings TODO: handle it better
419-
setattr(transformer.instance, "formatting_config", self.formatting)
420-
setattr(transformer.instance, "transformers", self.transformers_lookup)
421-
setattr(transformer.instance, "languages", self.language)
440+
transformer.instance.formatting_config = self.formatting
441+
transformer.instance.transformers = self.transformers_lookup
442+
transformer.instance.languages = self.language
422443
self.transformers.append(transformer.instance)
423444
self.transformers_lookup[transformer.name] = transformer.instance

0 commit comments

Comments
 (0)