Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 00c8b0f

Browse files
devdoomaridevdoomari3
authored andcommitted
add class method extraction, proper options handling (needs refactoring later)
1 parent 08a43be commit 00c8b0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+463
-189
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ClassWithMethod:
2+
def some_method(self, arg: int) -> int:
3+
return id(self) + arg

py_type_extractor/type_extractor/__base__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from abc import ABC
1+
import abc
22
from typing import Dict, Union, List, Set, Optional
33

4-
from py_type_extractor.type_extractor.nodes.BaseNodeType import NodeType, BaseOption
4+
from py_type_extractor.type_extractor.nodes.BaseNodeType import NodeType
5+
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption
56

67

7-
class BaseTypeExtractor(ABC):
8+
class BaseTypeExtractor(metaclass=abc.ABCMeta):
89
collected_types: Dict[str, NodeType]
910

1011
def __init__(self):
@@ -14,14 +15,19 @@ def __init__(self):
1415
def to_collected_types_key(module_name, typ_name):
1516
return f"{module_name}___{typ_name}"
1617

18+
@abc.abstractmethod
1719
def params_to_nodes(
1820
self,
1921
params: Dict[str, Union[type, None]],
2022
param_names_list: List[str],
2123
) -> Dict[str, NodeType]:
2224
pass
2325

24-
def rawtype_to_node(self, typ) -> NodeType:
26+
@abc.abstractmethod
27+
def rawtype_to_node(
28+
self, typ,
29+
options: Optional[Set[BaseOption]] = None,
30+
) -> NodeType:
2531
pass
2632

2733
def add(

py_type_extractor/type_extractor/__tests__/test_class_of_generic_origin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import py_type_extractor.test_fixtures.generic_classes as t
2-
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test
1+
import py_type_extractor.test_fixtures.generic_classes as t
2+
from py_type_extractor.type_extractor.__tests__.utils.cleanup_node import cleanup
3+
from py_type_extractor.type_extractor.__tests__.utils.hash_test import hash_test
4+
from py_type_extractor.type_extractor.__tests__.utils.traverse_node import traverse
35
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
46
from py_type_extractor.type_extractor.nodes.TypeVarFound import TypeVarFound
57
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
68

79
module_name = t.__name__
810

11+
912
def test_class_of_generic_origin():
1013
type_extractor = TypeExtractor()
1114
type_extractor.add()(t.SomeGenericClass)

py_type_extractor/type_extractor/__tests__/test_class_with_circular_deps.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ def test_class_of_generic_origin():
1111
type_extractor.add()(ClassA)
1212
print(type_extractor)
1313

14-
# fixme: circular-dep hash
1514
hash_test(type_extractor)

py_type_extractor/type_extractor/__tests__/test_class_with_generic_instance_and_inheritance.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import py_type_extractor.test_fixtures.generic_classes_extended as t
21
import py_type_extractor.test_fixtures.generic_classes as t2
3-
4-
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test
2+
import py_type_extractor.test_fixtures.generic_classes_extended as t
3+
from py_type_extractor.type_extractor.__tests__.utils.cleanup_node import cleanup
4+
from py_type_extractor.type_extractor.__tests__.utils.traverse_node import traverse
55
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
66
from py_type_extractor.type_extractor.nodes.FixedGenericFound import FixedGenericFound
7-
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
87
from py_type_extractor.type_extractor.nodes.TypeVarFound import TypeVarFound
98
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
109
from py_type_extractor.type_extractor.utils.generics import flatten_generics_inheritance_to
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup
2+
from py_type_extractor.type_extractor.__tests__.utils.flags.KeepClassMethods import (
3+
keep_class_methods,
4+
)
5+
from py_type_extractor.type_extractor.__tests__.utils.hash_test import hash_test
6+
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
7+
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
8+
from py_type_extractor.type_extractor.nodes.__flags import FromMethod
9+
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
10+
import py_type_extractor.test_fixtures.class_with_methods as t
11+
12+
module_name = t.__name__
13+
14+
def test_class_with_method():
15+
type_extractor = TypeExtractor()
16+
type_extractor.add()(t.ClassWithMethod)
17+
18+
classes = {
19+
key: traverse(value, cleanup, None, { keep_class_methods })
20+
for (key, value) in type_extractor.collected_types.items()
21+
if isinstance(value, ClassFound)
22+
}
23+
24+
class_type_key = type_extractor.to_collected_types_key(
25+
module_name=module_name,
26+
typ_name=t.ClassWithMethod.__qualname__,
27+
)
28+
some_method = FunctionFound(
29+
name='some_method',
30+
module_name=module_name,
31+
params={
32+
'arg': int,
33+
},
34+
return_type=int,
35+
)
36+
class_found = ClassFound(
37+
methods={
38+
'some_method': some_method,
39+
},
40+
fields={},
41+
module_name=module_name,
42+
name=t.ClassWithMethod.__qualname__,
43+
)
44+
some_method.options = {
45+
FromMethod(
46+
method_name='some_method',
47+
)
48+
}
49+
assert classes[class_type_key] == class_found
50+
print(type_extractor)
51+
52+
hash_test(type_extractor)

py_type_extractor/type_extractor/__tests__/test_class_with_union_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22

33
import py_type_extractor.test_fixtures.union_type_class as t
4-
from py_type_extractor.type_extractor.nodes.BaseNodeType import BaseOption
4+
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption
55
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
66
from py_type_extractor.type_extractor.nodes.TypeOR import TypeOR
77
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test

py_type_extractor/type_extractor/__tests__/test_func_with_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22

33
from py_type_extractor.type_extractor.__tests__.utils import cleanup, traverse
4-
from py_type_extractor.type_extractor.nodes.BaseNodeType import BaseOption
4+
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption
55
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
66
from py_type_extractor.type_extractor.nodes.ListFound import ListFound
77
from py_type_extractor.type_extractor.type_extractor import TypeExtractor

py_type_extractor/type_extractor/__tests__/test_func_with_nested_arg_class.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def to_collected_types_key(a):
3434
name='test_func_with_nested_arg_class.ChildClass',
3535
module_name=module_name,
3636
fields={
37-
'return': None,
3837
'carg1': str,
3938
},
4039
doc='',
@@ -46,7 +45,6 @@ def to_collected_types_key(a):
4645
module_name=module_name,
4746
name="test_func_with_nested_arg_class.ParentClass",
4847
fields={
49-
'return': None,
5048
'parg1': str,
5149
'parg2': child_class,
5250
},

py_type_extractor/type_extractor/__tests__/test_func_with_typed_dict.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from py_type_extractor.test_fixtures.func_with_typed_dict import func_with_typed_dict
21
import py_type_extractor.test_fixtures.func_with_typed_dict as t
32
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
43
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
@@ -11,7 +10,7 @@
1110
def test_func_with_typed_dict():
1211
type_collector = TypeExtractor()
1312

14-
type_collector.add(None)(func_with_typed_dict)
13+
type_collector.add(None)(t.func_with_typed_dict)
1514

1615
collected_types_key = type_collector.to_collected_types_key(
1716
module_name=module_name,
@@ -22,7 +21,7 @@ def test_func_with_typed_dict():
2221

2322
to_compare_func = traverse(
2423
FunctionFound(
25-
name=func_with_typed_dict.__qualname__,
24+
name=t.func_with_typed_dict.__qualname__,
2625
module_name=module_name,
2726
params={
2827
'input': TypedDictFound(

0 commit comments

Comments
 (0)