Typhinting as art in Python
Lets look at the get method of requests library.
def get(
url: str | bytes,
params: _Params | None = ...,
*,
data: _Data | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
timeout: _Timeout | None = ...,
allow_redirects: bool = ...,
proxies: _TextMapping | None = ...,
hooks: _HooksInput | None = ...,
stream: bool | None = ...,
verify: _Verify | None = ...,
cert: _Cert | None = ...,
json: Any | None = ...,
) -> Response:
Do you notice how the parameters are typehinted?
Example:
params: _Params | None = ...,
In this case literally a whole sentence is hidden behind this typehint.
The developers say "For the param you need to provide a datatype similar to Params class (which is a Python Type Annotation class), or, don't provide anything at all, and in that case the param will be reassigned inside of the method".