Skip to content

Commit ac018c1

Browse files
authored
Require newer aiohttp for blackd (#4451)
1 parent 058da5f commit ac018c1

File tree

4 files changed

+6
-75
lines changed

4 files changed

+6
-75
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<!-- Changes to how Black is packaged, such as dependency requirements -->
3535

3636
- Upgrade version of mypyc used to 1.11.2 (#4450)
37+
- `blackd` now requires a newer version of aiohttp. (#4451)
3738

3839
### Parser
3940

pyproject.toml

+2-21
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ dynamic = ["readme", "version"]
7878
[project.optional-dependencies]
7979
colorama = ["colorama>=0.4.3"]
8080
uvloop = ["uvloop>=0.15.2"]
81-
d = [
82-
"aiohttp>=3.7.4; sys_platform != 'win32' or implementation_name != 'pypy'",
83-
"aiohttp>=3.7.4, !=3.9.0; sys_platform == 'win32' and implementation_name == 'pypy'",
84-
]
81+
d = ["aiohttp>=3.10"]
8582
jupyter = [
8683
"ipython>=7.8.0",
8784
"tokenize-rt>=3.2.0",
@@ -219,23 +216,7 @@ markers = [
219216
"incompatible_with_mypyc: run when testing mypyc compiled black"
220217
]
221218
xfail_strict = true
222-
filterwarnings = [
223-
"error",
224-
# this is mitigated by a try/catch in https://github.com/psf/black/pull/2974/
225-
# this ignore can be removed when support for aiohttp 3.7 is dropped.
226-
'''ignore:Decorator `@unittest_run_loop` is no longer needed in aiohttp 3\.8\+:DeprecationWarning''',
227-
# this is mitigated by a try/catch in https://github.com/psf/black/pull/3198/
228-
# this ignore can be removed when support for aiohttp 3.x is dropped.
229-
'''ignore:Middleware decorator is deprecated since 4\.0 and its behaviour is default, you can simply remove this decorator:DeprecationWarning''',
230-
# aiohttp is using deprecated cgi modules - Safe to remove when fixed:
231-
# https://github.com/aio-libs/aiohttp/issues/6905
232-
'''ignore:'cgi' is deprecated and slated for removal in Python 3.13:DeprecationWarning''',
233-
# Work around https://github.com/pytest-dev/pytest/issues/10977 for Python 3.12
234-
'''ignore:(Attribute s|Attribute n|ast.Str|ast.Bytes|ast.NameConstant|ast.Num) is deprecated and will be removed in Python 3.14:DeprecationWarning''',
235-
# Will be fixed with aiohttp 3.9.0
236-
# https://github.com/aio-libs/aiohttp/pull/7302
237-
"ignore:datetime.*utcfromtimestamp\\(\\) is deprecated and scheduled for removal:DeprecationWarning",
238-
]
219+
filterwarnings = ["error"]
239220
[tool.coverage.report]
240221
omit = [
241222
"src/blib2to3/*",

src/blackd/middlewares.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
1-
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypeVar
1+
from typing import Awaitable, Callable, Iterable
22

3+
from aiohttp.typedefs import Middleware
4+
from aiohttp.web_middlewares import middleware
35
from aiohttp.web_request import Request
46
from aiohttp.web_response import StreamResponse
57

68
Handler = Callable[[Request], Awaitable[StreamResponse]]
79

8-
if TYPE_CHECKING:
9-
from aiohttp.typedefs import Middleware
10-
11-
F = TypeVar("F", bound=Callable[..., Any])
12-
middleware: Callable[[F], F]
13-
else:
14-
try:
15-
# Available in aiohttp 3.9 and newer
16-
from aiohttp.typedefs import Middleware
17-
except ImportError:
18-
Middleware = Callable[[Request, Handler], Awaitable[StreamResponse]]
19-
20-
try:
21-
from aiohttp.web_middlewares import middleware
22-
except ImportError:
23-
# @middleware is deprecated and its behaviour is the default since aiohttp 4.0
24-
# so if it doesn't exist anymore, define a no-op for forward compatibility.
25-
middleware = lambda x: x # noqa: E731
26-
2710

2811
def cors(allow_headers: Iterable[str]) -> Middleware:
2912
@middleware

tests/test_blackd.py

-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
from typing import TYPE_CHECKING, Any, Callable, TypeVar
32
from unittest.mock import patch
43

54
import pytest
@@ -15,20 +14,6 @@
1514
except ImportError as e:
1615
raise RuntimeError("Please install Black with the 'd' extra") from e
1716

18-
if TYPE_CHECKING:
19-
F = TypeVar("F", bound=Callable[..., Any])
20-
21-
unittest_run_loop: Callable[[F], F] = lambda x: x
22-
else:
23-
try:
24-
from aiohttp.test_utils import unittest_run_loop
25-
except ImportError:
26-
# unittest_run_loop is unnecessary and a no-op since aiohttp 3.8, and
27-
# aiohttp 4 removed it. To maintain compatibility we can make our own
28-
# no-op decorator.
29-
def unittest_run_loop(func, *args, **kwargs):
30-
return func
31-
3217

3318
@pytest.mark.blackd
3419
class BlackDTestCase(AioHTTPTestCase):
@@ -42,20 +27,17 @@ def test_blackd_main(self) -> None:
4227
async def get_application(self) -> web.Application:
4328
return blackd.make_app()
4429

45-
@unittest_run_loop
4630
async def test_blackd_request_needs_formatting(self) -> None:
4731
response = await self.client.post("/", data=b"print('hello world')")
4832
self.assertEqual(response.status, 200)
4933
self.assertEqual(response.charset, "utf8")
5034
self.assertEqual(await response.read(), b'print("hello world")\n')
5135

52-
@unittest_run_loop
5336
async def test_blackd_request_no_change(self) -> None:
5437
response = await self.client.post("/", data=b'print("hello world")\n')
5538
self.assertEqual(response.status, 204)
5639
self.assertEqual(await response.read(), b"")
5740

58-
@unittest_run_loop
5941
async def test_blackd_request_syntax_error(self) -> None:
6042
response = await self.client.post("/", data=b"what even ( is")
6143
self.assertEqual(response.status, 400)
@@ -65,21 +47,18 @@ async def test_blackd_request_syntax_error(self) -> None:
6547
msg=f"Expected error to start with 'Cannot parse', got {repr(content)}",
6648
)
6749

68-
@unittest_run_loop
6950
async def test_blackd_unsupported_version(self) -> None:
7051
response = await self.client.post(
7152
"/", data=b"what", headers={blackd.PROTOCOL_VERSION_HEADER: "2"}
7253
)
7354
self.assertEqual(response.status, 501)
7455

75-
@unittest_run_loop
7656
async def test_blackd_supported_version(self) -> None:
7757
response = await self.client.post(
7858
"/", data=b"what", headers={blackd.PROTOCOL_VERSION_HEADER: "1"}
7959
)
8060
self.assertEqual(response.status, 200)
8161

82-
@unittest_run_loop
8362
async def test_blackd_invalid_python_variant(self) -> None:
8463
async def check(header_value: str, expected_status: int = 400) -> None:
8564
response = await self.client.post(
@@ -102,7 +81,6 @@ async def check(header_value: str, expected_status: int = 400) -> None:
10281
await check("pypy3.0")
10382
await check("jython3.4")
10483

105-
@unittest_run_loop
10684
async def test_blackd_pyi(self) -> None:
10785
source, expected = read_data("cases", "stub.py")
10886
response = await self.client.post(
@@ -111,7 +89,6 @@ async def test_blackd_pyi(self) -> None:
11189
self.assertEqual(response.status, 200)
11290
self.assertEqual(await response.text(), expected)
11391

114-
@unittest_run_loop
11592
async def test_blackd_diff(self) -> None:
11693
diff_header = re.compile(
11794
r"(In|Out)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d\+\d\d:\d\d"
@@ -129,7 +106,6 @@ async def test_blackd_diff(self) -> None:
129106
actual = diff_header.sub(DETERMINISTIC_HEADER, actual)
130107
self.assertEqual(actual, expected)
131108

132-
@unittest_run_loop
133109
async def test_blackd_python_variant(self) -> None:
134110
code = (
135111
"def f(\n"
@@ -161,14 +137,12 @@ async def check(header_value: str, expected_status: int) -> None:
161137
await check("py34,py36", 204)
162138
await check("34", 204)
163139

164-
@unittest_run_loop
165140
async def test_blackd_line_length(self) -> None:
166141
response = await self.client.post(
167142
"/", data=b'print("hello")\n', headers={blackd.LINE_LENGTH_HEADER: "7"}
168143
)
169144
self.assertEqual(response.status, 200)
170145

171-
@unittest_run_loop
172146
async def test_blackd_invalid_line_length(self) -> None:
173147
response = await self.client.post(
174148
"/",
@@ -177,7 +151,6 @@ async def test_blackd_invalid_line_length(self) -> None:
177151
)
178152
self.assertEqual(response.status, 400)
179153

180-
@unittest_run_loop
181154
async def test_blackd_skip_first_source_line(self) -> None:
182155
invalid_first_line = b"Header will be skipped\r\ni = [1,2,3]\nj = [1,2,3]\n"
183156
expected_result = b"Header will be skipped\r\ni = [1, 2, 3]\nj = [1, 2, 3]\n"
@@ -191,19 +164,16 @@ async def test_blackd_skip_first_source_line(self) -> None:
191164
self.assertEqual(response.status, 200)
192165
self.assertEqual(await response.read(), expected_result)
193166

194-
@unittest_run_loop
195167
async def test_blackd_preview(self) -> None:
196168
response = await self.client.post(
197169
"/", data=b'print("hello")\n', headers={blackd.PREVIEW: "true"}
198170
)
199171
self.assertEqual(response.status, 204)
200172

201-
@unittest_run_loop
202173
async def test_blackd_response_black_version_header(self) -> None:
203174
response = await self.client.post("/")
204175
self.assertIsNotNone(response.headers.get(blackd.BLACK_VERSION_HEADER))
205176

206-
@unittest_run_loop
207177
async def test_cors_preflight(self) -> None:
208178
response = await self.client.options(
209179
"/",
@@ -218,13 +188,11 @@ async def test_cors_preflight(self) -> None:
218188
self.assertIsNotNone(response.headers.get("Access-Control-Allow-Headers"))
219189
self.assertIsNotNone(response.headers.get("Access-Control-Allow-Methods"))
220190

221-
@unittest_run_loop
222191
async def test_cors_headers_present(self) -> None:
223192
response = await self.client.post("/", headers={"Origin": "*"})
224193
self.assertIsNotNone(response.headers.get("Access-Control-Allow-Origin"))
225194
self.assertIsNotNone(response.headers.get("Access-Control-Expose-Headers"))
226195

227-
@unittest_run_loop
228196
async def test_preserves_line_endings(self) -> None:
229197
for data in (b"c\r\nc\r\n", b"l\nl\n"):
230198
# test preserved newlines when reformatted
@@ -234,14 +202,12 @@ async def test_preserves_line_endings(self) -> None:
234202
response = await self.client.post("/", data=data)
235203
self.assertEqual(response.status, 204)
236204

237-
@unittest_run_loop
238205
async def test_normalizes_line_endings(self) -> None:
239206
for data, expected in ((b"c\r\nc\n", "c\r\nc\r\n"), (b"l\nl\r\n", "l\nl\n")):
240207
response = await self.client.post("/", data=data)
241208
self.assertEqual(await response.text(), expected)
242209
self.assertEqual(response.status, 200)
243210

244-
@unittest_run_loop
245211
async def test_single_character(self) -> None:
246212
response = await self.client.post("/", data="1")
247213
self.assertEqual(await response.text(), "1\n")

0 commit comments

Comments
 (0)