-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
Add type stubs #365
Add type stubs #365
Conversation
Add type stubs for the library as a separate stub file which means we can use the python 3 syntax, with the downside of having to keep them in sync with the source. I ran [monkeytype][1] and [stubgen][2] over responses.py and combined the results manually. I had to move the module into a package for the types to work. see: https://www.python.org/dev/peps/pep-0561/#packaging-type-information Fixes getsentry#339 https://www.python.org/dev/peps/pep-0561/ [1]: https://github.com/Instagram/MonkeyType [2]: https://mypy.readthedocs.io/en/stable/stubgen.html
Codecov Report
@@ Coverage Diff @@
## master #365 +/- ##
=======================================
Coverage 96.25% 96.25%
=======================================
Files 3 3
Lines 1282 1282
=======================================
Hits 1234 1234
Misses 48 48
Continue to review full report at Codecov.
|
match_querystring: bool = ..., | ||
match: List[Any] = ..., | ||
) -> None: ... | ||
def get_response( # type: ignore [override] |
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.
I used the following config when developing, not sure if you want mypy running or not in CI
[mypy]
show_column_numbers=True
show_error_codes = True
disallow_any_unimported=True
disallow_any_expr=False
disallow_any_decorated=True
disallow_any_explicit=False
disallow_any_generics=True
disallow_subclassing_any=True
disallow_untyped_calls=True
disallow_untyped_defs=True
disallow_incomplete_defs=True
check_untyped_defs=True
disallow_untyped_decorators=True
no_implicit_optional=True
strict_optional=True
warn_redundant_casts=True
warn_unused_ignores=True
warn_no_return=True
warn_return_any=False
warn_unreachable=False
strict_equality=True
ignore_missing_imports=False
[mypy-urllib3.*]
ignore_missing_imports=True
[mypy-test_responses]
disallow_untyped_defs=False
disallow_untyped_calls=False
disallow_any_decorated=False
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.
Having mypy run as part of CI would be ideal. I can take care of that though.
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.
How were you running mypy locally? When I ran it with this configuration file there were ~380 errors from the test_responses module.
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.
not sure what command I was running, but were you using the config above?
[mypy-test_responses]
disallow_untyped_defs=False
disallow_untyped_calls=False
disallow_any_decorated=False
should ignore most of the errors until the tests module is fully typed
class _Activate(Protocol): | ||
def __call__(self, func: _F) -> _F: ... | ||
|
||
class _Add(Protocol): |
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.
I ended up doing this really ugly protocol stuff since there isn't really a good way to get the function type out of the class for reusing below.
Once python 2 support is removed, add the types are added inline, we won't need any of this Protocol stuff
@@ -62,14 +62,15 @@ def run_tests(self): | |||
license="Apache 2.0", | |||
long_description=open("README.rst").read(), | |||
long_description_content_type="text/x-rst", | |||
py_modules=["responses"], | |||
packages=["responses"], |
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.
content_type: Optional[str] = ..., | ||
adding_headers: Optional[Mapping[str, str]] = ..., | ||
match_querystring: bool = ..., | ||
match: List[Any] = ..., |
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.
I changed the methods that take *args
and **kwargs
to list out the specific parameters that are used instead to help with typos
Add type stubs for the library as a separate stub file which means we can
use the python 3 syntax, with the downside of having to keep them in
sync with the source.
I ran monkeytype and stubgen over responses.py and combined the
results manually.
I had to move the module into a package for the types to work.
see: https://www.python.org/dev/peps/pep-0561/#packaging-type-information
Fixes #339
https://www.python.org/dev/peps/pep-0561/