-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strict mypy check, update docs on type checking
- Loading branch information
Showing
5 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Type Checking | ||
============= | ||
|
||
You should be able to use *mypy* and other type checkers to verify the | ||
correctness of your code. The library itself is checked with the ``--strict`` | ||
flag. | ||
|
||
For the most part, you should not have issues with type checking except when | ||
passing keyword arguments to your endpoints. Unfortunately you will see the | ||
following error. | ||
|
||
:: | ||
|
||
error: Unexpected keyword argument "arg" for "call" of "Client" | ||
[call-arg] | ||
|
||
This is due to inherent limitations with the typing spec as of Python 3.12, | ||
and the fact that keyword arguments cannot be concatenated for type checking | ||
purposes. For more information, see `pep`_ 612. | ||
|
||
.. _pep: https://peps.python.org/pep-0612/#concatenating-keyword-parameters | ||
|
||
Mitigations | ||
----------- | ||
|
||
One way around this is to include arbitrary keyword-only arguments in your | ||
endpoint definition. This will let mypy know that the wrapper function can | ||
also accept arbitrary keyword-only arguments. The obvious downside is that | ||
it does not look very clean and if you have multiple endpoints it can get | ||
tiring to write them like this. | ||
|
||
:: | ||
|
||
from typing import Any | ||
|
||
@get('/my_endpoint/{item_id}') | ||
def my_call(self, response, /, **_: Any) -> str: | ||
return response | ||
|
||
The other way is to manually silence this error for a certain scope. | ||
For more information, see the `mypy`_ docs. | ||
|
||
.. _mypy: https://mypy.readthedocs.io/en/stable/error_codes.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters