Skip to content

Commit d130d31

Browse files
authored
Merge pull request #10674 from uranusjr/mypy-fix-windows
2 parents e666842 + cf1dda1 commit d130d31

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

tests/lib/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ def __getattr__(self, attr: str) -> Any:
266266
if sys.platform == "win32":
267267

268268
@property
269-
def stdout(self):
269+
def stdout(self) -> str:
270270
return self._impl.stdout.replace("\r\n", "\n")
271271

272272
@property
273-
def stderr(self):
273+
def stderr(self) -> str:
274274
return self._impl.stderr.replace("\r\n", "\n")
275275

276-
def __str__(self):
276+
def __str__(self) -> str:
277277
return str(self._impl).replace("\r\n", "\n")
278278

279279
else:
@@ -618,7 +618,7 @@ def run(
618618
cwd = cwd or self.cwd
619619
if sys.platform == "win32":
620620
# Partial fix for ScriptTest.run using `shell=True` on Windows.
621-
args = [str(a).replace("^", "^^").replace("&", "^&") for a in args]
621+
args = tuple(str(a).replace("^", "^^").replace("&", "^&") for a in args)
622622

623623
if allow_error:
624624
kw["expect_error"] = True

tests/lib/compat.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# mypy: no-warn-unused-ignores
2+
13
import contextlib
2-
from typing import Iterator
4+
import signal
5+
from typing import Iterable, Iterator
36

47

58
@contextlib.contextmanager
@@ -18,3 +21,34 @@ def nullcontext() -> Iterator[None]:
1821
support.
1922
"""
2023
yield
24+
25+
26+
# Applies on Windows.
27+
if not hasattr(signal, "pthread_sigmask"):
28+
# We're not relying on this behavior anywhere currently, it's just best
29+
# practice.
30+
blocked_signals = nullcontext
31+
else:
32+
33+
@contextlib.contextmanager
34+
def blocked_signals() -> Iterator[None]:
35+
"""Block all signals for e.g. starting a worker thread."""
36+
# valid_signals() was added in Python 3.8 (and not using it results
37+
# in a warning on pthread_sigmask() call)
38+
mask: Iterable[int]
39+
try:
40+
mask = signal.valid_signals()
41+
except AttributeError:
42+
mask = set(range(1, signal.NSIG))
43+
44+
old_mask = signal.pthread_sigmask( # type: ignore[attr-defined]
45+
signal.SIG_SETMASK, # type: ignore[attr-defined]
46+
mask,
47+
)
48+
try:
49+
yield
50+
finally:
51+
signal.pthread_sigmask( # type: ignore[attr-defined]
52+
signal.SIG_SETMASK, # type: ignore[attr-defined]
53+
old_mask,
54+
)

tests/lib/server.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import signal
32
import ssl
43
import threading
54
from base64 import b64encode
@@ -11,10 +10,10 @@
1110
from werkzeug.serving import BaseWSGIServer, WSGIRequestHandler
1211
from werkzeug.serving import make_server as _make_server
1312

14-
from .compat import nullcontext
13+
from .compat import blocked_signals
1514

1615
if TYPE_CHECKING:
17-
from wsgi import StartResponse, WSGIApplication, WSGIEnvironment
16+
from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironment
1817

1918
Body = Iterable[bytes]
2019

@@ -23,31 +22,6 @@ class MockServer(BaseWSGIServer):
2322
mock: Mock = Mock()
2423

2524

26-
# Applies on Python 2 and Windows.
27-
if not hasattr(signal, "pthread_sigmask"):
28-
# We're not relying on this behavior anywhere currently, it's just best
29-
# practice.
30-
blocked_signals = nullcontext
31-
else:
32-
33-
@contextmanager
34-
def blocked_signals() -> Iterator[None]:
35-
"""Block all signals for e.g. starting a worker thread."""
36-
# valid_signals() was added in Python 3.8 (and not using it results
37-
# in a warning on pthread_sigmask() call)
38-
mask: Iterable[int]
39-
try:
40-
mask = signal.valid_signals()
41-
except AttributeError:
42-
mask = set(range(1, signal.NSIG))
43-
44-
old_mask = signal.pthread_sigmask(signal.SIG_SETMASK, mask)
45-
try:
46-
yield
47-
finally:
48-
signal.pthread_sigmask(signal.SIG_SETMASK, old_mask)
49-
50-
5125
class _RequestHandler(WSGIRequestHandler):
5226
def make_environ(self) -> Dict[str, Any]:
5327
environ = super().make_environ()

tests/unit/test_appdirs.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# mypy: no-warn-unused-ignores
2+
13
import os
24
import sys
35
from unittest import mock
@@ -14,7 +16,7 @@ def test_user_cache_dir_win(self, monkeypatch: pytest.MonkeyPatch) -> None:
1416
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Local")
1517

1618
monkeypatch.setattr(
17-
platformdirs.windows, # type: ignore[attr-defined]
19+
platformdirs.windows, # type: ignore
1820
"get_win_folder",
1921
_get_win_folder,
2022
raising=False,
@@ -65,7 +67,11 @@ def test_user_cache_dir_unicode(self, monkeypatch: pytest.MonkeyPatch) -> None:
6567
def my_get_win_folder(csidl_name: str) -> str:
6668
return "\u00DF\u00E4\u03B1\u20AC"
6769

68-
monkeypatch.setattr(platformdirs.windows, "get_win_folder", my_get_win_folder)
70+
monkeypatch.setattr(
71+
platformdirs.windows, # type: ignore
72+
"get_win_folder",
73+
my_get_win_folder,
74+
)
6975

7076
# Do not use the isinstance expression directly in the
7177
# assert statement, as the Unicode characters in the result
@@ -85,7 +91,7 @@ def test_site_config_dirs_win(self, monkeypatch: pytest.MonkeyPatch) -> None:
8591
_get_win_folder = mock.Mock(return_value="C:\\ProgramData")
8692

8793
monkeypatch.setattr(
88-
platformdirs.windows, # type: ignore[attr-defined]
94+
platformdirs.windows, # type: ignore
8995
"get_win_folder",
9096
_get_win_folder,
9197
raising=False,
@@ -145,7 +151,7 @@ def test_user_config_dir_win_no_roaming(
145151
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Local")
146152

147153
monkeypatch.setattr(
148-
platformdirs.windows, # type: ignore[attr-defined]
154+
platformdirs.windows, # type: ignore
149155
"get_win_folder",
150156
_get_win_folder,
151157
raising=False,
@@ -164,7 +170,7 @@ def test_user_config_dir_win_yes_roaming(
164170
_get_win_folder = mock.Mock(return_value="C:\\Users\\test\\AppData\\Roaming")
165171

166172
monkeypatch.setattr(
167-
platformdirs.windows, # type: ignore[attr-defined]
173+
platformdirs.windows, # type: ignore
168174
"get_win_folder",
169175
_get_win_folder,
170176
raising=False,

tests/unit/test_locations.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def patch(self) -> None:
5151
# now patch
5252
tempfile.gettempdir = lambda: self.tempdir
5353
getpass.getuser = lambda: self.username
54-
os.geteuid = lambda: self.st_uid
5554
os.fstat = lambda fd: self.get_mock_fstat(fd)
56-
5755
if sys.platform != "win32":
56+
os.geteuid = lambda: self.st_uid
5857
pwd.getpwuid = self.get_mock_getpwuid
5958

6059
def revert_patch(self) -> None:

0 commit comments

Comments
 (0)