Skip to content

Commit 3b1c825

Browse files
authored
[Python] Add ruff as Python linter (project-chip#32384)
* Add ruff as Python linter * Add ruff to linter workflow * Remove flake8 * Set default line-length and explicitly select rules * Address REPL Test issue * Explicitly set line lenght for autopep8 * Fix restyled config
1 parent d166650 commit 3b1c825

File tree

14 files changed

+37
-26
lines changed

14 files changed

+37
-26
lines changed

.flake8

-6
This file was deleted.

.github/workflows/lint.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
if: github.actor != 'restyled-io[bot]'
3030

3131
container:
32-
image: ghcr.io/project-chip/chip-build:35
32+
image: ghcr.io/project-chip/chip-build:39
3333

3434
steps:
3535
- name: Checkout
@@ -267,12 +267,11 @@ jobs:
267267
run: |
268268
git grep -I -n 'emberAfWriteAttribute' -- './*' ':(exclude).github/workflows/lint.yml' ':(exclude)src/app/util/af.h' ':(exclude)zzz_generated/app-common/app-common/zap-generated/attributes/Accessors.cpp' ':(exclude)src/app/zap-templates/templates/app/attributes/Accessors-src.zapt' ':(exclude)src/app/util/attribute-table.cpp' ':(exclude)examples/common/pigweed/rpc_services/Attributes.h' ':(exclude)src/app/util/attribute-table.h' ':(exclude)src/app/util/ember-compatibility-functions.cpp' && exit 1 || exit 0
269269
270-
# Run python Linter (flake8) and verify python files
271-
# ignore some style errors, restyler should do that
272-
- name: Check for errors using flake8 Python linter
270+
# Run ruff python linter
271+
- name: Check for errors using ruff Python linter
273272
if: always()
274273
run: |
275-
flake8 --extend-ignore=E501,W391
274+
ruff check
276275
277276
# git grep exits with 0 if it finds a match, but we want
278277
# to fail (exit nonzero) on match. And we want to exclude this file,

.restyled.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ restylers:
230230
command:
231231
- autopep8
232232
- "--in-place"
233+
- "--max-line-length"
234+
- "132"
233235
arguments: []
234236
include:
235237
- "**/*.py"

integrations/docker/images/base/chip-build/Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ RUN set -x \
144144
click \
145145
coloredlogs \
146146
cxxfilt \
147-
flake8 \
148147
future \
149148
ghapi \
150149
mobly \

ruff.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exclude = [
2+
".environment",
3+
".git",
4+
".github",
5+
".*",
6+
"build",
7+
"out",
8+
"third_party",
9+
"examples/common/QRCode/",
10+
]
11+
target-version = "py37"
12+
13+
line-length = 132
14+
15+
[lint]
16+
select = ["E4", "E7", "E9", "F"]
17+
ignore = [
18+
"E721" # We use is for good reasons
19+
]

scripts/py_matter_idl/matter_idl/generators/java/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Optional[str]
114114
'event_no': 'chip::EventNumber',
115115
'fabric_id': 'chip::FabricId',
116116
'fabric_idx': 'chip::FabricIndex',
117-
'fabric_idx': 'chip::FabricIndex',
118117
'field_id': 'chip::FieldId',
119118
'group_id': 'chip::GroupId',
120119
'node_id': 'chip::NodeId',

scripts/py_matter_idl/matter_idl/generators/kotlin/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Optional[str]
114114
'event_no': 'chip::EventNumber',
115115
'fabric_id': 'chip::FabricId',
116116
'fabric_idx': 'chip::FabricIndex',
117-
'fabric_idx': 'chip::FabricIndex',
118117
'field_id': 'chip::FieldId',
119118
'group_id': 'chip::GroupId',
120119
'node_id': 'chip::NodeId',

scripts/py_matter_yamltests/matter_yamltests/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ def _response_values_validation(self, expected_response, received_response, resu
10861086
break
10871087

10881088
received_value = received_response.get('value')
1089-
if not self.is_attribute and not self.is_event and not (self.command in ANY_COMMANDS_LIST):
1089+
if not self.is_attribute and not self.is_event and self.command not in ANY_COMMANDS_LIST:
10901090
expected_name = value.get('name')
10911091
if expected_name not in received_value:
10921092
result.error(check_type, error_name_does_not_exist.format(
@@ -1173,7 +1173,7 @@ def _maybe_save_as(self, key: str, default_target: str, expected_response, recei
11731173
continue
11741174

11751175
received_value = received_response.get(default_target)
1176-
if not self.is_attribute and not self.is_event and not (self.command in ANY_COMMANDS_LIST):
1176+
if not self.is_attribute and not self.is_event and self.command not in ANY_COMMANDS_LIST:
11771177
expected_name = value.get('name')
11781178
if received_value is None or expected_name not in received_value:
11791179
result.error(check_type, error_name_does_not_exist.format(

scripts/tests/yaml/extensions/wildcard_response_extractor_cluster.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __get_argument(self, request, argument_name):
113113
if arguments is None:
114114
return None
115115

116-
if not type(arguments) is list:
116+
if type(arguments) is not list:
117117
return None
118118

119119
for argument in arguments:

scripts/tools/nrfconnect/generate_nrfconnect_chip_factory_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def _validate_args(self):
246246
"Cannot find paths to DAC or PAI certificates .der files. To generate a new ones please provide a path to chip-cert executable (--chip_cert_path) and add --gen_certs argument"
247247
assert self._args.output.endswith(".json"), \
248248
"Output path doesn't contain .json file path. ({})".format(self._args.output)
249-
assert not (self._args.passcode in INVALID_PASSCODES), \
249+
assert self._args.passcode not in INVALID_PASSCODES, \
250250
"Provided invalid passcode!"
251251

252252
def generate_json(self):

scripts/tools/silabs/FactoryDataProvider.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def __init__(self, arguments) -> None:
140140

141141
assert (bool(arguments.gen_spake2p_path) != bool(arguments.spake2_verifier)
142142
), "Provide either the spake2_verifier string or the path to the spake2 generator"
143-
assert not (arguments.passcode in INVALID_PASSCODES), "The provided passcode is invalid"
143+
assert arguments.passcode not in INVALID_PASSCODES, "The provided passcode is invalid"
144144

145145
self._args = arguments
146146

src/python_testing/TestSpecParsingSupport.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def test_derived_clusters(self):
262262
0, "Unexpected number of unknown commands in base")
263263
asserts.assert_equal(len(clusters[0xFFFF].unknown_commands), 2, "Unexpected number of unknown commands in derived cluster")
264264

265-
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name)
265+
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name, problems)
266266
# Ensure the base-only attribute (1) was added to the derived cluster
267267
asserts.assert_equal(set(clusters[0xFFFF].attributes.keys()), set(
268268
[0, 1, 2, 3] + expected_global_attrs), "Unexpected attribute list")

src/python_testing/spec_parsing_support.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def remove_problem(location: typing.Union[CommandPathLocation, FeaturePathLocati
487487
clusters[action_id].accepted_commands[c].conformance = optional()
488488
remove_problem(CommandPathLocation(endpoint_id=0, cluster_id=action_id, command_id=c))
489489

490-
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name)
490+
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name, problems)
491491

492492
for alias_base_name, aliased_clusters in CLUSTER_ALIASES.items():
493493
for id, (alias_name, pics) in aliased_clusters.items():
@@ -547,10 +547,10 @@ def remove_problem(location: typing.Union[CommandPathLocation, FeaturePathLocati
547547
return clusters, problems
548548

549549

550-
def combine_derived_clusters_with_base(xml_clusters: dict[int, XmlCluster], pure_base_clusters: dict[str, XmlCluster], ids_by_name: dict[str, int]) -> None:
550+
def combine_derived_clusters_with_base(xml_clusters: dict[int, XmlCluster], pure_base_clusters: dict[str, XmlCluster], ids_by_name: dict[str, int], problems: list[ProblemNotice]) -> None:
551551
''' Overrides base elements with the derived cluster values for derived clusters. '''
552552

553-
def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAttribute], cluster_id: uint) -> dict[uint, XmlAttribute]:
553+
def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAttribute], cluster_id: uint, problems: list[ProblemNotice]) -> dict[uint, XmlAttribute]:
554554
ret = deepcopy(base)
555555
extras = {k: v for k, v in derived.items() if k not in base.keys()}
556556
overrides = {k: v for k, v in derived.items() if k in base.keys()}
@@ -590,7 +590,7 @@ def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAt
590590
command_map.update(c.command_map)
591591
features = deepcopy(base.features)
592592
features.update(c.features)
593-
attributes = combine_attributes(base.attributes, c.attributes, id)
593+
attributes = combine_attributes(base.attributes, c.attributes, id, problems)
594594
accepted_commands = deepcopy(base.accepted_commands)
595595
accepted_commands.update(c.accepted_commands)
596596
generated_commands = deepcopy(base.generated_commands)

src/test_driver/linux-cirque/helper/CHIPTestBase.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ def assertTrue(self, exp, note=None):
299299
assert(Not)Equal
300300
python unittest style functions that raise exceptions when condition not met
301301
'''
302-
if not (exp is True):
302+
if exp is not True:
303303
if note:
304304
self.logger.error(note)
305305
raise AssertionError
306306

307307
def assertFalse(self, exp, note=None):
308-
if not (exp is False):
308+
if exp is not False:
309309
if note:
310310
self.logger.error(note)
311311
raise AssertionError

0 commit comments

Comments
 (0)