Skip to content

Commit 847ac72

Browse files
[Python] Enforce code simplification if possible - some SIM rules (project-chip#42187)
* Enforce Python code simplification if possible (SIM) * Apply fixes for SIM118 * Apply fixes for SIM101 * Apply fixes for SIM103 * Apply fixes for SIM105 * Apply fixes for SIM109 * Restyle * Fold long lines * Restyled by autopep8 --------- Co-authored-by: Restyled.io <[email protected]>
1 parent 6fd991a commit 847ac72

File tree

70 files changed

+162
-198
lines changed

Some content is hidden

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

70 files changed

+162
-198
lines changed

build/gn_run_binary.py

100644100755
File mode changed.

examples/chef/sample_app_util/zap_file_parser.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
available.
3030
- Add support for .matter files.
3131
"""
32+
import contextlib
3233
import copy
3334
import json
3435
import os
@@ -101,15 +102,11 @@ def _read_value(input_string: str) -> str:
101102
elif "0x" in input_string:
102103
ret_val = int(input_string, 16)
103104
elif "." in input_string:
104-
try:
105+
with contextlib.suppress(ValueError):
105106
ret_val = float(input_string)
106-
except ValueError:
107-
pass
108107
else:
109-
try:
108+
with contextlib.suppress(ValueError):
110109
ret_val = int(input_string)
111-
except ValueError:
112-
pass
113110
return str(ret_val)
114111

115112

examples/lighting-app/python/lighting.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
import asyncio
18+
import contextlib
1819
import os
1920
import string
2021
import sys
@@ -87,10 +88,8 @@ def __init__(self, rendezvousAddr=None, controllerNodeId=0, bluetoothAdapter=Non
8788
if "libedit" in readline.__doc__:
8889
readline.parse_and_bind("bind ^I rl_complete")
8990
readline.set_completer_delims(" ")
90-
try:
91+
with contextlib.suppress(IOError):
9192
readline.read_history_file(self.historyFileName)
92-
except IOError:
93-
pass
9493
except ImportError:
9594
pass
9695

@@ -129,10 +128,8 @@ def postloop(self):
129128
try:
130129
import readline
131130

132-
try:
131+
with contextlib.suppress(IOError):
133132
readline.write_history_file(self.historyFileName)
134-
except IOError:
135-
pass
136133
except ImportError:
137134
pass
138135

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ select = [
3434
"COM",
3535
# Simplify the code if possible
3636
"C4", "RET",
37+
"SIM118", "SIM101", "SIM103", "SIM105", "SIM109",
3738
# Ensure that scripts are executable
3839
"EXE",
3940
# Check for common logging issues
@@ -46,6 +47,7 @@ ignore = [
4647
"ASYNC230", # Allow open() calls in async functions
4748
"COM812", # Do not enforce trailing commas in multi-line collections for now
4849
"LOG015", # Do not enforce non-root loggers in the entire codebase for now
50+
"SIM108", # Do not enforce ternary operator in place of if/else blocks
4951
"E501", # Do not report line length issues (formatter should handle this)
5052
"E721", # We use it for good reasons
5153
]

scripts/examples/gn_to_cmakelists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def WriteProject(project):
751751
posixpath.join(project.build_path, "empty.cpp")))
752752
out.write('")\n')
753753

754-
for target_name in project.targets.keys():
754+
for target_name in project.targets:
755755
out.write('\n')
756756
WriteTarget(out, Target(target_name, project), project)
757757

scripts/flashing/bouffalolab_firmware_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,14 @@ def decrypt_data(data_bytearray, key_bytearray, iv_bytearray):
329329
mfd_str = ""
330330
if dict_sec.keys():
331331
for idx in range(1, 1 + max(dict_sec.keys())):
332-
if idx in dict_sec.keys():
332+
if idx in dict_sec:
333333
mfd_str += dict_sec[idx] + ","
334334
else:
335335
mfd_str += ","
336336

337337
mfd_str = mfd_str + ":"
338338
for idx in range(0x8001, 1 + max(dict_raw.keys())):
339-
if idx in dict_raw.keys():
339+
if idx in dict_raw:
340340
mfd_str += dict_raw[idx] + ","
341341
else:
342342
mfd_str += ","

scripts/flashing/firmware_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def format_command(self, template, args=None, opt=None):
331331
↦ ρᵢ if opt[name]==σᵢ
332332
ρ otherwise
333333
"""
334-
if isinstance(template, str) or isinstance(template, pathlib.Path):
334+
if isinstance(template, (str, pathlib.Path)):
335335
result = [str(template).format_map(opt)]
336336
elif isinstance(template, list):
337337
result = []

scripts/pregenerate/using_codegen.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ def Accept(self, idl: InputIdlFile):
105105
return False
106106

107107
# we should not be checked for these, but verify just in case
108-
if '/tests/' in idl.relative_path:
109-
return False
110-
111-
return True
108+
return '/tests/' not in idl.relative_path
112109

113110
def CreateTarget(self, idl: InputIdlFile, runner):
114111
return CodegenTarget(sdk_root=self.sdk_root, idl=idl, generator="cpp-app", runner=runner)

scripts/pregenerate/using_zap.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def Accept(self, idl: InputIdlFile):
8686
return False
8787

8888
# FIXME: implement a proper check
89-
if 'test_files' in idl.relative_path:
90-
return False
91-
return True
89+
return 'test_files' not in idl.relative_path
9290

9391
def CreateTarget(self, idl: InputIdlFile, runner):
9492
# TODO: add additional arguments: tell how to invoke zap/codegen

scripts/py_matter_idl/matter/idl/generators/java/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,7 @@ def JavaAttributeCallbackName(attr: Attribute, context: TypeLookupContext) -> st
264264

265265
def IsFieldGlobalName(field: Field, context: TypeLookupContext) -> bool:
266266
global_name = FieldToGlobalName(field, context)
267-
if global_name:
268-
return True
269-
270-
return False
267+
return bool(global_name)
271268

272269

273270
def attributesWithSupportedCallback(attrs, context: TypeLookupContext):

0 commit comments

Comments
 (0)