Skip to content

Commit

Permalink
Merge branch 'recursive-ann-node-to-type' of https://github.com/justi…
Browse files Browse the repository at this point in the history
  • Loading branch information
david-yz-liu committed Aug 20, 2018
2 parents eba06c7 + cce0e42 commit 5dfd7a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions python_ta/typecheck/base.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -941,6 +941,11 @@ def _ann_node_to_type(node: astroid.Name) -> TypeResult:
# Attempted to create ForwardRef with invalid string # Attempted to create ForwardRef with invalid string
return TypeFailAnnotationInvalid(node) return TypeFailAnnotationInvalid(node)


ann_type = _generic_to_annotation(ann_node_type, node)
return ann_type


def _generic_to_annotation(ann_node_type: type, node: NodeNG) -> TypeResult:
if (isinstance(ann_node_type, _GenericAlias) and if (isinstance(ann_node_type, _GenericAlias) and
ann_node_type is getattr(typing, getattr(ann_node_type, '_name', '') or '', None)): ann_node_type is getattr(typing, getattr(ann_node_type, '_name', '') or '', None)):
if ann_node_type == Dict: if ann_node_type == Dict:
Expand All @@ -950,6 +955,11 @@ def _ann_node_to_type(node: astroid.Name) -> TypeResult:
ann_type = wrap_container(ann_node_type, Any) ann_type = wrap_container(ann_node_type, Any)
else: else:
ann_type = wrap_container(ann_node_type, Any) ann_type = wrap_container(ann_node_type, Any)
elif isinstance(ann_node_type, _GenericAlias):
parsed_args = []
for arg in ann_node_type.__args__:
_generic_to_annotation(arg, node) >> parsed_args.append
ann_type = wrap_container(ann_node_type, *parsed_args)
else: else:
try: try:
_type_check(ann_node_type, '') _type_check(ann_node_type, '')
Expand Down
13 changes: 12 additions & 1 deletion tests/test_type_inference/test_annassign.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tests.custom_hypothesis_support import lookup_type from tests.custom_hypothesis_support import lookup_type
import hypothesis.strategies as hs import hypothesis.strategies as hs
from python_ta.typecheck.base import _node_to_type, TypeFail, TypeFailAnnotationInvalid, TypeFailUnify, NoType, _gorg from python_ta.typecheck.base import _node_to_type, TypeFail, TypeFailAnnotationInvalid, TypeFailUnify, NoType, _gorg
from typing import List, Set, Dict, Any, Tuple, _GenericAlias from typing import List, Set, Dict, Any, Tuple, Union, _GenericAlias
from nose import SkipTest from nose import SkipTest
from nose.tools import eq_ from nose.tools import eq_
settings.load_profile("pyta") settings.load_profile("pyta")
Expand Down Expand Up @@ -246,5 +246,16 @@ def test_annotation_forward_ref_space():
assert isinstance(ann_node.inf_type, TypeFailAnnotationInvalid) assert isinstance(ann_node.inf_type, TypeFailAnnotationInvalid)




def test_annotation_union_list():
src = """
x: Union[List, int]
"""
module, inferer = cs._parse_text(src, reset=True)
for ann_node in module.nodes_of_class(astroid.AnnAssign):
assert not isinstance(ann_node.inf_type, TypeFail)
x_type = lookup_type(inferer, module, 'x')
eq_(x_type, Union[List[Any], int])


if __name__ == '__main__': if __name__ == '__main__':
nose.main() nose.main()

0 comments on commit 5dfd7a3

Please sign in to comment.