Skip to content

Commit b50a4ef

Browse files
authored
Simplify stubinfo without Python 2 (#13265)
1 parent e311f82 commit b50a4ef

File tree

4 files changed

+62
-84
lines changed

4 files changed

+62
-84
lines changed

mypy/build.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,12 +2620,8 @@ def find_module_and_diagnose(
26202620
# otherwise updating mypy can silently result in new false
26212621
# negatives. (Unless there are stubs but they are incomplete.)
26222622
global_ignore_missing_imports = manager.options.ignore_missing_imports
2623-
py_ver = options.python_version[0]
26242623
if (
2625-
(
2626-
is_legacy_bundled_package(top_level, py_ver)
2627-
or is_legacy_bundled_package(second_level, py_ver)
2628-
)
2624+
(is_legacy_bundled_package(top_level) or is_legacy_bundled_package(second_level))
26292625
and global_ignore_missing_imports
26302626
and not options.ignore_missing_imports_per_module
26312627
and result is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
@@ -2743,10 +2739,10 @@ def module_not_found(
27432739
top_level = second_level
27442740
for note in notes:
27452741
if "{stub_dist}" in note:
2746-
note = note.format(stub_dist=legacy_bundled_packages[top_level].name)
2742+
note = note.format(stub_dist=legacy_bundled_packages[top_level])
27472743
errors.report(line, 0, note, severity="note", only_once=True, code=codes.IMPORT)
27482744
if reason is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED:
2749-
manager.missing_stub_packages.add(legacy_bundled_packages[top_level].name)
2745+
manager.missing_stub_packages.add(legacy_bundled_packages[top_level])
27502746
errors.set_import_context(save_import_context)
27512747

27522748

mypy/modulefinder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ def _find_module_non_stub_helper(
343343
# If this is not a directory then we can't traverse further into it
344344
if not self.fscache.isdir(dir_path):
345345
break
346-
if is_legacy_bundled_package(components[0], self.python_major_ver):
346+
if is_legacy_bundled_package(components[0]):
347347
if len(components) == 1 or (
348348
self.find_module(components[0])
349349
is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
350350
):
351351
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
352-
if is_legacy_bundled_package(".".join(components[:2]), self.python_major_ver):
352+
if is_legacy_bundled_package(".".join(components[:2])):
353353
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
354354
if plausible_match:
355355
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS

mypy/stubinfo.py

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
from typing import Optional
2-
3-
4-
class StubInfo:
5-
def __init__(self, name: str, py_version: Optional[int] = None) -> None:
6-
self.name = name
7-
# If None, compatible with py2+py3, if 2/3, only compatible with py2/py3
8-
self.py_version = py_version
9-
10-
11-
def is_legacy_bundled_package(prefix: str, py_version: int) -> bool:
12-
if prefix not in legacy_bundled_packages:
13-
return False
14-
package_ver = legacy_bundled_packages[prefix].py_version
15-
return package_ver is None or package_ver == py_version
1+
def is_legacy_bundled_package(prefix: str) -> bool:
2+
return prefix in legacy_bundled_packages
163

174

185
# Stubs for these third-party packages used to be shipped with mypy.
@@ -21,56 +8,56 @@ def is_legacy_bundled_package(prefix: str, py_version: int) -> bool:
218
#
229
# Package name can have one or two components ('a' or 'a.b').
2310
legacy_bundled_packages = {
24-
"aiofiles": StubInfo("types-aiofiles", py_version=3),
25-
"atomicwrites": StubInfo("types-atomicwrites"),
26-
"attr": StubInfo("types-attrs"),
27-
"backports": StubInfo("types-backports"),
28-
"backports_abc": StubInfo("types-backports_abc"),
29-
"bleach": StubInfo("types-bleach"),
30-
"boto": StubInfo("types-boto"),
31-
"cachetools": StubInfo("types-cachetools"),
32-
"chardet": StubInfo("types-chardet"),
33-
"click_spinner": StubInfo("types-click-spinner"),
34-
"contextvars": StubInfo("types-contextvars", py_version=3),
35-
"croniter": StubInfo("types-croniter"),
36-
"dataclasses": StubInfo("types-dataclasses", py_version=3),
37-
"dateparser": StubInfo("types-dateparser"),
38-
"datetimerange": StubInfo("types-DateTimeRange"),
39-
"dateutil": StubInfo("types-python-dateutil"),
40-
"decorator": StubInfo("types-decorator"),
41-
"deprecated": StubInfo("types-Deprecated"),
42-
"docutils": StubInfo("types-docutils", py_version=3),
43-
"emoji": StubInfo("types-emoji"),
44-
"first": StubInfo("types-first"),
45-
"geoip2": StubInfo("types-geoip2"),
46-
"gflags": StubInfo("types-python-gflags"),
47-
"google.protobuf": StubInfo("types-protobuf"),
48-
"markdown": StubInfo("types-Markdown"),
49-
"maxminddb": StubInfo("types-maxminddb"),
50-
"mock": StubInfo("types-mock"),
51-
"OpenSSL": StubInfo("types-pyOpenSSL"),
52-
"paramiko": StubInfo("types-paramiko"),
53-
"pkg_resources": StubInfo("types-setuptools", py_version=3),
54-
"polib": StubInfo("types-polib"),
55-
"pycurl": StubInfo("types-pycurl"),
56-
"pymysql": StubInfo("types-PyMySQL"),
57-
"pyrfc3339": StubInfo("types-pyRFC3339", py_version=3),
58-
"python2": StubInfo("types-six"),
59-
"pytz": StubInfo("types-pytz"),
60-
"pyVmomi": StubInfo("types-pyvmomi"),
61-
"redis": StubInfo("types-redis"),
62-
"requests": StubInfo("types-requests"),
63-
"retry": StubInfo("types-retry"),
64-
"simplejson": StubInfo("types-simplejson"),
65-
"singledispatch": StubInfo("types-singledispatch"),
66-
"six": StubInfo("types-six"),
67-
"slugify": StubInfo("types-python-slugify"),
68-
"tabulate": StubInfo("types-tabulate"),
69-
"termcolor": StubInfo("types-termcolor"),
70-
"toml": StubInfo("types-toml"),
71-
"typed_ast": StubInfo("types-typed-ast", py_version=3),
72-
"tzlocal": StubInfo("types-tzlocal"),
73-
"ujson": StubInfo("types-ujson"),
74-
"waitress": StubInfo("types-waitress", py_version=3),
75-
"yaml": StubInfo("types-PyYAML"),
11+
"aiofiles": "types-aiofiles",
12+
"atomicwrites": "types-atomicwrites",
13+
"attr": "types-attrs",
14+
"backports": "types-backports",
15+
"backports_abc": "types-backports_abc",
16+
"bleach": "types-bleach",
17+
"boto": "types-boto",
18+
"cachetools": "types-cachetools",
19+
"chardet": "types-chardet",
20+
"click_spinner": "types-click-spinner",
21+
"contextvars": "types-contextvars",
22+
"croniter": "types-croniter",
23+
"dataclasses": "types-dataclasses",
24+
"dateparser": "types-dateparser",
25+
"datetimerange": "types-DateTimeRange",
26+
"dateutil": "types-python-dateutil",
27+
"decorator": "types-decorator",
28+
"deprecated": "types-Deprecated",
29+
"docutils": "types-docutils",
30+
"emoji": "types-emoji",
31+
"first": "types-first",
32+
"geoip2": "types-geoip2",
33+
"gflags": "types-python-gflags",
34+
"google.protobuf": "types-protobuf",
35+
"markdown": "types-Markdown",
36+
"maxminddb": "types-maxminddb",
37+
"mock": "types-mock",
38+
"OpenSSL": "types-pyOpenSSL",
39+
"paramiko": "types-paramiko",
40+
"pkg_resources": "types-setuptools",
41+
"polib": "types-polib",
42+
"pycurl": "types-pycurl",
43+
"pymysql": "types-PyMySQL",
44+
"pyrfc3339": "types-pyRFC3339",
45+
"python2": "types-six",
46+
"pytz": "types-pytz",
47+
"pyVmomi": "types-pyvmomi",
48+
"redis": "types-redis",
49+
"requests": "types-requests",
50+
"retry": "types-retry",
51+
"simplejson": "types-simplejson",
52+
"singledispatch": "types-singledispatch",
53+
"six": "types-six",
54+
"slugify": "types-python-slugify",
55+
"tabulate": "types-tabulate",
56+
"termcolor": "types-termcolor",
57+
"toml": "types-toml",
58+
"typed_ast": "types-typed-ast",
59+
"tzlocal": "types-tzlocal",
60+
"ujson": "types-ujson",
61+
"waitress": "types-waitress",
62+
"yaml": "types-PyYAML",
7663
}

mypy/test/teststubinfo.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55

66
class TestStubInfo(unittest.TestCase):
77
def test_is_legacy_bundled_packages(self) -> None:
8-
assert not is_legacy_bundled_package("foobar_asdf", 2)
9-
assert not is_legacy_bundled_package("foobar_asdf", 3)
10-
11-
assert is_legacy_bundled_package("pycurl", 2)
12-
assert is_legacy_bundled_package("pycurl", 3)
13-
14-
assert not is_legacy_bundled_package("dataclasses", 2)
15-
assert is_legacy_bundled_package("dataclasses", 3)
8+
assert not is_legacy_bundled_package("foobar_asdf")
9+
assert is_legacy_bundled_package("pycurl")
10+
assert is_legacy_bundled_package("dataclasses")

0 commit comments

Comments
 (0)