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 additional optional type parameter to typing.ContextManager and typing.AbstractContextManager #118660

Closed
Daverball opened this issue May 6, 2024 · 0 comments
Labels
topic-typing type-feature A feature request or enhancement

Comments

@Daverball
Copy link

Daverball commented May 6, 2024

Feature or enhancement

Proposal:

As part of python/typeshed#11048 contextlib.AbstractContextManager and contextlib.AbstractAsyncContextManager received an additional optional type parameter for the return value of __exit__ in order to have greater control over code-flow analysis in type checkers with things like ExitStack.

The contextlib ABCs don't require any changes, since they accept an arbitrary number of arguments at runtime, but the aliases that live in typing are a different story. These will reject the additional parameter at runtime, throwing an exception.

@JelleZijlstra Added support for type var defaults for typing._SpecialGenericAlias in #118648, we could leverage that work to add the new optional parameter to the typing aliases, so they can support the same number of type parameters as their contextlib counterparts.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/add-an-else-clause-to-with-statements-to-detect-early-termination/38031

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants