-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response parameters type hints #178
Response parameters type hints #178
Conversation
Makes improvements to the type hints of response parameters in both `MockerCore` and `Adapter`: - Added explicit parameters for `reason`, `cookies`, `json`, `content`, `body`, `raw` and `exc` as per the documentation at https://requests-mock.readthedocs.io/en/latest/response.html#registering-responses - Updated the `text` parameter to support static and dynamic values as per https://requests-mock.readthedocs.io/en/latest/response.html#dynamic-response - Ensured that the "new" `json`, `content` and `body` parameters support both static and dynamic values, just like the `text` parameter
It does appear that all the dynamic typing that was done for the API makes getting type annotations right for this pretty challenging :) Out of interest - it doesn't appear here that having _Context and _RequestObjectProxy as private makes any difference? At one point someone had asked to make them public as it would simplify some IDE things. |
text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., | ||
content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., | ||
body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., | ||
raw: HTTPResponse = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting - i always thought this was callable as well. Guess noone has ever used that.
@jamielennox wrote:
Making the types public would help users of the API to annotate their own callback functions. As it is, a linter (e.g. pylint) might complain at the use of private types in the parameter annotations. For example, if I were to write my own def text_callback(request: _RequestObjectProxy, context: _Context) -> str:
context.status_code = 200
context.headers['Test1'] = 'value1'
return 'response' Of course, changing the |
Related to #173.
Makes improvements to the type hints of response parameters in both
MockerCore
andAdapter
:reason
,cookies
,json
,content
,body
,raw
andexc
as per the documentation at https://requests-mock.readthedocs.io/en/latest/response.html#registering-responsestext
parameter to support static and dynamic values as per https://requests-mock.readthedocs.io/en/latest/response.html#dynamic-responsejson
,content
andbody
parameters support both static and dynamic values, just like thetext
parameterAlso, as per @jamielennox's suggestion, I added "single star" parameters from PEP 3102 to indicate where the list of keyword-only arguments begin in both
MockerCore
andAdapter
.