refactor: Use isinstance instead of type comparison for value check

Use isinstance function instead of type comparison for checking the type of the value in the utility functions `value_to_schema` and `limit_example_size`. As suggested by flake8.
pull/87/head
alufers 12 months ago
parent 51b1ca12f3
commit d30b6ea2d2

@ -111,22 +111,22 @@ def response_to_headers(headers):
def value_to_schema(value): def value_to_schema(value):
# check if value is a number # check if value is a number
if type(value) == int or type(value) == float: if isinstance(value, (int, float)):
return {"type": "number"} return {"type": "number"}
# check if value is a boolean # check if value is a boolean
elif type(value) == bool: elif isinstance(value, bool):
return {"type": "boolean"} return {"type": "boolean"}
# check if value is a string # check if value is a string
elif type(value) == str: elif isinstance(value, str):
return {"type": "string"} return {"type": "string"}
# check if value is a list # check if value is a list
elif type(value) == list: elif isinstance(value, list):
if len(value) == 0: if len(value) == 0:
return {"type": "array", "items": {}} return {"type": "array", "items": {}}
return {"type": "array", "items": value_to_schema(value[0])} return {"type": "array", "items": value_to_schema(value[0])}
# check if value is a dict # check if value is a dict
elif type(value) == dict: elif isinstance(value, dict):
return { return {
"type": "object", "type": "object",
"properties": {key: value_to_schema(value[key]) for key in value}, "properties": {key: value_to_schema(value[key]) for key in value},
@ -142,14 +142,14 @@ MAX_EXAMPLE_OBJECT_PROPERTIES = 150
# recursively scan an example value and limit the number of elements and properties # recursively scan an example value and limit the number of elements and properties
def limit_example_size(example): def limit_example_size(example):
if type(example) == list: if isinstance(example, list):
new_list = [] new_list = []
for element in example: for element in example:
if len(new_list) >= MAX_EXAMPLE_ARRAY_ELEMENTS: if len(new_list) >= MAX_EXAMPLE_ARRAY_ELEMENTS:
break break
new_list.append(limit_example_size(element)) new_list.append(limit_example_size(element))
return new_list return new_list
elif type(example) == dict: elif isinstance(example, dict):
new_dict = {} new_dict = {}
for key in example: for key in example:
if len(new_dict) >= MAX_EXAMPLE_OBJECT_PROPERTIES: if len(new_dict) >= MAX_EXAMPLE_OBJECT_PROPERTIES:

Loading…
Cancel
Save