Skip to content

Commit 411f559

Browse files
author
Robert Roos
committed
Fixed new ruff formatting
1 parent b0a580b commit 411f559

File tree

11 files changed

+48
-39
lines changed

11 files changed

+48
-39
lines changed

src/tctools/common.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def __init__(self, *args):
5252

5353
# All the fields were are going to have in the arguments output:
5454
fields = {
55-
action.dest for action in parser._actions if action.dest != "help" # noqa
55+
action.dest
56+
for action in parser._actions
57+
if action.dest != "help" # noqa
5658
}
5759

5860
self.config_file: Path | None = None
@@ -243,7 +245,7 @@ def set_main_argument(cls, parser):
243245
@staticmethod
244246
def get_xml_header(file: str) -> str | None:
245247
"""Get raw XML header as string."""
246-
with open(file, "r") as fh:
248+
with open(file) as fh:
247249
# Search only the start of the path, otherwise give up
248250
for _ in range(100):
249251
line = fh.readline()

src/tctools/format/format_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def format_file(self, path: str):
148148
The path is read as text and code inside XML tags is detected manually. Other
149149
lines of XML remain untouched.
150150
"""
151-
with open(path, "r", encoding="utf-8", errors="ignore", newline="") as fh:
151+
with open(path, encoding="utf-8", errors="ignore", newline="") as fh:
152152
content = fh.readlines()
153153

154154
self._file = path
@@ -211,7 +211,7 @@ def split_code_segments(content: list[str]):
211211

212212
# Iterate over pairs of regions so we got the start and end together
213213
for (rowcol_prev, kind_prev, name_prev), (rowcol, _, _) in zip(
214-
regions[:-1], regions[1:]
214+
regions[:-1], regions[1:], strict=False
215215
):
216216
lines = content[rowcol_prev[0] : (rowcol[0] + 1)] # Inclusive range
217217

src/tctools/format/format_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def get_property(
7474
if value_type is None:
7575
return value # Unprocessed
7676

77-
if value_type == bool:
77+
if value_type is bool:
7878
if isinstance(value, str):
7979
return value in ["TRUE", "True", "true", "1"]
8080
return bool(value)
@@ -324,7 +324,7 @@ def format_argument_list(self, content: list[str]):
324324

325325
for i, line_chunks in variable_definitions.items():
326326
new_line = ""
327-
for chunk, indent in zip(line_chunks, chunk_indent_levels):
327+
for chunk, indent in zip(line_chunks, chunk_indent_levels, strict=True):
328328
if chunk:
329329
if indent > 0:
330330
new_line += self._pad_to_indent_level(new_line, indent)

src/tctools/patch_plc/patch_plc_class.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def determine_source_folders(self, source_files: Iterable[Path]) -> FileItems:
295295
raise ValueError(
296296
f"Source file `{file}` is not relative to project "
297297
f"directory `{project_dir}`"
298-
)
298+
) from None
299299
else:
300300
if relative_path.name in self.args.ignore:
301301
continue # Skip this file if the name matches exactly
@@ -356,53 +356,57 @@ def sources_to_remove(
356356
"""
357357
to_remove = FileItems()
358358

359-
remove_items = [to_remove.files]
360-
sources_items = [current.files]
361-
if self.args.recursive: # Don't touch folders without `-r`
362-
remove_items.append(to_remove.folders)
363-
sources_items.append(current.folders)
364-
365359
project_folder = self._project_file.parent
366360

367-
for remove_list, sources_list in zip(remove_items, sources_items):
361+
def remove_helper(remove_list, sources_list):
368362
for item in sources_list:
369-
for target in new_sources.keys():
363+
for target in new_sources:
370364
if target.is_absolute():
371365
target = target.relative_to(project_folder)
372366
if item == target or (
373367
self.args.recursive and item.is_relative_to(target)
374368
): # Exact match without `-r`, also relative with
375369
remove_list.add(item)
376370

371+
remove_helper(to_remove.files, current.files)
372+
if self.args.recursive: # Don't touch folders without `-r`
373+
remove_helper(to_remove.folders, current.folders)
374+
377375
return to_remove
378376

