From ab80646b860d89f0fea2c3aea262cd922c7413d7 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Sun, 11 Aug 2019 01:28:09 +0200 Subject: [PATCH] Fix an issue with type vars that might have been a problem for other things as well --- jedi/evaluate/compiled/mixed.py | 6 +++++- test/test_api/test_interpreter.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/jedi/evaluate/compiled/mixed.py b/jedi/evaluate/compiled/mixed.py index 9c419e2f..37d9fbe2 100644 --- a/jedi/evaluate/compiled/mixed.py +++ b/jedi/evaluate/compiled/mixed.py @@ -266,7 +266,11 @@ def _create(evaluator, access_handle, parent_context, *args): if name is not None: evaluator.module_cache.add(string_names, ContextSet([module_context])) else: - assert parent_context.tree_node.get_root_node() == module_node + if parent_context.tree_node.get_root_node() != module_node: + # This happens e.g. when __module__ is wrong, or when using + # TypeVar('foo'), where Jedi uses 'foo' as the name and + # Python's TypeVar('foo').__module__ will be typing. + return ContextSet({compiled_object}) module_context = parent_context.get_root_context() tree_contexts = ContextSet({ diff --git a/test/test_api/test_interpreter.py b/test/test_api/test_interpreter.py index 3bbe886c..1ed63d13 100644 --- a/test/test_api/test_interpreter.py +++ b/test/test_api/test_interpreter.py @@ -473,3 +473,12 @@ def test_partial_signatures(code, expected, index): assert sig.name == 'partial' assert [p.name for p in sig.params] == expected assert index == sig.index + + +@pytest.mark.skipif(sys.version_info[0] == 2, reason="Ignore Python 2, because EOL") +def test_type_var(): + """This was an issue before, see Github #1369""" + import typing + x = typing.TypeVar('myvar') + def_, = jedi.Interpreter('x', [locals()]).goto_definitions() + assert def_.name == 'TypeVar'