-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_type.py
37 lines (26 loc) · 912 Bytes
/
common_type.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_common_type(type1: type, type2: type) -> type:
if type1 == str:
return str
if type1 == bool:
return type2
if type1 == type2:
return type1
if type1 == list and type2 == range or type2 == tuple:
return list
else:
return str
if type1 == range:
return str
if type1 == float and type2 == complex:
return complex
if type2 == complex:
return complex
else:
return int
"""
Calculate common type according to rule, that it must have the most adequate interpretation after conversion.
Look in tests for adequacy calibration.
:param type1: one of [bool, int, float, complex, list, range, tuple, str] types
:param type2: one of [bool, int, float, complex, list, range, tuple, str] types
:return: the most concrete common type, which can be used to convert both input values
"""