Skip to content

Commit

Permalink
[Python] Add ruff as Python linter (project-chip#32384)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
agners authored Mar 15, 2024
1 parent d166650 commit 3b1c825
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 26 deletions.
6 changes: 0 additions & 6 deletions .flake8

This file was deleted.

9 changes: 4 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
if: github.actor != 'restyled-io[bot]'

container:
image: ghcr.io/project-chip/chip-build:35
image: ghcr.io/project-chip/chip-build:39

steps:
- name: Checkout
Expand Down Expand Up @@ -267,12 +267,11 @@ jobs:
run: |
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
# Run python Linter (flake8) and verify python files
# ignore some style errors, restyler should do that
- name: Check for errors using flake8 Python linter
# Run ruff python linter
- name: Check for errors using ruff Python linter
if: always()
run: |
flake8 --extend-ignore=E501,W391
ruff check
# git grep exits with 0 if it finds a match, but we want
# to fail (exit nonzero) on match. And we want to exclude this file,
Expand Down
2 changes: 2 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ restylers:
command:
- autopep8
- "--in-place"
- "--max-line-length"
- "132"
arguments: []
include:
- "**/*.py"
Expand Down
1 change: 0 additions & 1 deletion integrations/docker/images/base/chip-build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ RUN set -x \
click \
coloredlogs \
cxxfilt \
flake8 \
future \
ghapi \
mobly \
Expand Down
19 changes: 19 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exclude = [
".environment",
".git",
".github",
".*",
"build",
"out",
"third_party",
"examples/common/QRCode/",
]
target-version = "py37"

line-length = 132

[lint]
select = ["E4", "E7", "E9", "F"]
ignore = [
"E721" # We use is for good reasons
]
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Optional[str]
'event_no': 'chip::EventNumber',
'fabric_id': 'chip::FabricId',
'fabric_idx': 'chip::FabricIndex',
'fabric_idx': 'chip::FabricIndex',
'field_id': 'chip::FieldId',
'group_id': 'chip::GroupId',
'node_id': 'chip::NodeId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Optional[str]
'event_no': 'chip::EventNumber',
'fabric_id': 'chip::FabricId',
'fabric_idx': 'chip::FabricIndex',
'fabric_idx': 'chip::FabricIndex',
'field_id': 'chip::FieldId',
'group_id': 'chip::GroupId',
'node_id': 'chip::NodeId',
Expand Down
4 changes: 2 additions & 2 deletions scripts/py_matter_yamltests/matter_yamltests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ def _response_values_validation(self, expected_response, received_response, resu
break

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

received_value = received_response.get(default_target)
if not self.is_attribute and not self.is_event and not (self.command in ANY_COMMANDS_LIST):
if not self.is_attribute and not self.is_event and self.command not in ANY_COMMANDS_LIST:
expected_name = value.get('name')
if received_value is None or expected_name not in received_value:
result.error(check_type, error_name_does_not_exist.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __get_argument(self, request, argument_name):
if arguments is None:
return None

if not type(arguments) is list:
if type(arguments) is not list:
return None

for argument in arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def _validate_args(self):
"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"
assert self._args.output.endswith(".json"), \
"Output path doesn't contain .json file path. ({})".format(self._args.output)
assert not (self._args.passcode in INVALID_PASSCODES), \
assert self._args.passcode not in INVALID_PASSCODES, \
"Provided invalid passcode!"

def generate_json(self):
Expand Down
2 changes: 1 addition & 1 deletion scripts/tools/silabs/FactoryDataProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __init__(self, arguments) -> None:

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

self._args = arguments

Expand Down
2 changes: 1 addition & 1 deletion src/python_testing/TestSpecParsingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def test_derived_clusters(self):
0, "Unexpected number of unknown commands in base")
asserts.assert_equal(len(clusters[0xFFFF].unknown_commands), 2, "Unexpected number of unknown commands in derived cluster")

combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name)
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name, problems)
# Ensure the base-only attribute (1) was added to the derived cluster
asserts.assert_equal(set(clusters[0xFFFF].attributes.keys()), set(
[0, 1, 2, 3] + expected_global_attrs), "Unexpected attribute list")
Expand Down
8 changes: 4 additions & 4 deletions src/python_testing/spec_parsing_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def remove_problem(location: typing.Union[CommandPathLocation, FeaturePathLocati
clusters[action_id].accepted_commands[c].conformance = optional()
remove_problem(CommandPathLocation(endpoint_id=0, cluster_id=action_id, command_id=c))

combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name)
combine_derived_clusters_with_base(clusters, pure_base_clusters, ids_by_name, problems)

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


def combine_derived_clusters_with_base(xml_clusters: dict[int, XmlCluster], pure_base_clusters: dict[str, XmlCluster], ids_by_name: dict[str, int]) -> None:
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:
''' Overrides base elements with the derived cluster values for derived clusters. '''

def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAttribute], cluster_id: uint) -> dict[uint, XmlAttribute]:
def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAttribute], cluster_id: uint, problems: list[ProblemNotice]) -> dict[uint, XmlAttribute]:
ret = deepcopy(base)
extras = {k: v for k, v in derived.items() if k not in base.keys()}
overrides = {k: v for k, v in derived.items() if k in base.keys()}
Expand Down Expand Up @@ -590,7 +590,7 @@ def combine_attributes(base: dict[uint, XmlAttribute], derived: dict[uint, XmlAt
command_map.update(c.command_map)
features = deepcopy(base.features)
features.update(c.features)
attributes = combine_attributes(base.attributes, c.attributes, id)
attributes = combine_attributes(base.attributes, c.attributes, id, problems)
accepted_commands = deepcopy(base.accepted_commands)
accepted_commands.update(c.accepted_commands)
generated_commands = deepcopy(base.generated_commands)
Expand Down
4 changes: 2 additions & 2 deletions src/test_driver/linux-cirque/helper/CHIPTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ def assertTrue(self, exp, note=None):
assert(Not)Equal
python unittest style functions that raise exceptions when condition not met
'''
if not (exp is True):
if exp is not True:
if note:
self.logger.error(note)
raise AssertionError

def assertFalse(self, exp, note=None):
if not (exp is False):
if exp is not False:
if note:
self.logger.error(note)
raise AssertionError
Expand Down

0 comments on commit 3b1c825

Please sign in to comment.