From 3852431549d3e7c4f5c99d0f907c71b40110f8df Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 31 Dec 2015 01:59:34 +0100 Subject: [PATCH] typing.Union and typing.Optional --- jedi/evaluate/pep0484.py | 7 +++++++ test/completion/pep0484_typing.py | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py index 422ca85f..8b4381ff 100644 --- a/jedi/evaluate/pep0484.py +++ b/jedi/evaluate/pep0484.py @@ -110,6 +110,13 @@ def get_types_for_typing_module(evaluator, typ, node): nodes = node.children[::2] # skip the commas else: nodes = [node] + del node + + # hacked in Union and Optional, since it's hard to do nicely in parsed code + if typ.name.value == "Union": + return set().union(*[evaluator.eval_element(node) for node in nodes]) + if typ.name.value == "Optional": + return evaluator.eval_element(nodes[0]) typing = _get_typing_replacement_module() factories = evaluator.find_types(typing, "factory") diff --git a/test/completion/pep0484_typing.py b/test/completion/pep0484_typing.py index 8b8ad852..6b085fc2 100644 --- a/test/completion/pep0484_typing.py +++ b/test/completion/pep0484_typing.py @@ -154,5 +154,32 @@ def mapping( ##? Value() --- TODO fix support for tuple assignment value +def union( + p: typing.Union[int], + q: typing.Union[int, int], + r: typing.Union[int, str, "int"], + s: typing.Union[int, typing.Union[str, "typing.Union['float', 'dict']"]], + t: typing.Union[int, None]): + #? int() + p + #? int() + q + #? int() str() + r + #? int() str() float() dict() + s + #? int() + t + +def optional( + p: typing.Optional[int]): + """ + Optional does not do anything special. However it should be recognised + as being of that type. Jedi doesn't do anything with the extra into that + it can be None as well + """ + #? int() + p + class ForwardReference: pass