Skip to content

Feature/add class method extraction #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions py_type_extractor/test_fixtures/class_with_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SomeArgClass:
a: int


class ClassWithMethod:
def some_method(self, arg: SomeArgClass) -> int:
return id(self) + arg.a
15 changes: 11 additions & 4 deletions py_type_extractor/type_extractor/__base__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from abc import ABC
import abc
from typing import Dict, Union, List, Set, Optional

from py_type_extractor.type_extractor.nodes.BaseNodeType import NodeType, BaseOption
from py_type_extractor.type_extractor.nodes.BaseNodeType import NodeType
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption


class BaseTypeExtractor(ABC):
class BaseTypeExtractor(metaclass=abc.ABCMeta):
collected_types: Dict[str, NodeType]

def __init__(self):
Expand All @@ -14,14 +15,20 @@ def __init__(self):
def to_collected_types_key(module_name, typ_name):
return f"{module_name}___{typ_name}"

@abc.abstractmethod
def params_to_nodes(
self,
params: Dict[str, Union[type, None]],
param_names_list: List[str],
options: Optional[Set[BaseOption]] = None,
) -> Dict[str, NodeType]:
pass

def rawtype_to_node(self, typ) -> NodeType:
@abc.abstractmethod
def rawtype_to_node(
self, typ,
options: Optional[Set[BaseOption]] = None,
) -> NodeType:
pass

def add(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import py_type_extractor.test_fixtures.generic_classes as t
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test
import py_type_extractor.test_fixtures.generic_classes as t
from py_type_extractor.type_extractor.__tests__.utils.cleanup_node import cleanup
from py_type_extractor.type_extractor.__tests__.utils.hash_test import hash_test
from py_type_extractor.type_extractor.__tests__.utils.traverse_node import traverse
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
from py_type_extractor.type_extractor.nodes.TypeVarFound import TypeVarFound
from py_type_extractor.type_extractor.type_extractor import TypeExtractor

module_name = t.__name__


def test_class_of_generic_origin():
type_extractor = TypeExtractor()
type_extractor.add()(t.SomeGenericClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ def test_class_of_generic_origin():
type_extractor.add()(ClassA)
print(type_extractor)

# fixme: circular-dep hash
hash_test(type_extractor)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import py_type_extractor.test_fixtures.generic_classes_extended as t
import py_type_extractor.test_fixtures.generic_classes as t2

from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test
import py_type_extractor.test_fixtures.generic_classes_extended as t
from py_type_extractor.type_extractor.__tests__.utils.cleanup_node import cleanup
from py_type_extractor.type_extractor.__tests__.utils.traverse_node import traverse
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
from py_type_extractor.type_extractor.nodes.FixedGenericFound import FixedGenericFound
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
from py_type_extractor.type_extractor.nodes.TypeVarFound import TypeVarFound
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
from py_type_extractor.type_extractor.utils.generics import flatten_generics_inheritance_to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup
from py_type_extractor.type_extractor.__tests__.utils.flags.KeepClassMethods import (
keep_class_methods,
)
from py_type_extractor.type_extractor.__tests__.utils.hash_test import hash_test
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
from py_type_extractor.type_extractor.nodes.__flags import FromMethod
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
import py_type_extractor.test_fixtures.class_with_methods as t

module_name = t.__name__

def test_class_with_method():
type_extractor = TypeExtractor()
type_extractor.add()(t.ClassWithMethod)

classes = {
key: traverse(value, cleanup, None, { keep_class_methods })
for (key, value) in type_extractor.collected_types.items()
if isinstance(value, ClassFound)
}

class_type_key = type_extractor.to_collected_types_key(
module_name=module_name,
typ_name=t.ClassWithMethod.__qualname__,
)
some_arg_class = ClassFound(
fields={
'a': int,
},
module_name=module_name,
name=t.SomeArgClass.__qualname__,
)
some_method = FunctionFound(
name='some_method',
module_name=module_name,
params={
'arg': some_arg_class,
},
return_type=int,
options={
FromMethod('some_method'),
}
)
class_with_method = ClassFound(
methods={
'some_method': some_method,
},
fields={},
module_name=module_name,
name=t.ClassWithMethod.__qualname__,
)
assert classes[class_type_key] == class_with_method
print(type_extractor)

hash_test(type_extractor)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

import py_type_extractor.test_fixtures.union_type_class as t
from py_type_extractor.type_extractor.nodes.BaseNodeType import BaseOption
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
from py_type_extractor.type_extractor.nodes.TypeOR import TypeOR
from py_type_extractor.type_extractor.__tests__.utils import traverse, cleanup, hash_test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass

from py_type_extractor.type_extractor.__tests__.utils import cleanup, traverse
from py_type_extractor.type_extractor.nodes.BaseNodeType import BaseOption
from py_type_extractor.type_extractor.nodes.BaseOption import BaseOption
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
from py_type_extractor.type_extractor.nodes.ListFound import ListFound
from py_type_extractor.type_extractor.type_extractor import TypeExtractor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def to_collected_types_key(a):
name='test_func_with_nested_arg_class.ChildClass',
module_name=module_name,
fields={
'return': None,
'carg1': str,
},
doc='',
Expand All @@ -46,7 +45,6 @@ def to_collected_types_key(a):
module_name=module_name,
name="test_func_with_nested_arg_class.ParentClass",
fields={
'return': None,
'parg1': str,
'parg2': child_class,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from py_type_extractor.test_fixtures.func_with_typed_dict import func_with_typed_dict
import py_type_extractor.test_fixtures.func_with_typed_dict as t
from py_type_extractor.type_extractor.nodes.ClassFound import ClassFound
from py_type_extractor.type_extractor.nodes.FunctionFound import FunctionFound
Expand All @@ -11,7 +10,7 @@
def test_func_with_typed_dict():
type_collector = TypeExtractor()

type_collector.add(None)(func_with_typed_dict)
type_collector.add(None)(t.func_with_typed_dict)

collected_types_key = type_collector.to_collected_types_key(
module_name=module_name,
Expand All @@ -22,7 +21,7 @@ def test_func_with_typed_dict():

to_compare_func = traverse(
FunctionFound(
name=func_with_typed_dict.__qualname__,
name=t.func_with_typed_dict.__qualname__,
module_name=module_name,
params={
'input': TypedDictFound(
Expand Down
132 changes: 0 additions & 132 deletions py_type_extractor/type_extractor/__tests__/utils.py

This file was deleted.

5 changes: 5 additions & 0 deletions py_type_extractor/type_extractor/__tests__/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .cleanup_node import cleanup
from .hash_test import hash_test
from .traverse_node import (
traverse,
)
Loading