Skip to content

Commit 621b7d3

Browse files
emmatypinggvanrossum
authored andcommitted
Set python_version to default to sys.version_info (#4686)
This sets the default Python version used for type checking to `sys.version_info`. Fixes #4620. The design of this is such that we set tests to default to the running Python whenever possible, but modify tests that use new syntax and libraries to run on Python 3.5 or 3.6. Example output of failing tests on 3.4 before test changes https://gist.github.com/ethanhs/f782bec70eab0678d9e869465b40a571#file-output-log-L512. This was split out of #4403.
1 parent c646d62 commit 621b7d3

File tree

9 files changed

+29
-15
lines changed

9 files changed

+29
-15
lines changed

mypy/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self) -> None:
5252

5353
# -- build options --
5454
self.build_type = BuildType.STANDARD
55-
self.python_version = defaults.PYTHON3_VERSION
55+
self.python_version = sys.version_info[:2] # type: Tuple[int, int]
5656
self.platform = sys.platform
5757
self.custom_typing_module = None # type: Optional[str]
5858
self.custom_typeshed_dir = None # type: Optional[str]

mypy/test/testdiff.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from mypy import build
77
from mypy.build import BuildSource
8+
from mypy.defaults import PYTHON3_VERSION
89
from mypy.errors import CompileError
910
from mypy.nodes import MypyFile
1011
from mypy.options import Options
@@ -53,6 +54,7 @@ def build(self, source: str) -> Tuple[List[str], Optional[Dict[str, MypyFile]]]:
5354
options.use_builtins_fixtures = True
5455
options.show_traceback = True
5556
options.cache_dir = os.devnull
57+
options.python_version = PYTHON3_VERSION
5658
try:
5759
result = build.build(sources=[BuildSource('main', None, source)],
5860
options=options,

mypy/test/testmerge.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mypy import build
88
from mypy.build import BuildManager, BuildSource, State, Graph
9+
from mypy.defaults import PYTHON3_VERSION
910
from mypy.errors import Errors, CompileError
1011
from mypy.nodes import (
1112
Node, MypyFile, SymbolTable, SymbolTableNode, TypeInfo, Expression, Var, TypeVarExpr,
@@ -106,6 +107,7 @@ def build(self, source: str) -> Tuple[List[str], Optional[BuildManager], Dict[st
106107
options.fine_grained_incremental = True
107108
options.use_builtins_fixtures = True
108109
options.show_traceback = True
110+
options.python_version = PYTHON3_VERSION
109111
main_path = os.path.join(test_temp_dir, 'main')
110112
with open(main_path, 'w') as f:
111113
f.write(source)

mypy/test/testpythoneval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from typing import List
2121

22+
from mypy.defaults import PYTHON3_VERSION
2223
from mypy.test.config import test_temp_dir
2324
from mypy.test.data import DataDrivenTestCase, DataSuite
2425
from mypy.test.helpers import assert_string_arrays_equal, run_command
@@ -60,6 +61,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
6061
return
6162
else:
6263
interpreter = python3_path
64+
mypy_cmdline.append('--python-version={}'.format('.'.join(map(str, PYTHON3_VERSION))))
6365

6466
# Write the program to a file.
6567
program = '_' + testcase.name + '.py'

mypy/test/testsemanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mypy import build
88
from mypy.build import BuildSource
9+
from mypy.defaults import PYTHON3_VERSION
910
from mypy.test.helpers import (
1011
assert_string_arrays_equal, normalize_error_messages, testfile_pyversion,
1112
)
@@ -38,6 +39,7 @@ def get_semanal_options() -> Options:
3839
options.use_builtins_fixtures = True
3940
options.semantic_analysis_only = True
4041
options.show_traceback = True
42+
options.python_version = PYTHON3_VERSION
4143
return options
4244

4345

mypy/waiter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def load_log_file(self) -> Optional[List[Dict[str, Dict[str, Any]]]]:
160160
test_log = json.load(fp)
161161
except FileNotFoundError:
162162
test_log = []
163-
except json.JSONDecodeError:
163+
except ValueError:
164164
print('corrupt test log file {}'.format(self.FULL_LOG_FILENAME), file=sys.stderr)
165165
test_log = []
166166
return test_log

runtests.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ def add_mypy_cmd(self, name: str, mypy_args: List[str], cwd: Optional[str] = Non
7878
def add_mypy(self, name: str, *args: str, cwd: Optional[str] = None) -> None:
7979
self.add_mypy_cmd(name, list(args), cwd=cwd)
8080

81-
def add_mypy_modules(self, name: str, modules: Iterable[str],
82-
cwd: Optional[str] = None) -> None:
83-
args = list(itertools.chain(*(['-m', mod] for mod in modules)))
81+
def add_mypy_modules(self, name: str, modules: Iterable[str], cwd: Optional[str] = None,
82+
extra_args: Optional[List[str]] = None) -> None:
83+
args = extra_args or []
84+
args.extend(list(itertools.chain(*(['-m', mod] for mod in modules))))
8485
self.add_mypy_cmd(name, args, cwd=cwd)
8586

8687
def add_mypy_package(self, name: str, packagename: str, *flags: str) -> None:
@@ -256,7 +257,8 @@ def add_stubs(driver: Driver) -> None:
256257
module = file_to_module(f[len(stubdir) + 1:])
257258
modules.add(module)
258259

259-
driver.add_mypy_modules('stubs', sorted(modules))
260+
# these require at least 3.5 otherwise it will fail trying to import zipapp
261+
driver.add_mypy_modules('stubs', sorted(modules), extra_args=['--python-version=3.5'])
260262

261263

262264
def add_stdlibsamples(driver: Driver) -> None:
@@ -276,7 +278,11 @@ def add_stdlibsamples(driver: Driver) -> None:
276278

277279
def add_samples(driver: Driver) -> None:
278280
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
279-
driver.add_mypy('file %s' % f, f)
281+
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
282+
# This test requires 3.5 for async functions
283+
driver.add_mypy_cmd('file {}'.format(f), ['--python-version=3.5', f])
284+
else:
285+
driver.add_mypy('file %s' % f, f)
280286

281287

282288
def usage(status: int) -> None:

test-data/unit/cmdline.test

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ m.py:6: error: Explicit "Any" is not allowed
581581
m.py:9: error: Explicit "Any" is not allowed
582582

583583
[case testDisallowAnyExplicitVarDeclaration]
584-
# cmd: mypy m.py
584+
# cmd: mypy --python-version=3.6 m.py
585585

586586
[file mypy.ini]
587587
[[mypy]
@@ -601,7 +601,7 @@ m.py:3: error: Explicit "Any" is not allowed
601601
m.py:5: error: Explicit "Any" is not allowed
602602

603603
[case testDisallowAnyExplicitGenericVarDeclaration]
604-
# cmd: mypy m.py
604+
# cmd: mypy --python-version=3.6 m.py
605605

606606
[file mypy.ini]
607607
[[mypy]
@@ -785,7 +785,7 @@ N = TypedDict('N', {'x': str, 'y': List}) # no error
785785
m.py:4: error: Explicit "Any" is not allowed
786786

787787
[case testDisallowAnyGenericsTupleNoTypeParams]
788-
# cmd: mypy m.py
788+
# cmd: mypy --python-version=3.6 m.py
789789
[file mypy.ini]
790790
[[mypy]
791791
[[mypy-m]
@@ -821,7 +821,7 @@ def g(s: List[Tuple[str, str]]) -> None: pass # no error
821821
m.py:3: error: Missing type parameters for generic type
822822

823823
[case testDisallowAnyGenericsTypeType]
824-
# cmd: mypy m.py
824+
# cmd: mypy --python-version=3.6 m.py
825825
[file mypy.ini]
826826
[[mypy]
827827
[[mypy-m]
@@ -858,7 +858,7 @@ def g(l: L[str]) -> None: pass # no error
858858
m.py:5: error: Missing type parameters for generic type
859859

860860
[case testDisallowAnyGenericsGenericAlias]
861-
# cmd: mypy m.py
861+
# cmd: mypy --python-version=3.6 m.py
862862
[file mypy.ini]
863863
[[mypy]
864864
[[mypy-m]
@@ -882,7 +882,7 @@ m.py:7: error: Missing type parameters for generic type
882882
m.py:11: error: Missing type parameters for generic type
883883

884884
[case testDisallowAnyGenericsPlainList]
885-
# cmd: mypy m.py
885+
# cmd: mypy --python-version=3.6 m.py
886886
[file mypy.ini]
887887
[[mypy]
888888
[[mypy-m]
@@ -906,7 +906,7 @@ m.py:8: error: Need type annotation for 'x'
906906
m.py:9: error: Missing type parameters for generic type
907907

908908
[case testDisallowAnyGenericsCustomGenericClass]
909-
# cmd: mypy m.py
909+
# cmd: mypy --python-version=3.6 m.py
910910
[file mypy.ini]
911911
[[mypy]
912912
[[mypy-m]

test-data/unit/reports.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Total 0 16 100.00%
281281

282282

283283
[case testAnyExpressionsReportTypesOfAny]
284-
# cmd: mypy --any-exprs-report report n.py
284+
# cmd: mypy --python-version=3.6 --any-exprs-report report n.py
285285

286286
[file n.py]
287287
from typing import Any, List

0 commit comments

Comments
 (0)