Skip to content

Commit 72ea158

Browse files
dependabot[bot]moz-wptsync-bot
authored andcommitted
Bug 1758345 [wpt PR 33082] - Bump mypy from 0.812 to 0.931 in /tools, a=testonly
Automatic update from web-platform-tests Bump mypy from 0.812 to 0.931 in /tools (#33082) Changes to adapt: * Deals with changes in how mypy handles metaclasses * Prefers sys.platform == "win32" due to python/mypy#8166 and mypy not having WindowsError defined by default any more * Installs various typestubs * Rewrites tox.ini to avoid duplicating everything, and allowing new versions of Python to be easily tested (as tox -e py310-mypy will now work without further changes). * Make mypy warn when it thinks code is unreachable. Co-authored-by: Sam Sneddon <[email protected]> Co-authored-by: Philip Jägenstedt <[email protected]> -- wpt-commits: 51ad530bef4a8396c5706f508e46f256c21b2a57 wpt-pr: 33082
1 parent 2a7ef06 commit 72ea158

File tree

11 files changed

+71
-85
lines changed

11 files changed

+71
-85
lines changed

testing/web-platform/tests/tools/gitignore/gitignore.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
def fnmatch_translate(pat):
2828
# type: (bytes) -> Tuple[bool, Pattern[bytes]]
2929
parts = []
30-
seq = None
30+
seq = None # type: Optional[int]
3131
i = 0
3232
any_char = b"[^/]"
3333
if pat[0:1] == b"/":
@@ -60,10 +60,10 @@ def fnmatch_translate(pat):
6060
# TODO: this doesn't really handle invalid sequences in the right way
6161
if c == b"]":
6262
seq = None
63-
if parts[-1:] == b"[":
63+
if parts[-1] == b"[":
6464
parts = parts[:-1]
65-
elif parts[-1:] == b"^" and parts[-2:-1] == b"[":
66-
parts = parts[:-2]
65+
elif parts[-1] == b"^" and parts[-2] == b"[":
66+
raise ValueError
6767
else:
6868
parts.append(c)
6969
elif c == b"-":

testing/web-platform/tests/tools/gitignore/tests/test_gitignore.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
(b"a?c", True, [b"abc"]),
2727
(b"a[^b]c", True, [b"acc"]),
2828
(b"a[b-c]c", True, [b"abc", b"acc"]),
29+
(b"a[]c", True, [b"ac"]),
2930
] # type: Sequence[Tuple[bytes, bool, Iterable[bytes]]]
3031

