Skip to content

fix: Make context manager __exit__/__aexit__ signatures compatible with typing protocols #2915

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

Merged
merged 1 commit into from
Jul 10, 2025

Conversation

mxschmitt
Copy link
Member

Updates AsyncContextManager.__aexit__ and SyncContextManager.__exit__ to use Optional types for exception parameters (exc_type, exc_val, traceback) to match the standard Python typing protocols.

This aligns with the official type definitions in typeshed where AbstractContextManager and AbstractAsyncContextManager define these parameters as Optional since they are None when no exception occurs.

Ref: https://github.com/python/typeshed/blob/9317dc62bd4fb46b8b48ce5353286cab80308d47/stdlib/contextlib.pyi#L52-L54

Fixes type checker compatibility issues when using BrowserContext with type checkers that enforce strict protocol compliance.

Fixes #2856

…th typing protocols

Updates AsyncContextManager.__aexit__ and SyncContextManager.__exit__ to use
Optional types for exception parameters (exc_type, exc_val, traceback) to match
the standard Python typing protocols.

This aligns with the official type definitions in typeshed where
AbstractContextManager and AbstractAsyncContextManager define these parameters
as Optional since they are None when no exception occurs.

Ref: https://github.com/python/typeshed/blob/9317dc62bd4fb46b8b48ce5353286cab80308d47/stdlib/contextlib.pyi#L52-L54

Fixes type checker compatibility issues when using BrowserContext with
type checkers that enforce strict protocol compliance.
Copy link
Member

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exceptional!

@mxschmitt mxschmitt merged commit 8cd9256 into microsoft:main Jul 10, 2025
36 of 38 checks passed
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.

[Bug]: Type Hinting for BrowserContext.__aexit__ Incompatible with AbstractAsyncContextManager
2 participants