379377
def xml_add_sources(self, sources: FileItems):
380378
"""Modify the files and folders elements in-place."""
381-
for ref_elements, ref_set, template in zip(
382-
[self._element_folders, self._element_files],
383-
[sources.folders, sources.files],
384-
[
385-
'<Folder Include="{}"/>',
386-
'<Compile Include="{}"><SubType>Code</SubType></Compile>',
387-
],
388-
): # Repeat for folders and then for files
379+
380+
def add_helper(ref_elements, ref_set, template):
389381
for item in ref_set:
390382
item_str = self.path_to_str(item)
391383
xml = template.format(item_str)
392384
ref_elements.append(etree.XML(xml))
393385

386+
add_helper(
387+
self._element_folders,
388+
sources.folders,
389+
'<Folder Include="{}"/>', # ...
390+
)
391+
add_helper(
392+
self._element_files,
393+
sources.files,
394+
'<Compile Include="{}"><SubType>Code</SubType></Compile>',
395+
)
396+
394397
def xml_remove_source(self, to_remove: FileItems):
395398
"""Modify the files and folders elements in-place."""
396-
for ref_elements, ref_set in zip(
397-
[self._element_folders, self._element_files],
398-
[to_remove.folders, to_remove.files],
399-
): # Repeat for folders and then for files
399+
400+
def remove_helper(ref_elements, ref_set):
400401
for element in ref_elements:
401402
this_path = Path(PureWindowsPath(element.attrib["Include"]))
402403
# Force XML Windows path to native one
403404
if this_path in ref_set:
404405
ref_elements.remove(element)
405406

407+
remove_helper(self._element_folders, to_remove.folders)
408+
remove_helper(self._element_files, to_remove.files)
409+
406410
@staticmethod
407411
def path_to_str(path: PurePath) -> str:
408412
"""Turn any path into a windows-path string.

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def check_line(expected_line: str, line_to_check: str) -> bool:
3939
return expected_line in line_to_check
4040
return expected[idx].strip() == line.strip()
4141

42-
with open(file, "r") as fh:
42+
with open(file) as fh:
4343
count = 0
4444
while True:
4545
count += 1
@@ -58,9 +58,9 @@ def check_line(expected_line: str, line_to_check: str) -> bool:
5858
if check_line(ex_line, line):
5959
assert not check_true
6060

61-
assert (
62-
idx == len(expected)
63-
) == check_true, "Did not encounter right number of expected lines"
61+
assert (idx == len(expected)) == check_true, (
62+
"Did not encounter right number of expected lines"
63+
)
6464

6565

6666
def assert_strings_have_substrings(expected: list[list[str]], actual: list[str]):

tests/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_cli_version(self, capsys):
5252
with pytest.raises(SystemExit) as err:
5353
_ = MyTool("--version")
5454

55-
assert err.type == SystemExit
55+
assert err.type is SystemExit
5656

5757
message = capsys.readouterr().out
5858
assert message

tests/test_formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_help(capsys):
1414
with pytest.raises(SystemExit) as err:
1515
tctools.format.__main__.main("--help")
1616

17-
assert err.type == SystemExit
17+
assert err.type is SystemExit
1818

1919
message = capsys.readouterr().out
2020
assert "usage:" in message

tests/test_git_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_help(capsys):
1414
with pytest.raises(SystemExit) as err:
1515
tctools.git_info.__main__.main("--help")
1616

17-
assert err.type == SystemExit
17+
assert err.type is SystemExit
1818

1919
message = capsys.readouterr().out
2020
assert "usage:" in message

tests/test_make_release.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_help(capsys):
4141
with pytest.raises(SystemExit) as err:
4242
tctools.make_release.__main__.main("--help")
4343

44-
assert err.type == SystemExit
44+
assert err.type is SystemExit
4545

4646
message = capsys.readouterr().out
4747
assert "usage: " in message

tests/test_patch_plc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_help(capsys):
2525
with pytest.raises(SystemExit) as err:
2626
patch_plc_main("--help")
2727

28-
assert err.type == SystemExit
28+
assert err.type is SystemExit
2929

3030
message = capsys.readouterr().out
3131
assert "usage:" in message
@@ -59,9 +59,12 @@ def test_merge_single_file(plc_code):
5959

6060
project_content = project.read_text()
6161

62-
assert """<Compile Include="POUs\\untracked_source\\F_UntrackedFunc.TcPOU">
62+
assert (
63+
"""<Compile Include="POUs\\untracked_source\\F_UntrackedFunc.TcPOU">
6364
<SubType>Code</SubType>
64-
</Compile>""" in project_content
65+
</Compile>"""
66+
in project_content
67+
)
6568

6669
assert '<Folder Include="POUs\\untracked_source"/>' in project_content
6770

0 commit comments

Comments
 (0)