-
Notifications
You must be signed in to change notification settings - Fork 141
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
Fix __exit__
return type of various context managers
#849
Conversation
There's one more context manager which could have its return type adjusted: |
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.
Just a little change request, and a couple minor questions.
Did the pre-commit checks pass for you locally? |
Co-authored-by: Alex Grönholm <[email protected]>
I did not run pre-commit locally. I see that mypy is failing in three places unrelated to my PR.
This seems to be a problem in Trio itself. |
Why am I not seeing any pre-commit failures on latest
|
Okay, correction: Mypy is failing as it now sees code within With that said, this becomes a non-trivial problem typing-wise; I can see that those CancelScopes do not have a deadline set, which means they won't actually be suppressing; there's hardly any way to communicate that to a type checker. |
Fixing 2 out of the 4 reports would require either @agronholm, what would you have me do? |
Dang, this turned out to be way harder than I thought. Could we reduce the scope of this PR to just fix the non-problematic CMs? |
Done, although this PR no longer fixes the issue I've opened. I presume you'd still want to "fix" the issue at some point to be on pair with trio? ( |
Before I do anything, let's take stock on the remaining changes you wanted to make. Which classes where did you still need to fix and what were the issues there? |
I wanted to fix Changing the return type from with move_on_after(5):
await sleep(10)
result = 123
print(result) # -> bool | None => no issues, -> bool => result may be undefined but at the same time reports an issue for shield-only scopes (which never suppress at runtime): with CancelScope(shield=True):
await sleep(10)
result = 123
print(result) # this should be fine, but is now reported as potentially undefined The issues reported by mypy were at places where a shield-only |
This will still suppress a cancellation exception: with CancelScope(shield=True) as scope:
scope.cancel()
await sleep(10)
result = 123 |
Yes, but I meant specifically a |
There is no way to tell a type checker that a cancel scope won't suppress an exception. The way I see, it, |
Technically there is, but it's hardly practical.We could make @overload
def __init__(self: CancelScope[Literal[False]], shield: bool = ...) -> None: ...
@overload
def __init__(self: CancelScope[bool], shield: bool = ..., deadline: float = ...) -> None: ...
def cancel(self: CancelScope[Literal[True]] | CancelScope[bool]) -> None: ... # not bindable on shield-only scope
@deadline.setter
def deadline(self: CancelScope[Literal[True]] | CancelScope[bool], value: float) -> None: ... One could still create a cancellable Either way, I think that the benefits of type checkers reporting reachability problems outweigh the false positive on |
I don't understand your reasoning for these overloads. "not bindable on shield-only scope"? What does that even mean? |
Oh, wait, you meant that you would do |
If that's the case, then I think I got it. But I'm not very keen on that – I might consider it if Trio implements it, but not otherwise. At any rate, switching to |
Ok, I'll merge this some time later then, provided the changes we spoke of have been added. Thanks! |
I have undone the changes I've made on I can reintroduce them by silencing the issues with |
Can you show me the mypy errors? |
I'm working with pyright on my side (which generates a lot of errors for the repo), and pre-commit only runs on staged files. |
Okay. |
OK, so:
|
Ok, I think I can deal with these (later). |
I couldn't figure out a better way to deal with two of these errors than to add those type ignore comments. |
That's fine, one of the purposes of If we wanted to conform to the current type system, the functionality of |
Changes
Fixes #847.
A few context managers which can suppress exceptions had their
__exit__
method's return type incorrectly annotated asbool | None
, where according to typing docs it should bebool
.I've also included changes for
bool | None
->None
in places wherebool
is never returned. This shouldn't have any effect.Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/
) added which would fail without your patchdocs/
, in case of behavior changes or newfeatures)
docs/versionhistory.rst
).If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.
Updating the changelog
If there are no entries after the last release, use
**UNRELEASED**
as the version.If, say, your patch fixes issue #123, the entry should look like this:
If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.