Skip to content

Commit 620fc50

Browse files
Type hinting for backwards compatibility with Python 3.8 (#880)
1 parent 7d00cb1 commit 620fc50

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

nmostesting/MS05Utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from enum import IntEnum, Enum
2222
from itertools import takewhile, dropwhile
2323
from jsonschema import FormatChecker, SchemaError, validate, ValidationError
24-
from typing import Optional, Union
24+
from typing import List, Optional, Union
2525

2626
from .GenericTest import NMOSTestException, GenericTest
2727
from .TestResult import Test
@@ -504,7 +504,7 @@ def __init__(self, event_data_json):
504504

505505

506506
class NcObject():
507-
def __init__(self, class_id: list, oid: int, owner: Optional[int], role: str, role_path: list,
507+
def __init__(self, class_id: List[int], oid: int, owner: Optional[int], role: str, role_path: List[str],
508508
runtime_constraints: Optional[NcPropertyConstraints],
509509
member_descriptor: NcBlockMemberDescriptor):
510510
self.class_id = class_id
@@ -517,7 +517,7 @@ def __init__(self, class_id: list, oid: int, owner: Optional[int], role: str, ro
517517

518518

519519
class NcBlock(NcObject):
520-
def __init__(self, class_id: list, oid: int, owner: Optional[int], role: str, role_path: list,
520+
def __init__(self, class_id: List[int], oid: int, owner: Optional[int], role: str, role_path: List[str],
521521
runtime_constraints: Optional[NcPropertyConstraints],
522522
member_descriptor: NcBlockMemberDescriptor):
523523
NcObject.__init__(self, class_id, oid, owner, role, role_path, runtime_constraints, member_descriptor)
@@ -527,7 +527,7 @@ def __init__(self, class_id: list, oid: int, owner: Optional[int], role: str, ro
527527
def add_child_object(self, nc_object):
528528
self.child_objects.append(nc_object)
529529

530-
def get_role_paths(self) -> list[list[str]]:
530+
def get_role_paths(self) -> List[List[str]]:
531531
role_paths = []
532532
for child_object in self.child_objects:
533533
role_paths.append([child_object.role])
@@ -539,7 +539,7 @@ def get_role_paths(self) -> list[list[str]]:
539539
role_paths.append(role_path)
540540
return role_paths
541541

542-
def get_oids(self, root=True) -> list[int]:
542+
def get_oids(self, root=True) -> List[int]:
543543
oids = [self.oid] if root else []
544544
for child_object in self.child_objects:
545545
oids.append(child_object.oid)
@@ -562,15 +562,15 @@ def find_object_by_path(self, role_path) -> NcObject:
562562
return ret_val
563563

564564
# NcBlock Methods
565-
def get_member_descriptors(self, recurse=False) -> list[NcBlockMemberDescriptor]:
565+
def get_member_descriptors(self, recurse=False) -> List[NcBlockMemberDescriptor]:
566566
query_results = []
567567
for child_object in self.child_objects:
568568
query_results.append(child_object.member_descriptor)
569569
if recurse and type(child_object) is NcBlock:
570570
query_results += child_object.get_member_descriptors(recurse)
571571
return query_results
572572

573-
def find_members_by_path(self, role_path) -> list[NcBlockMemberDescriptor]:
573+
def find_members_by_path(self, role_path) -> List[NcBlockMemberDescriptor]:
574574
query_results = []
575575
query_role = role_path[0]
576576
for child_object in self.child_objects:
@@ -585,7 +585,7 @@ def find_members_by_role(self,
585585
role,
586586
case_sensitive=False,
587587
match_whole_string=False,
588-
recurse=False) -> list[NcBlockMemberDescriptor]:
588+
recurse=False) -> List[NcBlockMemberDescriptor]:
589589
def match(query_role, role, case_sensitive, match_whole_string):
590590
if case_sensitive:
591591
return query_role == role if match_whole_string else query_role in role
@@ -606,7 +606,7 @@ def find_members_by_class_id(self,
606606
class_id,
607607
include_derived=False,
608608
recurse=False,
609-
get_objects=False) -> Union[list[NcBlockMemberDescriptor], list[NcObject]]:
609+
get_objects=False) -> Union[List[NcBlockMemberDescriptor], List[NcObject]]:
610610
def match(query_class_id, class_id, include_derived):
611611
if query_class_id == (class_id[:len(query_class_id)] if include_derived else class_id):
612612
return True
@@ -626,16 +626,16 @@ def match(query_class_id, class_id, include_derived):
626626

627627

628628
class NcManager(NcObject):
629-
def __init__(self, class_id: list, oid: int, owner: Optional[int], role: list, role_path: str,
629+
def __init__(self, class_id: List[int], oid: int, owner: Optional[int], role: List[str], role_path: str,
630630
runtime_constraints: Optional[NcPropertyConstraints],
631631
member_descriptor: NcBlockMemberDescriptor):
632632
NcObject.__init__(self, class_id, oid, owner, role, role_path, runtime_constraints, member_descriptor)
633633

634634

635635
class NcClassManager(NcManager):
636-
def __init__(self, class_id: list, oid: int, owner: Optional[int], role: list, role_path: str,
637-
class_descriptors: list[NcClassDescriptor],
638-
datatype_descriptors: list[NcDatatypeDescriptor],
636+
def __init__(self, class_id: List[int], oid: int, owner: Optional[int], role: List[str], role_path: str,
637+
class_descriptors: List[NcClassDescriptor],
638+
datatype_descriptors: List[NcDatatypeDescriptor],
639639
runtime_constraints: Optional[NcPropertyConstraints],
640640
member_descriptor: NcBlockMemberDescriptor):
641641
NcObject.__init__(self, class_id, oid, owner, role, role_path, runtime_constraints, member_descriptor)
@@ -1470,7 +1470,7 @@ def get_properties(self,
14701470
block: NcBlock,
14711471
get_constraints=False,
14721472
get_sequences=False,
1473-
get_readonly=False) -> list[MS05PropertyMetadata]:
1473+
get_readonly=False) -> List[MS05PropertyMetadata]:
14741474

14751475
def is_read_only(class_id, property_descriptor):
14761476
"""Account for Worker enabled property cludge in the BCP-008 specs"""

nmostesting/suites/IS1401Test.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from copy import copy, deepcopy
1717
from enum import IntEnum
1818
from functools import cmp_to_key
19-
from typing import Union
19+
from typing import Dict, List, Union
2020
import random
2121
import string
2222
from ..Config import MS05_INVASIVE_TESTING
@@ -133,7 +133,7 @@ def reset_device_model(self):
133133
self.is14_utils.device_model = None
134134
self.oid_cache = []
135135

136-
def _do_request_json(self, test: GenericTest, method: str, url: str, **kwargs):
136+
def _do_request_json(self, test: GenericTest, method: str, url: str, **kwargs) -> any:
137137
"""Perform an HTTP request and return JSON response"""
138138
valid, response = self.do_request(method, url, **kwargs)
139139

@@ -151,7 +151,7 @@ def _do_request_json(self, test: GenericTest, method: str, url: str, **kwargs):
151151

152152
return response.json()
153153

154-
def _compare_property_ids(self, a: NcPropertyId, b: NcPropertyId):
154+
def _compare_property_ids(self, a: NcPropertyId, b: NcPropertyId) -> int:
155155
"""Compare level and index of an NcPropertyId"""
156156
if a.level > b.level:
157157
return 1
@@ -166,7 +166,7 @@ def _compare_property_ids(self, a: NcPropertyId, b: NcPropertyId):
166166

167167
def _compare_property_objects(self,
168168
a: Union[NcPropertyDescriptor, NcPropertyHolder],
169-
b: Union[NcPropertyDescriptor, NcPropertyHolder]):
169+
b: Union[NcPropertyDescriptor, NcPropertyHolder]) -> int:
170170
"""Compare NcPropertyIds of NcPropertyDescriptors or NcPropertyHolders"""
171171
return self._compare_property_ids(a.id, b.id)
172172

@@ -187,7 +187,7 @@ def _to_dict(self, obj):
187187
else:
188188
return obj
189189

190-
def _get_bulk_properties_holder(self, test: GenericTest, endpoint: str):
190+
def _get_bulk_properties_holder(self, test: GenericTest, endpoint: str) -> NcBulkPropertiesHolder:
191191
"""Get backup dataset from endpoint"""
192192
method_result_json = self._do_request_json(test, "GET", endpoint)
193193

@@ -213,7 +213,7 @@ def _apply_bulk_properties_holder(self,
213213
endpoint: str,
214214
bulk_properties_holder: NcBulkPropertiesHolder,
215215
restoreMode: NcRestoreMode,
216-
recurse: bool):
216+
recurse: bool) -> List[NcObjectPropertiesSetValidation]:
217217
"""Apply a backup dataset to the endpoint"""
218218
backup_dataset = {
219219
"arguments": {
@@ -253,7 +253,7 @@ def _restore_bulk_properties_holder(self,
253253
endpoint: str,
254254
bulk_properties_holder: NcBulkPropertiesHolder,
255255
restoreMode=NcRestoreMode.Modify,
256-
recurse=True):
256+
recurse=True) -> List[NcObjectPropertiesSetValidation]:
257257
"""Perform an HTTP PUT of the backup dataset to the endpoint"""
258258
return self._apply_bulk_properties_holder(test,
259259
test_metadata,
@@ -265,7 +265,7 @@ def _validate_bulk_properties_holder(self,
265265
endpoint: str,
266266
bulk_properties_holder: NcBulkPropertiesHolder,
267267
restoreMode=NcRestoreMode.Modify,
268-
recurse=True):
268+
recurse=True) -> List[NcObjectPropertiesSetValidation]:
269269
"""Perform an HTTP PATCH of the backup dataset to the endpoint"""
270270
return self._apply_bulk_properties_holder(test,
271271
test_metadata,
@@ -297,7 +297,7 @@ def test_02(self, test):
297297
+ "/docs/API_requests.html#url-and-usage")
298298
return test.PASS()
299299

300-
def _check_block_member_role_syntax(self, test: GenericTest, role_path: list[str]):
300+
def _check_block_member_role_syntax(self, test: GenericTest, role_path: List[str]):
301301
"""Check syntax of roles in this block"""
302302
method_result = self.is14_utils.get_property(test, NcBlockProperties.MEMBERS.value, role_path=role_path)
303303

@@ -725,11 +725,12 @@ def test_14(self, test):
725725

726726
return test.PASS()
727727

728-
def _create_notices_list(self, set_validations: list[NcObjectPropertiesSetValidation]):
728+
def _create_notices_list(self, set_validations: List[NcObjectPropertiesSetValidation]) -> List[str]:
729729
"""Create list of validation notices"""
730730
return [".".join(v.path) + str(n.id) for v in set_validations for n in v.notices]
731731

732-
def _create_object_properties_dict(self, bulk_properties_holder: NcBulkPropertiesHolder):
732+
def _create_object_properties_dict(self, bulk_properties_holder: NcBulkPropertiesHolder) \
733+
-> Dict[str, NcObjectPropertiesHolder]:
733734
"""Creates a dict keyed on formatted role path and property id"""
734735
return {".".join(o.path) + str(v.id): v for o in bulk_properties_holder.values for v in o.values}
735736

@@ -738,8 +739,8 @@ def _compare_backup_datasets(self,
738739
original_bulk_properties_holder: NcBulkPropertiesHolder,
739740
applied_bulk_properties_holder: NcBulkPropertiesHolder,
740741
updated_bulk_properties_holder: NcBulkPropertiesHolder,
741-
target_role_path: list[str],
742-
set_validations: list[NcObjectPropertiesSetValidation],
742+
target_role_path: List[str],
743+
set_validations: List[NcObjectPropertiesSetValidation],
743744
recurse: bool, validate=False):
744745
"""Compare the original backup dataset to the updated, given the applied dataset"""
745746
# original_bulk_properties_holder is the state before dataset applied
@@ -805,8 +806,8 @@ def _compare_values(expected, actual):
805806
def _check_object_properties_set_validations(self,
806807
test_metadata: TestMetadata,
807808
bulk_properties_holder: NcBulkPropertiesHolder,
808-
validations: list[NcObjectPropertiesSetValidation],
809-
target_role_path: list[str],
809+
validations: List[NcObjectPropertiesSetValidation],
810+
target_role_path: List[str],
810811
recurse: bool):
811812
"""Check that the NcObjectPropertiesSetValidations have no errors"""
812813
# Check there is one validation per object changed
@@ -851,7 +852,7 @@ def _check_object_properties_set_validations(self,
851852

852853
def _check_validate_restore_properties(self,
853854
test: GenericTest,
854-
target_role_path: list[str],
855+
target_role_path: List[str],
855856
restoreMode: NcRestoreMode,
856857
recurse: bool):
857858
"""Perform a validate and restore on the target role path"""
@@ -1041,7 +1042,7 @@ def test_20(self, test):
10411042

10421043
# Invasive testing
10431044

1044-
def _generate_property_value(self, test: GenericTest, type_name: str, value: any):
1045+
def _generate_property_value(self, test: GenericTest, type_name: str, value: any) -> any:
10451046
"""Generate a new value based on the existing value"""
10461047

10471048
# If this is a null property then not sure how to manipulate it
@@ -1125,8 +1126,8 @@ def _check_device_model_structure(self,
11251126

11261127
def _filter_property_holders(self,
11271128
bulk_properties_holder: NcBulkPropertiesHolder,
1128-
filter_list: list[str],
1129-
include=False):
1129+
filter_list: List[str],
1130+
include=False) -> NcBulkPropertiesHolder:
11301131
"""Either include or exclude the filter_list property keys from the bulk properties holder"""
11311132
# if include = True then properties with keys found in filter_list are kept (all others removed)
11321133
# if include = False then properties with keys found in filter_list are removed
@@ -1147,7 +1148,7 @@ def _filter_property_holders(self,
11471148
def _perform_restore(self,
11481149
test: GenericTest,
11491150
test_metadata: TestMetadata,
1150-
target_role_path: list[str],
1151+
target_role_path: List[str],
11511152
bulk_properties_holder: NcBulkPropertiesHolder,
11521153
original_bulk_properties_holder: NcBulkPropertiesHolder,
11531154
restoreMode: NcRestoreMode,

0 commit comments

Comments
 (0)