Skip to content

Commit b8cb312

Browse files
committed
add typing.Tuple
1 parent 31792b5 commit b8cb312

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Tuple
2+
3+
4+
def func_with_tuple(input: Tuple[str, int]) -> Tuple[int, str]:
5+
return (Tuple[1], Tuple[0])
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from py_codegen.type_extractor.__tests__.utils import cleanup, traverse
2+
from py_codegen.type_extractor.nodes.FunctionFound import FunctionFound
3+
from py_codegen.type_extractor.nodes.TupleFound import TupleFound
4+
from py_codegen.type_extractor.type_extractor import TypeExtractor
5+
from py_codegen.test_fixtures.func_with_tuple import func_with_tuple
6+
7+
8+
def test_func_with_tuple():
9+
type_collector = TypeExtractor()
10+
11+
type_collector.add_function(None)(func_with_tuple)
12+
13+
# assert type_collector.classes == {}
14+
func_found_cleaned = cleanup(
15+
type_collector.functions[func_with_tuple.__qualname__],
16+
)
17+
assert func_found_cleaned == traverse(
18+
FunctionFound(
19+
name=func_with_tuple.__qualname__,
20+
params={
21+
'input': TupleFound([str, int]),
22+
},
23+
return_type=TupleFound([int, str]),
24+
),
25+
cleanup,
26+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import NamedTuple, List
2+
3+
from py_codegen.type_extractor.nodes.BaseNodeType import BaseNodeType, NodeType
4+
5+
6+
class TupleFound(NamedTuple, BaseNodeType): # type: ignore
7+
types: List[NodeType]

py_codegen/type_extractor/type_extractor.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
from collections import OrderedDict
44
from copy import deepcopy
55
from dataclasses import dataclass
6-
from typing import Callable, Dict, Union, List
6+
from typing import (
7+
Callable,
8+
Dict,
9+
Union,
10+
List,
11+
Tuple,
12+
)
713

814
from mypy_extensions import _TypedDictMeta # type: ignore
915

1016
from py_codegen.type_extractor.nodes.BaseNodeType import NodeType
1117
from py_codegen.type_extractor.nodes.DictFound import DictFound
1218
from py_codegen.type_extractor.nodes.ListFound import ListFound
1319
from py_codegen.type_extractor.nodes.NoneNode import NoneNode
20+
from py_codegen.type_extractor.nodes.TupleFound import TupleFound
1421
from py_codegen.type_extractor.nodes.TypedDictFound import TypedDictFound
1522
from py_codegen.type_extractor.nodes.ClassFound import ClassFound
1623
from py_codegen.type_extractor.nodes.UnknownFound import unknown_found
@@ -76,6 +83,8 @@ def __process_param(self, typ):
7683
return self.__process_union(typ)
7784
if typ_origin is dict or typ_origin is Dict:
7885
return self.__process_dict(typ)
86+
if typ_origin is tuple or typ_origin is Tuple:
87+
return self.__process_tuple(typ)
7988

8089
except:
8190
pass
@@ -128,6 +137,11 @@ def __process_list(self, list_typ):
128137
processed_typ = self.__process_param(list_typ.__args__[0])
129138
return ListFound(typ=processed_typ)
130139

140+
def __process_tuple(self, tuple_typ):
141+
assert(tuple_typ.__origin__ == tuple)
142+
processed_typ = [self.__process_param(param) for param in tuple_typ.__args__]
143+
return TupleFound(types=processed_typ)
144+
131145
def __process_union(self, union):
132146
assert(union.__origin__ is Union)
133147
types = union.__args__

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
setuptools==39.0.1
1+
setuptools==41.0.1
22
dataclasses==0.6
33
extractor==0.5
44
typing==3.6.6

0 commit comments

Comments
 (0)