Skip to content

Commit 13064a5

Browse files
authored
mypy compliance for choice_conformance.py and commissioning.py (project-chip#38669)
* fixing 5 mypy errors * fix mypy issues in commissioning.py * update the gh action * fix type hint to align the type definitions with the actual usage and parsing logic, and resolve the mypy error * fix gh action * review comments * lint code * restyle * use Mobly assertions but keep type assertions for mypy * autopep format
1 parent d1bb152 commit 13064a5

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

.github/workflows/mypy-validation.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ jobs:
5858
src/python_testing/matter_testing_infrastructure/chip/testing/tasks.py \
5959
src/python_testing/matter_testing_infrastructure/chip/testing/taglist_and_topology_test.py \
6060
src/python_testing/matter_testing_infrastructure/chip/testing/pics.py \
61+
src/python_testing/matter_testing_infrastructure/chip/testing/choice_conformance.py \
62+
src/python_testing/matter_testing_infrastructure/chip/testing/commissioning.py \
6163
src/python_testing/matter_testing_infrastructure/chip/testing/decorators.py \
6264
src/python_testing/matter_testing_infrastructure/chip/testing/basic_composition.py"
6365

src/python_testing/matter_testing_infrastructure/chip/testing/choice_conformance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ def _evaluate_choices(location: AttributePathLocation, counts: dict[Choice, int]
3131

3232

3333
def evaluate_feature_choice_conformance(endpoint_id: int, cluster_id: int, xml_clusters: dict[int, XmlCluster], feature_map: uint, attribute_list: list[uint], all_command_list: list[uint]) -> list[ChoiceConformanceProblemNotice]:
34-
all_features = [1 << i for i in range(32)]
34+
all_features = [uint(1 << i) for i in range(32)]
3535
all_features = [f for f in all_features if f in xml_clusters[cluster_id].features.keys()]
3636

3737
# Other pieces of the 10.2 test check for unknown features, so just remove them here to check choice conformance
3838
counts: dict[Choice, int] = {}
3939
for f in all_features:
4040
xml_feature = xml_clusters[cluster_id].features[f]
4141
conformance_decision_with_choice = xml_feature.conformance(feature_map, attribute_list, all_command_list)
42-
_add_to_counts_if_required(conformance_decision_with_choice, (feature_map & f), counts)
42+
_add_to_counts_if_required(conformance_decision_with_choice, (feature_map & f) != 0, counts)
4343

4444
location = AttributePathLocation(endpoint_id=endpoint_id, cluster_id=cluster_id,
4545
attribute_id=GlobalAttributeIds.FEATURE_MAP_ID)

src/python_testing/matter_testing_infrastructure/chip/testing/commissioning.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CommissioningInfo:
6363
commissioning_method (Optional[str]):
6464
The method by which the device is being commissioned.
6565
66-
thread_operational_dataset (Optional[str]):
66+
thread_operational_dataset (Optional[bytes]):
6767
The Thread operational dataset if applicable during commissioning.
6868
6969
wifi_passphrase (Optional[str]):
@@ -81,7 +81,7 @@ class CommissioningInfo:
8181
"""
8282
commissionee_ip_address_just_for_testing: Optional[str] = None
8383
commissioning_method: Optional[str] = None
84-
thread_operational_dataset: Optional[str] = None
84+
thread_operational_dataset: Optional[bytes] = None
8585
wifi_passphrase: Optional[str] = None
8686
wifi_ssid: Optional[str] = None
8787
tc_version_to_simulate: Optional[int] = None
@@ -158,6 +158,12 @@ async def commission_device(
158158
return PairingStatus(exception=e)
159159
elif commissioning_info.commissioning_method == "ble-wifi":
160160
try:
161+
asserts.assert_is_not_none(commissioning_info.wifi_ssid, "WiFi SSID must be provided for ble-wifi commissioning")
162+
asserts.assert_is_not_none(commissioning_info.wifi_passphrase,
163+
"WiFi Passphrase must be provided for ble-wifi commissioning")
164+
# Type assertions to help mypy understand these are not None after the asserts
165+
assert commissioning_info.wifi_ssid is not None
166+
assert commissioning_info.wifi_passphrase is not None
161167
await dev_ctrl.CommissionWiFi(
162168
info.filter_value,
163169
info.passcode,
@@ -172,6 +178,10 @@ async def commission_device(
172178
return PairingStatus(exception=e)
173179
elif commissioning_info.commissioning_method == "ble-thread":
174180
try:
181+
asserts.assert_is_not_none(commissioning_info.thread_operational_dataset,
182+
"Thread dataset must be provided for ble-thread commissioning")
183+
# Type assertion to help mypy understand this is not None after the assert
184+
assert commissioning_info.thread_operational_dataset is not None
175185
await dev_ctrl.CommissionThread(
176186
info.filter_value,
177187
info.passcode,

src/python_testing/matter_testing_infrastructure/chip/testing/matter_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ class MatterTestConfig:
619619

620620
wifi_ssid: Optional[str] = None
621621
wifi_passphrase: Optional[str] = None
622-
thread_operational_dataset: Optional[str] = None
622+
thread_operational_dataset: Optional[bytes] = None
623623

624624
pics: dict[bool, str] = field(default_factory=dict)
625625

src/python_testing/matter_testing_infrastructure/chip/testing/spec_parsing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
import chip.clusters as Clusters
3131
import chip.testing.conformance as conformance_support
32-
from chip.testing.conformance import (OPTIONAL_CONFORM, TOP_LEVEL_CONFORMANCE_TAGS, ConformanceDecision, ConformanceException,
33-
ConformanceParseParameters, feature, is_disallowed, mandatory, optional, or_operation,
34-
parse_callable_from_xml, parse_device_type_callable_from_xml)
32+
from chip.testing.conformance import (OPTIONAL_CONFORM, TOP_LEVEL_CONFORMANCE_TAGS, ConformanceDecisionWithChoice,
33+
ConformanceException, ConformanceParseParameters, feature, is_disallowed, mandatory, optional,
34+
or_operation, parse_callable_from_xml, parse_device_type_callable_from_xml)
3535
from chip.testing.global_attribute_ids import GlobalAttributeIds
3636
from chip.testing.matter_testing import (AttributePathLocation, ClusterPathLocation, CommandPathLocation, DeviceTypePathLocation,
3737
EventPathLocation, FeaturePathLocation, ProblemLocation, ProblemNotice, ProblemSeverity)
@@ -55,7 +55,7 @@ class SpecParsingException(Exception):
5555

5656

5757
# passing in feature map, attribute list, command list
58-
ConformanceCallable = Callable[[uint, list[uint], list[uint]], ConformanceDecision]
58+
ConformanceCallable = Callable[[uint, list[uint], list[uint]], ConformanceDecisionWithChoice]
5959

6060

6161
@dataclass
@@ -92,7 +92,7 @@ def __str__(self):
9292
class XmlCommand:
9393
id: int
9494
name: str
95-
conformance: Callable[[uint], ConformanceDecision]
95+
conformance: ConformanceCallable
9696
privilege: Clusters.AccessControl.Enums.AccessControlEntryPrivilegeEnum
9797

9898
def __str__(self):

0 commit comments

Comments
 (0)