Skip to content

Don't use Python 3 stdlib stubs in Python 2 mode #10422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def get_toplevel_possibilities(self, lib_path: Tuple[str, ...], id: str) -> List
name = os.path.splitext(name)[0]
components.setdefault(name, []).append(dir)

if self.python2:
components = {id: filter_redundant_py2_dirs(dirs)
for id, dirs in components.items()}

self.initial_components[lib_path] = components
return components.get(id, [])

Expand Down Expand Up @@ -791,3 +795,19 @@ def typeshed_py_version(options: Options) -> Tuple[int, int]:
return max(options.python_version, (3, 6))
else:
return options.python_version


def filter_redundant_py2_dirs(dirs: List[str]) -> List[str]:
"""If dirs has <dir>/@python2 followed by <dir>, filter out the latter."""
if len(dirs) <= 1 or not any(d.endswith(PYTHON2_STUB_DIR) for d in dirs):
# Fast path -- nothing to do
return dirs
seen = []
result = []
for d in dirs:
if d.endswith(PYTHON2_STUB_DIR):
seen.append(os.path.dirname(d))
result.append(d)
elif d not in seen:
result.append(d)
return result
5 changes: 5 additions & 0 deletions test-data/unit/python2eval.test
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,8 @@ def foo() -> None:
reveal_type(x)
[out]
_testDefaultDictInference.py:5: note: Revealed type is "collections.defaultdict[builtins.str, builtins.list[builtins.int]]"

[case testIgnorePython3StdlibStubs_python2]
from collections import abc
[out]
_testIgnorePython3StdlibStubs_python2.py:1: error: Module "collections" has no attribute "abc"