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
#11795 Update the return type of __aexit__ #11796
#11795 Update the return type of __aexit__ #11796
Conversation
Specifying Literal[False] tells the type checker that we don't suppress exceptions.
for more information, see https://pre-commit.ci
please review |
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.
Many thanks for the changes.
Is this a bugfix or a feature?
We have a good set of automated tests and mypy is part of the test suite.
Why was an error similar to foo.py:3: error: Missing return statement
not already detected by our tests.
Would it be possible to have a test that will make sure that we will not introduce a regression with future changes?
It looks like the code has coverage ... so I think that tests are executed.
it might be that we don't have annotations in the tests or we don't run mypy on the test code.
The normal dev process require an updated test for every code change.
I think that is ok to merge this without an updated test.
But I am leaving this for review, so that another Twisted dev can change the change and approve the "missing tests" policy exception for this PR.
Or if you can find a way to run a mypy test as part of the changes from this PR, then we can merge without the need for a review from another dev.
Thanks again!
src/twisted/internet/defer.py
Outdated
def __aexit__( | ||
self, exc_type: bool, exc_val: bool, exc_tb: bool | ||
) -> Deferred[Literal[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.
I note in passing that the annotations for exc_type
, exc_val
and exc_tb
types look wrong here, see e.g. https://github.com/python/typeshed/blob/a7cf6144deeb3f99570f8b97fb5f5ecf16f20c01/stdlib/contextlib.pyi#L52-L54
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.
A good point in favor of having some kind of test for these changes.
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.
These annotations still look wrong.
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.
Oh sorry. I was focusing so much on the testing I missed this comment. I can update that, but I'm not sure how to make mypy complain about this annotation in order to "test" it.
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.
Nor I :/
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.
Done!
Thanks for the review.
I wasn't sure what to call it but saw that #11754 called it a feature. Though bugfix might be more reasonable and can change it to that.
Currently mypy is tested by running mypy over the entire codebase. And this error only triggers in the following two ways:
or
The 2nd one complains because Weirdly the following doesn't get a complaint from mypy:
Which is the way our tests use it in Note: This seems to be a common pattern in mypy. Check out the following:
Hmm. I can trigger the error in that test ( As for full mypy testing, I've seen other projects use something like https://pypi.org/project/pytest-mypy-plugins/ or https://pypi.org/project/pytest-mypy-testing/ to test their stubs. I don't know if you'd rather have that, but I don't know anything about how this project is tested. |
Thanks for the update. If you change the test code and run the |
Here's the error before:
|
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.
This looks good to me. There's a broader conversation to be had about how to verify type annotations separately from behavior, and particularly how to verify that application code will get errors sometimes, but I think that this has been conscientiously verified as well as we can with our current tools. Let's land!
Is anything else missing? |
I have enabled auto-merge for this since Glyph has approved it. Many thanks David for the patch and sorry for the delay. |
Scope and purpose
Fixes #11795
Specifying Literal[False] tells the type checker that this context manager doesn't suppress exceptions.
Contributor Checklist:
This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.
Below is a non-exhaustive list (as a reminder):
please review
.Our bot will trigger the review process, by applying the pending review label
and requesting a review from the Twisted dev team.