Invalid usage of optional parameter / missing usage of Optional types #1747
-
A lot of parameters in httpx are provided a fallback-value of Example from https://github.com/encode/httpx/blob/master/httpx/_client.py async def request(
self,
method: str,
url: URLTypes,
*,
content: RequestContent = None,
data: RequestData = None,
files: RequestFiles = None,
json: typing.Any = None,
params: QueryParamTypes = None,
headers: HeaderTypes = None,
cookies: CookieTypes = None,
auth: typing.Union[AuthTypes, UnsetType] = UNSET,
allow_redirects: bool = True,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
) -> Response:
# ... with for example RequestContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] It should either be # parameter
content: Optional[RequestContent] = None
# or type
RequestContent = Union[str, bytes, Iterable[bytes], AsyncIterable[bytes], None] # including None The latter would align with what is done e.g. in https://github.com/encode/httpx/blob/master/httpx/_types.py with AuthTypes = Union[
Tuple[Union[str, bytes], Union[str, bytes]],
Callable[["Request"], "Request"],
"Auth",
None,
] From personal preference I'd be in favour of the former though, as it makes the type-signature more meaningful. For context, I currently have a wrapper function around |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
#1439 seems related to this but as random input i've also had a similar issue with having to silence type warnings when wrapping the AsyncClient |
Beta Was this translation helpful? Give feedback.
-
I mean it's a real mixed bag. Yeah we could but it'd add an awful lot of extra noise to the codebase. I guess I'd be happy to review how the change looked if someone wanted to go ahead and issue a pull request that changed all those signatures, and switched up our own mypy rules to enforce it, but I certainly wouldn't be able to say if we'd accept the pull request or not, so it'd need someone who's willing to put in that time, without having any guarantees that it'd make it in. |
Beta Was this translation helpful? Give feedback.
#1439 seems related to this but as random input i've also had a similar issue with having to silence type warnings when wrapping the AsyncClient