3132
mismatch_data = [

testing/web-platform/tests/tools/manifest/item.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@
88
MYPY = False
99
if MYPY:
1010
# MYPY is set to True when run under Mypy.
11-
from typing import Optional
12-
from typing import Text
13-
from typing import Dict
14-
from typing import Tuple
15-
from typing import List
16-
from typing import Union
17-
from typing import Type
18-
from typing import Any
19-
from typing import Sequence
20-
from typing import Hashable
11+
from typing import Any, Dict, Hashable, List, Optional, Sequence, Text, Tuple, Type, Union, cast
2112
from .manifest import Manifest
2213
Fuzzy = Dict[Optional[Tuple[Text, Text, Text]], List[int]]
2314
PageRanges = Dict[Text, List[int]]
@@ -31,14 +22,23 @@ class ManifestItemMeta(ABCMeta):
3122
attribute, and otherwise behaves like an ABCMeta."""
3223

3324
def __new__(cls, name, bases, attrs):
34-
# type: (Type[ManifestItemMeta], str, Tuple[ManifestItemMeta, ...], Dict[str, Any]) -> ManifestItemMeta
35-
rv = super(ManifestItemMeta, cls).__new__(cls, name, bases, attrs)
36-
if not isabstract(rv):
37-
assert issubclass(rv, ManifestItem)
38-
assert isinstance(rv.item_type, str)
39-
item_types[rv.item_type] = rv
40-
41-
return rv # type: ignore
25+
# type: (Type[ManifestItemMeta], str, Tuple[type], Dict[str, Any]) -> ManifestItemMeta
26+
inst = super(ManifestItemMeta, cls).__new__(cls, name, bases, attrs)
27+
if isabstract(inst):
28+
return inst
29+
30+
assert issubclass(inst, ManifestItem)
31+
if MYPY:
32+
inst_ = cast(Type[ManifestItem], inst)
33+
item_type = cast(str, inst_.item_type)
34+
else:
35+
inst_ = inst
36+
assert isinstance(inst_.item_type, str)
37+
item_type = inst_.item_type
38+
39+
item_types[item_type] = inst_
40+
41+
return inst_
4242

4343

4444
class ManifestItem(metaclass=ManifestItemMeta):

testing/web-platform/tests/tools/manifest/sourcefile.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ def _parse_html(f):
174174
doc = html5lib.parse(f, treebuilder="etree", useChardet=False)
175175
if MYPY:
176176
return cast(ElementTree.Element, doc)
177-
return doc
177+
else:
178+
# (needs to be in else for mypy to believe this is reachable)
179+
return doc
178180

179181
def _parse_xml(f):
180182
# type: (BinaryIO) -> ElementTree.Element

testing/web-platform/tests/tools/manifest/typedata.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def __delitem__(self, key):
146146
def __iter__(self):
147147
# type: () -> Iterator[Tuple[Text, ...]]
148148
"""Iterator over keys in the TypeData in codepoint order"""
149-
data_node = self._data # type: Optional[Dict[Text, Any]]
150-
json_node = self._json_data # type: Optional[Dict[Text, Any]]
149+
data_node = self._data # type: Optional[Union[Dict[Text, Any], Set[item.ManifestItem]]]
150+
json_node = self._json_data # type: Optional[Union[Dict[Text, Any], List[Any]]]
151151
path = tuple() # type: Tuple[Text, ...]
152152
stack = [(data_node, json_node, path)]
153153
while stack:
@@ -174,21 +174,21 @@ def __len__(self):
174174
# type: () -> int
175175
count = 0
176176

177-
stack = [self._data]
177+
stack = [self._data] # type: List[Union[Dict[Text, Any], Set[item.ManifestItem]]]
178178
while stack:
179179
v = stack.pop()
180180
if isinstance(v, set):
181181
count += 1
182182
else:
183183
stack.extend(v.values())
184184

185-
stack = [self._json_data]
186-
while stack:
187-
v = stack.pop()
188-
if isinstance(v, list):
185+
json_stack = [self._json_data] # type: List[Union[Dict[Text, Any], List[Any]]]
186+
while json_stack:
187+
json_v = json_stack.pop()
188+
if isinstance(json_v, list):
189189
count += 1
190190
else:
191-
stack.extend(v.values())
191+
json_stack.extend(json_v.values())
192192

193193
return count
194194

testing/web-platform/tests/tools/manifest/utils.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
2-
import platform
32
import subprocess
3+
import sys
44

55
MYPY = False
66
if MYPY:
@@ -31,7 +31,7 @@ def rel_path_to_url(rel_path, url_base="/"):
3131

3232
def from_os_path(path):
3333
# type: (Text) -> Text
34-
assert os.path.sep == u"/" or platform.system() == "Windows"
34+
assert os.path.sep == u"/" or sys.platform == "win32"
3535
if u"/" == os.path.sep:
3636
rv = path
3737
else:
@@ -43,7 +43,7 @@ def from_os_path(path):
4343

4444
def to_os_path(path):
4545
# type: (Text) -> Text
46-
assert os.path.sep == u"/" or platform.system() == "Windows"
46+
assert os.path.sep == u"/" or sys.platform == "win32"
4747
if u"\\" in path:
4848
raise ValueError("normalised path contains \\")
4949
if u"/" == os.path.sep:
@@ -59,7 +59,7 @@ def gitfunc(cmd, *args):
5959
try:
6060
return subprocess.check_output(full_cmd, cwd=path, stderr=subprocess.STDOUT).decode('utf8')
6161
except Exception as e:
62-
if platform.uname()[0] == "Windows" and isinstance(e, WindowsError):
62+
if sys.platform == "win32" and isinstance(e, WindowsError):
6363
full_cmd[0] = u"git.bat"
6464
return subprocess.check_output(full_cmd, cwd=path, stderr=subprocess.STDOUT).decode('utf8')
6565
else:

testing/web-platform/tests/tools/manifest/vcs.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ def __init__(self, tests_root, url_base, cache_path, manifest_path=None, rebuild
102102
extras=[b".git/"],
103103
cache=self.ignore_cache)
104104
git = GitHasher(tests_root)
105-
if git is not None:
106-
self.hash_cache = git.hash_cache()
107-
else:
108-
self.hash_cache = {}
105+
self.hash_cache = git.hash_cache()
109106

110107
def __iter__(self):
111108
# type: () -> Iterator[Tuple[Text, Optional[Text], bool]]

testing/web-platform/tests/tools/mypy.ini

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ warn_redundant_casts = True
2121
warn_return_any = True
2222
warn_unused_configs = True
2323
warn_unused_ignores = True
24+
warn_unreachable = True
25+
26+
show_error_codes = True
2427

2528
# Ignore missing or untyped libraries.
2629

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
mypy==0.812
1+
mypy==0.931
22
mypy-extensions==0.4.3
3+
toml==0.10.2
34
typed-ast==1.4.3
5+
types-atomicwrites==1.4.1
6+
types-python-dateutil==2.8.9
7+
types-PyYAML==6.0.0
8+
types-requests==2.25.11
9+
types-setuptools==57.4.2
10+
types-six==1.16.2
11+
types-ujson==4.2.0
12+
typing-extensions==3.10.0.2
+13-41
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
11
[tox]
2-
envlist = py36,py37,py38,py39,{py36,py37,py38,py39}-flake8,{py36,py37,py38,py39}-mypy
2+
envlist = py36,py37,py38,py39,{py36,py37,py38,py39}-{flake8,mypy}
33
skipsdist=True
4-
skip_missing_interpreters = False
4+
skip_missing_interpreters=False
55

66
[testenv]
77
deps =
8-
-r{toxinidir}/requirements_pytest.txt
9-
-r{toxinidir}/requirements_tests.txt
8+
!flake8-!mypy: -r{toxinidir}/requirements_pytest.txt
9+
!flake8-!mypy: -r{toxinidir}/requirements_tests.txt
10+
flake8: -r{toxinidir}/requirements_flake8.txt
11+
mypy: -r{toxinidir}/requirements_mypy.txt
1012

11-
commands = pytest --cov=tools --cov-report=term {posargs}
13+
changedir =
14+
mypy: {toxinidir}/..
15+
16+
commands =
17+
!flake8-!mypy: pytest --cov=tools --cov-report=term {posargs}
18+
flake8: flake8 --append-config={toxinidir}/flake8.ini {posargs}
19+
mypy: mypy --config-file={toxinidir}/mypy.ini tools/
1220

1321
passenv =
1422
HYPOTHESIS_PROFILE
1523
PY_COLORS
1624
TASKCLUSTER_ROOT_URL
17-
18-
[testenv:py36-flake8]
19-
deps = -rrequirements_flake8.txt
20-
commands = flake8 --append-config={toxinidir}/flake8.ini {posargs}
21-
22-
[testenv:py37-flake8]
23-
deps = -rrequirements_flake8.txt
24-
commands = flake8 --append-config={toxinidir}/flake8.ini {posargs}
25-
26-
[testenv:py38-flake8]
27-
deps = -rrequirements_flake8.txt
28-
commands = flake8 --append-config={toxinidir}/flake8.ini {posargs}
29-
30-
[testenv:py39-flake8]
31-
deps = -rrequirements_flake8.txt
32-
commands = flake8 --append-config={toxinidir}/flake8.ini {posargs}
33-
34-
[testenv:py36-mypy]
35-
deps = -rrequirements_mypy.txt
36-
changedir = {toxinidir}/..
37-
commands = mypy --config-file={toxinidir}/mypy.ini tools/
38-
39-
[testenv:py37-mypy]
40-
deps = -rrequirements_mypy.txt
41-
changedir = {toxinidir}/..
42-
commands = mypy --config-file={toxinidir}/mypy.ini tools/
43-
44-
[testenv:py38-mypy]
45-
deps = -rrequirements_mypy.txt
46-
changedir = {toxinidir}/..
47-
commands = mypy --config-file={toxinidir}/mypy.ini tools/
48-
49-
[testenv:py39-mypy]
50-
deps = -rrequirements_mypy.txt
51-
changedir = {toxinidir}/..
52-
commands = mypy --config-file={toxinidir}/mypy.ini tools/

testing/web-platform/tests/tools/webdriver/webdriver/bidi/transport.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
def get_running_loop() -> asyncio.AbstractEventLoop:
1313
if sys.version_info >= (3, 7):
1414
return asyncio.get_running_loop()
15-
# Unlike the above, this will actually create an event loop
16-
# if there isn't one; hopefully running tests in Python >= 3.7
17-
# will allow us to catch any behaviour difference
18-
return asyncio.get_event_loop()
15+
else:
16+
# Unlike the above, this will actually create an event loop
17+
# if there isn't one; hopefully running tests in Python >= 3.7
18+
# will allow us to catch any behaviour difference
19+
# (Needs to be in else for mypy to believe this is reachable)
20+
return asyncio.get_event_loop()
1921

2022

2123
class Transport:

0 commit comments

Comments
 (0)