Skip to content

Commit 00afb8d

Browse files
Replace type() comparisons with isinstance() (#272)
- Replace type(x) == SomeType with isinstance(x, SomeType) - More Pythonic and follows PEP 8 style guide - Works correctly with inheritance and subclasses Signed-off-by: Andriy Kokhan <[email protected]>
1 parent e0f2e4e commit 00afb8d

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

common/sai.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def process_command(self, command):
176176

177177
if obj_key is None:
178178
obj_id = obj_type
179-
elif type(obj_key) == dict:
179+
elif isinstance(obj_key, dict):
180180
obj_key = json.dumps(obj_key)
181181
obj_id = obj_type + ":" + obj_key
182-
elif type(obj_key) == str and obj_key.startswith("oid:0x"):
182+
elif isinstance(obj_key, str) and obj_key.startswith("oid:0x"):
183183
obj_id = obj_key
184184
else:
185185
assert False, f"Failed to process: {obj_type}, {obj_key}, {operation}"
@@ -351,10 +351,10 @@ def get_meta(obj_type=None):
351351
if obj_type is None:
352352
return Sai.metadata
353353

354-
if type(obj_type) == SaiObjType:
354+
if isinstance(obj_type, SaiObjType):
355355
obj_type = "SAI_OBJECT_TYPE_" + SaiObjType(obj_type).name
356356
else:
357-
assert type(obj_type) == str
357+
assert isinstance(obj_type, str)
358358
assert obj_type.startswith("SAI_OBJECT_TYPE_")
359359

360360
for item in Sai.metadata:

common/sai_client/sai_redis_client/sai_redis_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ def operate(self, obj, attrs, op):
129129

130130
def create(self, obj, attrs, do_assert=True):
131131
vid = None
132-
if type(obj) == SaiObjType:
132+
if isinstance(obj, SaiObjType):
133133
vid = self.alloc_vid(obj)
134134
obj = "SAI_OBJECT_TYPE_" + obj.name + ":" + vid
135-
elif type(obj) == str and obj.startswith("SAI_OBJECT_TYPE_") and ":" not in obj:
135+
elif isinstance(obj, str) and obj.startswith("SAI_OBJECT_TYPE_") and ":" not in obj:
136136
vid = self.alloc_vid(obj)
137137
obj = obj + ":" + vid
138138
else:
@@ -240,11 +240,11 @@ def bulk_create(self, obj_type, keys, attrs, obj_count=0, do_assert=True):
240240
The third element contains the list of statuses of each individual object
241241
creation result.
242242
'''
243-
assert (type(obj_type) == SaiObjType) or (type(obj_type) == str and obj_type.startswith("SAI_OBJECT_TYPE_"))
243+
assert isinstance(obj_type, SaiObjType) or (isinstance(obj_type, str) and obj_type.startswith("SAI_OBJECT_TYPE_"))
244244
assert keys is None or len(keys) == len(attrs) or len(attrs) == 1
245245

246246
entries_num = len(keys) if keys else obj_count
247-
key = "SAI_OBJECT_TYPE_" + obj_type.name if type(obj_type) == SaiObjType else obj_type
247+
key = "SAI_OBJECT_TYPE_" + obj_type.name if isinstance(obj_type, SaiObjType) else obj_type
248248
key = key + ":" + str(entries_num)
249249

250250
str_attr = ""
@@ -312,9 +312,9 @@ def bulk_remove(self, obj_type, keys, do_assert = True):
312312
The second element contains the list of statuses of each individual object
313313
removal result.
314314
'''
315-
assert (type(obj_type) == SaiObjType) or (type(obj_type) == str and obj_type.startswith("SAI_OBJECT_TYPE_"))
315+
assert isinstance(obj_type, SaiObjType) or (isinstance(obj_type, str) and obj_type.startswith("SAI_OBJECT_TYPE_"))
316316

317-
key = "SAI_OBJECT_TYPE_" + obj_type.name if type(obj_type) == SaiObjType else obj_type
317+
key = "SAI_OBJECT_TYPE_" + obj_type.name if isinstance(obj_type, SaiObjType) else obj_type
318318
key = key + ":" + str(len(keys))
319319

320320
values = []
@@ -524,9 +524,9 @@ def get_object_key(self, obj_type=None):
524524
return oids_by_type
525525

526526
def alloc_vid(self, obj_type):
527-
if type(obj_type) == str and obj_type.startswith("SAI_OBJECT_TYPE_"):
527+
if isinstance(obj_type, str) and obj_type.startswith("SAI_OBJECT_TYPE_"):
528528
obj_type = SaiObjType[obj_type.replace("SAI_OBJECT_TYPE_", "")]
529-
assert type(obj_type) == SaiObjType
529+
assert isinstance(obj_type, SaiObjType)
530530

531531
vid = None
532532
if obj_type == SaiObjType.SWITCH:

common/sai_client/sai_thrift_client/sai_thrift_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def obj_to_items(obj):
4242
obj_type = None
4343
oid = None
4444
key = None
45-
if type(obj) == str and obj.startswith("oid:0x"):
45+
if isinstance(obj, str) and obj.startswith("oid:0x"):
4646
oid = obj
47-
elif type(obj) == str:
47+
elif isinstance(obj, str):
4848
obj = obj.split(":", 1)
4949
obj_type = obj[0]
5050
if len(obj) > 1:
@@ -57,7 +57,7 @@ def obj_to_items(obj):
5757
obj[1] = obj[1].replace("bvid", "bv_id")
5858
obj[1] = obj[1].replace("mac", "mac_address")
5959
key = json.loads(obj[1])
60-
elif type(obj) == SaiObjType:
60+
elif isinstance(obj, SaiObjType):
6161
obj_type = obj
6262
else:
6363
assert False, "Unsupported OID format type {}".format(type(obj))
@@ -67,8 +67,8 @@ def obj_to_items(obj):
6767
def create(self, obj, attrs, do_assert=True):
6868
obj_type, _, key = self.obj_to_items(obj)
6969
status, result = self._operate('create', attrs=attrs, obj_type=obj_type, key=key)
70-
if key is None and type(result) == int:
71-
if type(obj_type) == str:
70+
if key is None and isinstance(result, int):
71+
if isinstance(obj_type, str):
7272
obj_type = SaiObjType[obj_type.replace("SAI_OBJECT_TYPE_", "")]
7373
self.sai_type_map[result] = obj_type
7474

@@ -268,7 +268,7 @@ def bulk_create(self, obj_type, keys, attrs, obj_count=0, do_assert=True):
268268
entries_num = len(keys) if keys else obj_count
269269
statuses = [None] * entries_num
270270

271-
if type(obj_type) == SaiObjType:
271+
if isinstance(obj_type, SaiObjType):
272272
obj_type = "SAI_OBJECT_TYPE_" + obj_type.name
273273

274274
for i in range(entries_num):
@@ -289,7 +289,7 @@ def bulk_remove(self, obj_type, keys, do_assert=True):
289289
# TODO: Provide proper implementation once Thrift bulk API is available
290290
statuses = [None] * len(keys)
291291

292-
if type(obj_type) == SaiObjType:
292+
if isinstance(obj_type, SaiObjType):
293293
obj_type = "SAI_OBJECT_TYPE_" + obj_type.name
294294

295295
for key in keys:
@@ -306,7 +306,7 @@ def bulk_set(self, obj_type, keys, attrs, do_assert=True):
306306
# TODO: Provide proper implementation once Thrift bulk API is available
307307
statuses = [None] * len(keys)
308308

309-
if type(obj_type) == SaiObjType:
309+
if isinstance(obj_type, SaiObjType):
310310
obj_type = "SAI_OBJECT_TYPE_" + obj_type.name
311311

312312
for i in range(len(keys)):

common/sai_client/sai_thrift_client/sai_thrift_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def sai_qos_map_list(value):
243243
244244
{"count":1,"list":[{"key":0,"value":0}]} => sai_thrift_qos_map_list_t(count=1, maplist=[{"key":0,"value":0}])
245245
"""
246-
val = json.loads(value) if type(value) == str else value
246+
val = json.loads(value) if isinstance(value, str) else value
247247
maplist = []
248248
for entry in val["list"]:
249249
key = ThriftConverter.sai_qos_map_params(entry["key"])
@@ -624,10 +624,10 @@ def get_sai_meta(obj_type, attr_name):
624624
except IOError:
625625
return None
626626

627-
if type(obj_type) == SaiObjType:
627+
if isinstance(obj_type, SaiObjType):
628628
obj_type = "SAI_OBJECT_TYPE_" + SaiObjType(obj_type).name
629629
else:
630-
assert type(obj_type) == str
630+
assert isinstance(obj_type, str)
631631
assert obj_type.startswith("SAI_OBJECT_TYPE_")
632632

633633
for item in sai_json:

common/sai_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def generate_from_thrift():
4343
if variable_name == "SAI_OBJECT_TYPE_EXTENSIONS_RANGE_START":
4444
continue
4545

46-
value = variable_value if type(variable_value) == int else variable_value.value
46+
value = variable_value if isinstance(variable_value, int) else variable_value.value
4747
extend_enum(SaiObjType, variable_name[16:], value)
4848

4949
@staticmethod

common/sai_testbed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def get_asic_dir(base_dir, asic, asic_type="npu"):
8484
def get_sku_config(self, alias, asic_type="npu"):
8585
cfg = self.get_asic_config(alias, asic_type)
8686
sku = cfg.get("sku", None)
87-
if type(sku) == str:
87+
if isinstance(sku, str):
8888
asic_dir = self.get_asic_dir(self.base_dir, cfg["asic"], asic_type)
8989
try:
9090
target = cfg.get("target")

0 commit comments

Comments
 (0)