Skip to content
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

Merged
merged 4 commits into from
Jan 11, 2021
Merged

Add type stubs #365

merged 4 commits into from
Jan 11, 2021

Conversation

sbdchd
Copy link
Contributor

@sbdchd sbdchd commented Dec 27, 2020

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/

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
Copy link

codecov bot commented Dec 27, 2020

Codecov Report

Merging #365 (c4f730f) into master (5aa2844) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #365   +/-   ##
=======================================
  Coverage   96.25%   96.25%           
=======================================
  Files           3        3           
  Lines        1282     1282           
=======================================
  Hits         1234     1234           
  Misses         48       48           
Impacted Files Coverage Δ
responses/__init__.py 97.69% <ø> (ø)
responses/test_responses.py 97.82% <ø> (ø)
setup.py 0.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5aa2844...c4f730f. Read the comment docs.

match_querystring: bool = ...,
match: List[Any] = ...,
) -> None: ...
def get_response( # type: ignore [override]
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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):
Copy link
Contributor Author

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"],
Copy link
Contributor Author

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] = ...,
Copy link
Contributor Author

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

@markstory markstory merged commit a7d15eb into getsentry:master Jan 11, 2021
This was referenced Mar 17, 2021
This was referenced Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding type hints
2 participants