Permalink
Browse files

Merge branch 'recursive-ann-node-to-type' of https://github.com/justi…

  • Loading branch information...
david-yz-liu committed Aug 20, 2018
2 parents eba06c7 + cce0e42 commit 5dfd7a300aac376edfa341c1d05830b9bb84c8ae
Showing with 22 additions and 1 deletion.
  1. +10 −0 python_ta/typecheck/base.py
  2. +12 −1 tests/test_type_inference/test_annassign.py
@@ -941,6 +941,11 @@ def _ann_node_to_type(node: astroid.Name) -> TypeResult:
# Attempted to create ForwardRef with invalid string
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
ann_node_type is getattr(typing, getattr(ann_node_type, '_name', '') or '', None)):
if ann_node_type == Dict:
@@ -950,6 +955,11 @@ def _ann_node_to_type(node: astroid.Name) -> TypeResult:
ann_type = wrap_container(ann_node_type, Any)
else:
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:
try:
_type_check(ann_node_type, '')
@@ -5,7 +5,7 @@
from tests.custom_hypothesis_support import lookup_type
import hypothesis.strategies as hs
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.tools import eq_
settings.load_profile("pyta")
@@ -246,5 +246,16 @@ def test_annotation_forward_ref_space():
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__':
nose.main()

0 comments on commit 5dfd7a3

Please sign in to comment.