Skip to content

Commit f576e84

Browse files
ilevkivskyigvanrossum
authored andcommitted
Do not import List, Dict, and Set by default (#2863)
Fix #999 This is a straightforward fix: List, Set, and Dict are treated as aliases to their builtins counterparts, therefore they are ignored by remove_imported_names_from_symtable, I fix this by also removing the alias names. The fix also revealed few places in mypy where imports were missing, and one bug in fastparse.py. Those have been fixed. The fix also includes a typeshed sync at python/typeshed#933.
1 parent 70b278c commit f576e84

File tree

9 files changed

+10
-8
lines changed

9 files changed

+10
-8
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
398398
if arg.annotation is not None:
399399
arg_type = TypeConverter(self.errors, line=line).visit(arg.annotation)
400400
elif arg.type_comment is not None:
401-
arg_type = parse_type_comment(arg.type_comment, arg.lineno, arg.col_offset)
401+
arg_type = parse_type_comment(arg.type_comment, arg.lineno, self.errors)
402402
return Argument(Var(arg.arg), arg_type, self.visit(default), kind)
403403

404404
new_args = []

mypy/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pprint
33
import sys
44

5-
from typing import Any, Mapping, Optional, Tuple, List, Pattern
5+
from typing import Any, Mapping, Optional, Tuple, List, Pattern, Dict
66

77
from mypy import defaults
88

mypy/semanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ def visit_file(self, file_node: MypyFile, fnam: str, options: Options) -> None:
253253

254254
if self.cur_mod_id == 'builtins':
255255
remove_imported_names_from_symtable(self.globals, 'builtins')
256+
for alias_name in ['List', 'Dict', 'Set']:
257+
self.globals.pop(alias_name, None)
256258

257259
if '__all__' in self.globals:
258260
for name, g in self.globals.items():

mypy/test/testgraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test cases for graph processing code in build.py."""
22

3-
from typing import AbstractSet, Dict, Set
3+
from typing import AbstractSet, Dict, Set, List
44

55
from mypy.myunit import Suite, assert_equal
66
from mypy.build import BuildManager, State, BuildSourceSet

mypy/typevars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, List
22

33
from mypy.nodes import TypeInfo
44

test-data/samples/crawl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
import time
1818
import urllib.parse
19-
from typing import Any, Generator, IO, Optional, Sequence, Set, Tuple
19+
from typing import Any, Generator, IO, Optional, Sequence, Set, Tuple, List, Dict
2020

2121

2222
ARGS = argparse.ArgumentParser(description="Web crawler")

test-data/samples/crawl2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import sys
1818
import time
1919
import urllib.parse
20-
from typing import Any, Awaitable, IO, Optional, Sequence, Set, Tuple
20+
from typing import Any, Awaitable, IO, Optional, Sequence, Set, Tuple, List, Dict
2121

2222

2323
ARGS = argparse.ArgumentParser(description="Web crawler")

test-data/unit/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ print(b.x + 1)
11471147
4
11481148

11491149
[case testInferenceWithLambda]
1150-
from typing import TypeVar, Iterable, Iterator
1150+
from typing import TypeVar, Iterable, Iterator, List
11511151
import itertools
11521152

11531153
_T = TypeVar('_T')

0 commit comments

Comments
 (0)