Skip to content

Commit 8434c8a

Browse files
gvanrossumJukkaL
authored andcommitted
Drop Python 3.2 support. (#3244)
* Drop Python 3.2 support. Tighten parsing of Python version on command line and in config file. The only versions now supported are: - 2.7 - 3.3 and higher 3.x versions Closes #3231. * Add tests for python_version parsing in mypy.ini * Add positive acceptance tests for 2.7, 3.3, 3.6 (assuming 3.x is a range check)
1 parent 853a4c1 commit 8434c8a

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

mypy/main.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,22 @@ def __getattr__(self, name: str) -> Any:
121121

122122
def parse_version(v: str) -> Tuple[int, int]:
123123
m = re.match(r'\A(\d)\.(\d+)\Z', v)
124-
if m:
125-
return int(m.group(1)), int(m.group(2))
126-
else:
124+
if not m:
127125
raise argparse.ArgumentTypeError(
128126
"Invalid python version '{}' (expected format: 'x.y')".format(v))
127+
major, minor = int(m.group(1)), int(m.group(2))
128+
if major == 2:
129+
if minor != 7:
130+
raise argparse.ArgumentTypeError(
131+
"Python 2.{} is not supported (must be 2.7)".format(minor))
132+
elif major == 3:
133+
if minor <= 2:
134+
raise argparse.ArgumentTypeError(
135+
"Python 3.{} is not supported (must be 3.3 or higher)".format(minor))
136+
else:
137+
raise argparse.ArgumentTypeError(
138+
"Python major version '{}' out of range (must be 2 or 3)".format(major))
139+
return major, minor
129140

130141

131142
# Make the help output a little less jarring.
@@ -553,8 +564,7 @@ def get_init_file(dir: str) -> Optional[str]:
553564
# exists to specify types for values initialized to None or container
554565
# types.
555566
config_types = {
556-
# TODO: Check validity of python version
557-
'python_version': lambda s: tuple(map(int, s.split('.'))),
567+
'python_version': parse_version,
558568
'strict_optional_whitelist': lambda s: s.split(),
559569
'custom_typing_module': str,
560570
'custom_typeshed_dir': str,
@@ -663,7 +673,11 @@ def parse_section(prefix: str, template: Options,
663673
if ct is bool:
664674
v = section.getboolean(key) # type: ignore # Until better stub
665675
elif callable(ct):
666-
v = ct(section.get(key))
676+
try:
677+
v = ct(section.get(key))
678+
except argparse.ArgumentTypeError as err:
679+
print("%s: %s: %s" % (prefix, key, err), file=sys.stderr)
680+
continue
667681
else:
668682
print("%s: Don't know what type %s should have" % (prefix, key), file=sys.stderr)
669683
continue

mypy/test/testcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import Dict, List, Optional, Set, Tuple
1111

1212
from mypy import build, defaults
13-
from mypy.main import parse_version, process_options
13+
from mypy.main import process_options
1414
from mypy.build import BuildSource, find_module_clear_caches
1515
from mypy.myunit import AssertionFailure
1616
from mypy.test.config import test_temp_dir, test_data_prefix

test-data/unit/check-unreachable-code.test

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ x = 1
404404
[out]
405405

406406
[case testCustomSysVersionInfo]
407-
# flags: --python-version 3.2
407+
# flags: --python-version 3.5
408408
import sys
409-
if sys.version_info == (3, 2):
409+
if sys.version_info == (3, 5):
410410
x = "foo"
411411
else:
412412
x = 3
@@ -415,9 +415,9 @@ reveal_type(x) # E: Revealed type is 'builtins.str'
415415
[out]
416416

417417
[case testCustomSysVersionInfo2]
418-
# flags: --python-version 3.1
418+
# flags: --python-version 3.5
419419
import sys
420-
if sys.version_info == (3, 2):
420+
if sys.version_info == (3, 6):
421421
x = "foo"
422422
else:
423423
x = 3

test-data/unit/cmdline.test

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,64 @@ whatever
514514
main.py:1: error: Cannot find module named 'a'
515515
main.py:1: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
516516
main.py:1: error: Cannot find module named 'a.b'
517+
518+
[case testPythonVersionTooOld10]
519+
# cmd: mypy -c pass
520+
[file mypy.ini]
521+
[[mypy]
522+
python_version = 1.0
523+
[out]
524+
mypy.ini: [mypy]: python_version: Python major version '1' out of range (must be 2 or 3)
525+
526+
[case testPythonVersionTooOld26]
527+
# cmd: mypy -c pass
528+
[file mypy.ini]
529+
[[mypy]
530+
python_version = 2.6
531+
[out]
532+
mypy.ini: [mypy]: python_version: Python 2.6 is not supported (must be 2.7)
533+
534+
[case testPythonVersionTooOld32]
535+
# cmd: mypy -c pass
536+
[file mypy.ini]
537+
[[mypy]
538+
python_version = 3.2
539+
[out]
540+
mypy.ini: [mypy]: python_version: Python 3.2 is not supported (must be 3.3 or higher)
541+
542+
[case testPythonVersionTooNew28]
543+
# cmd: mypy -c pass
544+
[file mypy.ini]
545+
[[mypy]
546+
python_version = 2.8
547+
[out]
548+
mypy.ini: [mypy]: python_version: Python 2.8 is not supported (must be 2.7)
549+
550+
[case testPythonVersionTooNew40]
551+
# cmd: mypy -c pass
552+
[file mypy.ini]
553+
[[mypy]
554+
python_version = 4.0
555+
[out]
556+
mypy.ini: [mypy]: python_version: Python major version '4' out of range (must be 2 or 3)
557+
558+
[case testPythonVersionAccepted27]
559+
# cmd: mypy -c pass
560+
[file mypy.ini]
561+
[[mypy]
562+
python_version = 2.7
563+
[out]
564+
565+
[case testPythonVersionAccepted33]
566+
# cmd: mypy -c pass
567+
[file mypy.ini]
568+
[[mypy]
569+
python_version = 3.3
570+
[out]
571+
572+
[case testPythonVersionAccepted36]
573+
# cmd: mypy -c pass
574+
[file mypy.ini]
575+
[[mypy]
576+
python_version = 3.6
577+
[out]

0 commit comments

Comments
 (0)