11from __future__ import annotations
22
33import uuid
4- from collections .abc import Iterable
4+ from collections .abc import Callable , Iterable
55from dataclasses import dataclass
6- from typing import (
7- TYPE_CHECKING ,
8- Any ,
9- Callable ,
10- Optional ,
11- Union ,
12- cast ,
13- )
6+ from typing import TYPE_CHECKING , Any , TypeAlias , cast
147from urllib .parse import urlencode , urljoin , urlsplit , urlunsplit
158
169from parsel import Selector , SelectorList
1710from w3lib .html import strip_html5_whitespace
1811
1912if TYPE_CHECKING :
13+ import requests
14+ import scrapy
15+ import web_poet
2016 from lxml .html import FormElement , HtmlElement
2117
2218
@@ -29,9 +25,9 @@ class FileField:
2925 content_type : str = "application/octet-stream"
3026
3127
32- FormdataVType = Union [ str , FileField , Iterable [str ] ]
33- FormdataKVType = tuple [str , FormdataVType ]
34- FormdataType = Optional [ Union [ dict [str , FormdataVType ], Iterable [FormdataKVType ]]]
28+ FormdataVType : TypeAlias = str | FileField | Iterable [str ]
29+ FormdataKVType : TypeAlias = tuple [str , FormdataVType ]
30+ FormdataType : TypeAlias = dict [str , FormdataVType ] | Iterable [FormdataKVType ] | None
3531
3632
3733def _parsel_to_lxml (
@@ -211,14 +207,14 @@ class Request:
211207 headers : list [tuple [str , str ]]
212208 body : bytes
213209
214- def to_poet (self , ** kwargs : Any ):
210+ def to_poet (self , ** kwargs : Any ) -> web_poet . HttpRequest :
215211 """Convert the request to :class:`web_poet.HttpRequest
216212 <web_poet.page_inputs.http.HttpRequest>`.
217213
218214 All *kwargs* are passed to :class:`web_poet.HttpRequest
219215 <web_poet.page_inputs.http.HttpRequest>` as is.
220216 """
221- import web_poet
217+ import web_poet # noqa: PLC0415
222218
223219 return web_poet .HttpRequest (
224220 url = self .url ,
@@ -228,12 +224,12 @@ def to_poet(self, **kwargs: Any):
228224 ** kwargs ,
229225 )
230226
231- def to_requests (self , ** kwargs : Any ):
227+ def to_requests (self , ** kwargs : Any ) -> requests . PreparedRequest :
232228 """Convert the request to :class:`requests.PreparedRequest`.
233229
234230 All *kwargs* are passed to :class:`requests.Request` as is.
235231 """
236- import requests
232+ import requests # noqa: PLC0415
237233
238234 request = requests .Request (
239235 self .method ,
@@ -244,12 +240,12 @@ def to_requests(self, **kwargs: Any):
244240 )
245241 return request .prepare ()
246242
247- def to_scrapy (self , callback : Callable , ** kwargs : Any ):
243+ def to_scrapy (self , callback : Callable , ** kwargs : Any ) -> scrapy . Request :
248244 """Convert the request to :class:`scrapy.Request`.
249245
250246 All *kwargs* are passed to :class:`scrapy.Request` as is.
251247 """
252- import scrapy
248+ import scrapy # noqa: PLC0415
253249
254250 return scrapy .Request (
255251 self .url ,
0 commit comments