Skip to content

Commit e916cb9

Browse files
committed
Use type dict to convert primitive type
1 parent d6e32b3 commit e916cb9

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

python/spider-py/src/spider_py/type/tdl_convert.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
from spider_py.type.type import Double, Float, Int8, Int16, Int32, Int64
2121
from spider_py.type.utils import get_class_name
2222

23+
TypeDict = {
24+
Int8: Int8Type(),
25+
Int16: Int16Type(),
26+
Int32: Int32Type(),
27+
Int64: Int64Type(),
28+
Float: FloatType(),
29+
Double: DoubleType(),
30+
bool: BoolType(),
31+
}
32+
2333

2434
def _to_primitive_tdl_type(native_type: type | GenericAlias) -> TdlType | None:
2535
"""
@@ -30,27 +40,14 @@ def _to_primitive_tdl_type(native_type: type | GenericAlias) -> TdlType | None:
3040
- None if `native_type` is not a primitive Python type.
3141
:raises TypeError: If `native_type` is a primitive Python type not supported by TDL.
3242
"""
33-
tdl_type: TdlType | None = None
34-
if native_type is Int8:
35-
tdl_type = Int8Type()
36-
elif native_type is Int16:
37-
tdl_type = Int16Type()
38-
elif native_type is Int32:
39-
tdl_type = Int32Type()
40-
elif native_type is Int64:
41-
tdl_type = Int64Type()
42-
elif native_type is Float:
43-
tdl_type = FloatType()
44-
elif native_type is Double:
45-
tdl_type = DoubleType()
46-
elif native_type is bool:
47-
tdl_type = BoolType()
43+
if isinstance(native_type, type) and native_type in TypeDict:
44+
return TypeDict[native_type]
4845

4946
if native_type in (int, float, str, complex, bytes):
5047
msg = f"{native_type} is not a TDL type. Please use the corresponding TDL primitive type."
5148
raise TypeError(msg)
5249

53-
return tdl_type
50+
return None
5451

5552

5653
def to_tdl_type(native_type: type | GenericAlias) -> TdlType:

0 commit comments

Comments
 (0)