Skip to content

Commit 1ad4593

Browse files
committed
fix: Make context manager __exit__/__aexit__ signatures compatible with 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.
1 parent 6157d89 commit 1ad4593

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

playwright/_impl/_async_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ async def __aenter__(self: Self) -> Self:
9696

9797
async def __aexit__(
9898
self,
99-
exc_type: Type[BaseException],
100-
exc_val: BaseException,
101-
traceback: TracebackType,
99+
exc_type: Optional[Type[BaseException]],
100+
exc_val: Optional[BaseException],
101+
traceback: Optional[TracebackType],
102102
) -> None:
103103
await self.close()
104104

playwright/_impl/_sync_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ def __enter__(self: Self) -> Self:
142142

143143
def __exit__(
144144
self,
145-
exc_type: Type[BaseException],
146-
exc_val: BaseException,
147-
_traceback: TracebackType,
145+
exc_type: Optional[Type[BaseException]],
146+
exc_val: Optional[BaseException],
147+
_traceback: Optional[TracebackType],
148148
) -> None:
149149
self.close()
150150

0 commit comments

Comments
 (0)