From 6e184bca97fa686d6bda8be246ec19dfe6bcb11a Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Thu, 2 Jul 2020 03:00:01 +0200 Subject: [PATCH] Remove most version_info.major usages --- jedi/inference/gradual/typeshed.py | 5 ++--- jedi/inference/helpers.py | 6 +----- jedi/inference/value/instance.py | 5 +---- jedi/inference/value/module.py | 3 --- jedi/plugins/stdlib.py | 7 +------ test/conftest.py | 4 +--- test/test_api/test_api.py | 4 +--- .../test_api_classes_follow_definition.py | 5 +---- test/test_api/test_call_signatures.py | 21 ++++++++----------- test/test_api/test_full_name.py | 3 --- test/test_api/test_keyword.py | 11 +--------- test/test_inference/test_annotations.py | 9 -------- test/test_inference/test_compiled.py | 19 ++++------------- .../test_gradual/test_typeshed.py | 7 ++----- test/test_inference/test_precedence.py | 2 -- test/test_inference/test_stdlib.py | 10 ++------- 16 files changed, 26 insertions(+), 95 deletions(-) diff --git a/jedi/inference/gradual/typeshed.py b/jedi/inference/gradual/typeshed.py index 3d1efb45..910b8646 100644 --- a/jedi/inference/gradual/typeshed.py +++ b/jedi/inference/gradual/typeshed.py @@ -58,15 +58,14 @@ def _create_stub_map(directory_path_info): def _get_typeshed_directories(version_info): - check_version_list = ['2and3', str(version_info.major)] + check_version_list = ['2and3', '3'] for base in ['stdlib', 'third_party']: base_path = os.path.join(TYPESHED_PATH, base) base_list = os.listdir(base_path) for base_list_entry in base_list: match = re.match(r'(\d+)\.(\d+)$', base_list_entry) if match is not None: - if int(match.group(1)) == version_info.major \ - and int(match.group(2)) <= version_info.minor: + if int(match.group(1)) == '3' and int(match.group(2)) <= version_info.minor: check_version_list.append(base_list_entry) for check_version in check_version_list: diff --git a/jedi/inference/helpers.py b/jedi/inference/helpers.py index 7a827d72..8f9fa7c8 100644 --- a/jedi/inference/helpers.py +++ b/jedi/inference/helpers.py @@ -122,11 +122,7 @@ def get_names_of_node(node): def is_string(value): - if value.inference_state.environment.version_info.major == 2: - str_classes = (unicode, bytes) - else: - str_classes = (unicode,) - return value.is_compiled() and isinstance(value.get_safe_value(default=None), str_classes) + return value.is_compiled() and isinstance(value.get_safe_value(default=None), str) def is_literal(value): diff --git a/jedi/inference/value/instance.py b/jedi/inference/value/instance.py index 5435c791..910ab831 100644 --- a/jedi/inference/value/instance.py +++ b/jedi/inference/value/instance.py @@ -258,10 +258,7 @@ class _BaseTreeInstance(AbstractInstanceValue): for generator in self.execute_function_slots(iter_slot_names): if generator.is_instance() and not generator.is_compiled(): # `__next__` logic. - if self.inference_state.environment.version_info.major == 2: - name = u'next' - else: - name = u'__next__' + name = u'__next__' next_slot_names = generator.get_function_slot_names(name) if next_slot_names: yield LazyKnownValues( diff --git a/jedi/inference/value/module.py b/jedi/inference/value/module.py index 12d2bfcf..35c0cd22 100644 --- a/jedi/inference/value/module.py +++ b/jedi/inference/value/module.py @@ -26,9 +26,6 @@ class _ModuleAttributeName(AbstractNameDefinition): def infer(self): if self._string_value is not None: s = self._string_value - if self.parent_context.inference_state.environment.version_info.major == 2 \ - and not isinstance(s, bytes): - s = s.encode('utf-8') return ValueSet([ create_simple_object(self.parent_context.inference_state, s) ]) diff --git a/jedi/plugins/stdlib.py b/jedi/plugins/stdlib.py index 2fee82d4..a270c06c 100644 --- a/jedi/plugins/stdlib.py +++ b/jedi/plugins/stdlib.py @@ -182,14 +182,9 @@ def argument_clinic(clinic_string, want_value=False, want_context=False, @argument_clinic('iterator[, default], /', want_inference_state=True) def builtins_next(iterators, defaults, inference_state): - if inference_state.environment.version_info.major == 2: - name = 'next' - else: - name = '__next__' - # TODO theoretically we have to check here if something is an iterator. # That is probably done by checking if it's not a class. - return defaults | iterators.py__getattribute__(name).execute_with_values() + return defaults | iterators.py__getattribute__('__next__').execute_with_values() @argument_clinic('iterator[, default], /') diff --git a/test/conftest.py b/test/conftest.py index 26254000..a0b2ea14 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -102,9 +102,7 @@ def collect_static_analysis_tests(base_dir, test_files): @pytest.fixture(scope='session') def venv_path(tmpdir_factory, environment): - if environment.version_info.major < 3: - pytest.skip("python -m venv does not exist in Python 2") - elif isinstance(environment, InterpreterEnvironment): + if isinstance(environment, InterpreterEnvironment): # The environment can be a tox virtualenv environment which we don't # want, so use the system environment. environment = get_system_environment( diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index c6fc33a7..5f862a60 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -337,9 +337,7 @@ def test_fuzzy_completion(Script): def test_math_fuzzy_completion(Script, environment): script = Script('import math\nmath.og') - expected = ['copysign', 'log', 'log10', 'log1p'] - if environment.version_info.major >= 3: - expected.append('log2') + expected = ['copysign', 'log', 'log10', 'log1p', 'log2'] completions = script.complete(fuzzy=True) assert expected == [comp.name for comp in completions] for c in completions: diff --git a/test/test_api/test_api_classes_follow_definition.py b/test/test_api/test_api_classes_follow_definition.py index 4bf3f254..96108141 100644 --- a/test/test_api/test_api_classes_follow_definition.py +++ b/test/test_api/test_api_classes_follow_definition.py @@ -36,10 +36,7 @@ def test_follow_import_incomplete(Script, environment): # incomplete `from * import` part datetime = check_follow_definition_types(Script, "from datetime import datetim") - if environment.version_info.major == 2: - assert datetime == ['class'] - else: - assert set(datetime) == {'class', 'instance'} # py3: builtin and pure py version + assert set(datetime) == {'class', 'instance'} # py3: builtin and pure py version # os.path check ospath = check_follow_definition_types(Script, "from os.path import abspat") assert set(ospath) == {'function'} diff --git a/test/test_api/test_call_signatures.py b/test/test_api/test_call_signatures.py index 3f305cb4..59e3a640 100644 --- a/test/test_api/test_call_signatures.py +++ b/test/test_api/test_call_signatures.py @@ -125,7 +125,7 @@ def test_get_signatures_whitespace(Script): abs( def x(): pass - """) + """) # noqa assert_signature(Script, s, 'abs', 0, line=1, column=5) @@ -239,13 +239,12 @@ def test_complex(Script, environment): func1, = sig1._name.infer() func2, = sig2._name.infer() - if environment.version_info.major == 3: - # Do these checks just for Python 3, I'm too lazy to deal with this - # legacy stuff. ~ dave. - assert get_signature(func1.tree_node) \ - == 'compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]' - assert get_signature(func2.tree_node) \ - == 'compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) ->\nPattern[AnyStr]' + # Do these checks just for Python 3, I'm too lazy to deal with this + # legacy stuff. ~ dave. + assert get_signature(func1.tree_node) \ + == 'compile(pattern: AnyStr, flags: _FlagsType = ...) -> Pattern[AnyStr]' + assert get_signature(func2.tree_node) \ + == 'compile(pattern: Pattern[AnyStr], flags: _FlagsType = ...) ->\nPattern[AnyStr]' # jedi-vim #70 s = """def foo(""" @@ -373,10 +372,8 @@ def test_keyword_argument_index(Script, environment): def get(source, column=None): return Script(source).get_signatures(column=column)[0] - # The signature of sorted changed from 2 to 3. - py2_offset = int(environment.version_info.major == 2) - assert get('sorted([], key=a').index == 1 + py2_offset - assert get('sorted([], key=').index == 1 + py2_offset + assert get('sorted([], key=a').index == 1 + assert get('sorted([], key=').index == 1 assert get('sorted([], no_key=a').index is None kw_func = 'def foo(a, b): pass\nfoo(b=3, a=4)' diff --git a/test/test_api/test_full_name.py b/test/test_api/test_full_name.py index 242e0c1d..44690141 100644 --- a/test/test_api/test_full_name.py +++ b/test/test_api/test_full_name.py @@ -46,9 +46,6 @@ class TestFullNameWithGotoDefinitions(MixinTestFullName, TestCase): operation = 'infer' def test_tuple_mapping(self): - if self.environment.version_info.major == 2: - pytest.skip('Python 2 also yields None.') - self.check(""" import re any_re = re.compile('.*') diff --git a/test/test_api/test_keyword.py b/test/test_api/test_keyword.py index e6cd0a56..dbf71b89 100644 --- a/test/test_api/test_keyword.py +++ b/test/test_api/test_keyword.py @@ -2,8 +2,6 @@ Test of keywords and ``jedi.keywords`` """ -import pytest - def test_goto_keyword(Script): """ @@ -17,10 +15,7 @@ def test_goto_keyword(Script): def test_keyword(Script, environment): """ github jedi-vim issue #44 """ defs = Script("print").infer() - if environment.version_info.major < 3: - assert defs == [] - else: - assert [d.docstring() for d in defs] + assert [d.docstring() for d in defs] assert Script("import").goto() == [] @@ -51,10 +46,6 @@ def test_keyword_attributes(Script): def test_none_keyword(Script, environment): - if environment.version_info.major == 2: - # Just don't care about Python 2 anymore, it's almost gone. - pytest.skip() - none, = Script('None').complete() assert not none.docstring() assert none.name == 'None' diff --git a/test/test_inference/test_annotations.py b/test/test_inference/test_annotations.py index 7940d7db..879b2ec5 100644 --- a/test/test_inference/test_annotations.py +++ b/test/test_inference/test_annotations.py @@ -9,9 +9,6 @@ def test_simple_annotations(Script, environment): If annotations adhere to PEP-0484, we use them (they override inference), else they are parsed but ignored """ - if environment.version_info.major == 2: - pytest.skip() - source = dedent("""\ def annot(a:3): return a @@ -45,18 +42,12 @@ def test_simple_annotations(Script, environment): r'1\n' ]) def test_illegal_forward_references(Script, environment, reference): - if environment.version_info.major == 2: - pytest.skip() - source = 'def foo(bar: "%s"): bar' % reference assert not Script(source).infer() def test_lambda_forward_references(Script, environment): - if environment.version_info.major == 2: - pytest.skip() - source = 'def foo(bar: "lambda: 3"): bar' # For now just receiving the 3 is ok. I'm doubting that this is what we diff --git a/test/test_inference/test_compiled.py b/test/test_inference/test_compiled.py index 2f7cf90b..89e87d41 100644 --- a/test/test_inference/test_compiled.py +++ b/test/test_inference/test_compiled.py @@ -16,11 +16,7 @@ def test_simple(inference_state, environment): upper, = obj.py__getattribute__(u'upper') objs = list(upper.execute_with_values()) assert len(objs) == 1 - if environment.version_info.major == 2: - expected = 'unicode' - else: - expected = 'str' - assert objs[0].name.string_name == expected + assert objs[0].name.string_name == 'str' def test_builtin_loading(inference_state): @@ -65,13 +61,9 @@ def test_string_literals(Script, environment): assert typ('""') == 'str' assert typ('r""') == 'str' - if environment.version_info.major > 2: - assert typ('br""') == 'bytes' - assert typ('b""') == 'bytes' - assert typ('u""') == 'str' - else: - assert typ('b""') == 'str' - assert typ('u""') == 'unicode' + assert typ('br""') == 'bytes' + assert typ('b""') == 'bytes' + assert typ('u""') == 'str' def test_method_completion(Script, environment): @@ -94,9 +86,6 @@ def test_time_docstring(Script): def test_dict_values(Script, environment): - if environment.version_info.major == 2: - # It looks like typeshed for Python 2 returns Any. - pytest.skip() assert Script('import sys\nsys.modules["alshdb;lasdhf"]').infer() diff --git a/test/test_inference/test_gradual/test_typeshed.py b/test/test_inference/test_gradual/test_typeshed.py index 1130a2b2..890de9b8 100644 --- a/test/test_inference/test_gradual/test_typeshed.py +++ b/test/test_inference/test_gradual/test_typeshed.py @@ -103,11 +103,8 @@ def test_sys_getwindowsversion(Script, environment): # This should only exist on Windows, but type inference should happen # everywhere. definitions = Script('import sys; sys.getwindowsversion().major').infer() - if environment.version_info.major == 2: - assert not definitions - else: - def_, = definitions - assert def_.name == 'int' + def_, = definitions + assert def_.name == 'int' def test_sys_hexversion(Script): diff --git a/test/test_inference/test_precedence.py b/test/test_inference/test_precedence.py index 9cb70a57..f92e2af5 100644 --- a/test/test_inference/test_precedence.py +++ b/test/test_inference/test_precedence.py @@ -10,8 +10,6 @@ import pytest pytest.param('... == ...', marks=pytest.mark.xfail), ]) def test_equals(Script, environment, source): - if environment.version_info.major < 3: - pytest.skip("Ellipsis does not exists in 2") script = Script(source) node = script._module_node.children[0] first, = script._get_module_context().infer_node(node) diff --git a/test/test_inference/test_stdlib.py b/test/test_inference/test_stdlib.py index 0e910d7f..151fd8f6 100644 --- a/test/test_inference/test_stdlib.py +++ b/test/test_inference/test_stdlib.py @@ -90,14 +90,8 @@ def test_re_sub(Script, environment): return {d.name for d in defs} names = run("import re; re.sub('a', 'a', 'f')") - if environment.version_info.major == 2: - assert names == {'str'} - else: - assert names == {'str'} + assert names == {'str'} # This param is missing because of overloading. names = run("import re; re.sub('a', 'a')") - if environment.version_info.major == 2: - assert names == {'str', 'unicode'} - else: - assert names == {'str', 'bytes'} + assert names == {'str', 'bytes'}