Skip to content

Commit 74679ce

Browse files
committed
wip - clean up tests, fix bugs
1 parent 8364e3d commit 74679ce

File tree

7 files changed

+50
-39
lines changed

7 files changed

+50
-39
lines changed

py_codegen/plugins/typescript.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from py_codegen.type_extractor.type_extractor import CollectType
1+
from py_codegen.type_extractor.type_extractor import TypeExtractor
22

3-
def extract_class_to_interfaces(collected_types: CollectType) -> str:
3+
def extract_class_to_interfaces(collected_types: TypeExtractor) -> str:
44
interfaces = collected_types.classes
55
return ''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from py_codegen.type_extractor.type_extractor import TypeExtractor
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from py_codegen.type_extractor import CollectType
1+
from py_codegen.type_extractor import TypeExtractor
22

33

44
def test_func_with_arg_class():
5-
type_collector = CollectType()
5+
type_collector = TypeExtractor()
66

77
class ArgClass:
88
arg1: str
@@ -11,10 +11,4 @@ class ArgClass:
1111
@type_collector.add_function(None)
1212
def func_with_arg_class(a: ArgClass) -> ArgClass:
1313
return a
14-
classes_found = type_collector.classes.items()
15-
16-
import pdb;pdb.set_trace()
17-
print('type_collector.classes', type_collector.classes)
18-
19-
# if __name__ == '__main__':
20-
test_func_with_arg_class()
14+
type_collector.classes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from py_codegen.type_extractor import TypeExtractor
2+
3+
4+
def test_func_with_builtin_type_args():
5+
type_collector = TypeExtractor()
6+
7+
@type_collector.add_function(None)
8+
def func_with_builtin_args(a: int) -> int:
9+
return a + 1
10+
classes_found = type_collector.classes.items()
11+
12+
import pdb;pdb.set_trace()
13+
print('type_collector.classes', type_collector.classes)

py_codegen/type_extractor/__tests__/test_func_with_nested_arg_class.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from py_codegen.type_extractor import CollectType
1+
from py_codegen.type_extractor import TypeExtractor
22

33

44
def test_func_with_arg_class():
5-
typeCollector = CollectType()
5+
typeCollector = TypeExtractor()
66
class ArgClass:
77
arg1: str
88
arg2: int

py_codegen/type_extractor/__tests__/test_various_classes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from py_codegen.type_extractor import CollectType
1+
from py_codegen.type_extractor import TypeExtractor
22

33

44
def test_func_with_arg_class():
5-
typeCollector = CollectType()
5+
typeCollector = TypeExtractor()
66
class ArgClass:
77
arg1: str
88
arg2: int

py_codegen/type_extractor/type_extractor.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
from .FunctionFound import FunctionFound
1010

1111
def is_builtin(something):
12-
return inspect.getmodule(something) is not builtins
12+
return inspect.getmodule(something) is builtins
1313

14-
class CollectType:
15-
functions: Dict[str, FunctionFound] = dict()
14+
class TypeExtractor:
15+
functions: Dict[str, FunctionFound]
16+
classes: Dict[str, ClassFound]
1617

17-
classes: Dict[str, ClassFound] = dict()
18+
def __init__(self):
19+
self.functions = dict()
20+
self.classes = dict()
1821

1922
def add_function(self, options):
2023
def add_function_decoration(func: Callable):
@@ -29,21 +32,23 @@ def add_function_decoration(func: Callable):
2932
function_found
3033
)
3134
self.functions[function_found.name] = function_found
32-
# import pdb; pdb.set_trace()
3335
return func
3436
return add_function_decoration
3537

3638
def __process_params(self, params: OrderedDict):
37-
processed_params = dict()
38-
for key, value in params.items():
39-
if inspect.isclass(value) and not is_builtin(value):
40-
class_found = self.__to_class_found(value)
41-
processed_params[key] = class_found
42-
if inspect.isfunction(value):
43-
function_found = self.__to_function_found(value)
44-
processed_params[key] = function_found
45-
print("key", key, " / value", value)
46-
return processed_params
39+
return {k: self.__process_param(v) for k, v in params.items()}
40+
41+
def __process_param(self, value):
42+
if is_builtin(value):
43+
return value
44+
if inspect.isclass(value) and not is_builtin(value):
45+
# self.add_class(None)(value)
46+
class_found = self.__to_class_found(value)
47+
self.__add_class_found(class_found)
48+
return class_found
49+
if inspect.isfunction(value) and not is_builtin(value):
50+
function_found = self.__to_function_found(value)
51+
return function_found
4752

4853
def __to_class_found(self, _class):
4954
_data_class = dataclass(_class)
@@ -66,27 +71,25 @@ def __to_function_found(self, func: Callable) -> FunctionFound:
6671
module = inspect.getmodule(func)
6772
filename = module.__file__
6873
params = self.__process_params(argspec.annotations)
74+
return_type = self.__process_param(signature.return_annotation)
6975
func_found = FunctionFound(
7076
name=func.__name__,
7177
filePath=filename,
7278
raw_params=argspec.annotations,
7379
params=params,
7480
doc=func.__doc__,
7581
func=func,
76-
return_type=signature.return_annotation,
82+
return_type=return_type,
7783
)
7884
return func_found
7985

80-
def __to_dictionary_key(self, obj):
81-
module = inspect.getmodule(obj)
82-
filename = module.__file__
83-
return obj.__name + '::' + filename
86+
def __add_class_found(self, class_found: ClassFound):
87+
self.classes[class_found.name] = class_found
8488

8589
def add_class(self, options):
8690
def add_class_decoration(_class):
91+
if is_builtin(_class):
92+
return
8793
class_found = self.__to_class_found(_class)
88-
self.classes[self.__to_dictionary_key(_class)] = class_found
89-
import pdb;pdb.set_trace()
94+
self.__add_class_found(class_found)
9095
return add_class_decoration
91-
92-
# def get_classes(self):

0 commit comments

Comments
 (0)