From 72df055fc1aab911201cec7613f501f1e0a83268 Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 19 Mar 2025 20:28:11 +0100 Subject: [PATCH 1/2] use a covariant generic type parameter for `warnings.catch_warnings` --- stdlib/warnings.pyi | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/warnings.pyi b/stdlib/warnings.pyi index 533a36817506..99e0ef75a937 100644 --- a/stdlib/warnings.pyi +++ b/stdlib/warnings.pyi @@ -21,7 +21,8 @@ if sys.version_info >= (3, 13): __all__ += ["deprecated"] _T = TypeVar("_T") -_W = TypeVar("_W", bound=list[WarningMessage] | None) +_W_co = TypeVar("_W_co", bound=list[WarningMessage] | None, covariant=True) + if sys.version_info >= (3, 14): _ActionKind: TypeAlias = Literal["default", "error", "ignore", "always", "module", "once"] else: @@ -66,7 +67,7 @@ class WarningMessage: source: Any | None = None, ) -> None: ... -class catch_warnings(Generic[_W]): +class catch_warnings(Generic[_W_co]): if sys.version_info >= (3, 11): @overload def __init__( @@ -113,7 +114,7 @@ class catch_warnings(Generic[_W]): self: catch_warnings[list[WarningMessage] | None], *, record: bool, module: ModuleType | None = None ) -> None: ... - def __enter__(self) -> _W: ... + def __enter__(self) -> _W_co: ... def __exit__( self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None ) -> None: ... From 91e7793b977ac8535be7c473a671518957a87ed2 Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 19 Mar 2025 20:41:49 +0100 Subject: [PATCH 2/2] set a default for the type-parameter of `warnings.catch_warnings` --- stdlib/warnings.pyi | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/stdlib/warnings.pyi b/stdlib/warnings.pyi index 99e0ef75a937..49c98cb07540 100644 --- a/stdlib/warnings.pyi +++ b/stdlib/warnings.pyi @@ -3,8 +3,8 @@ import sys from _warnings import warn as warn, warn_explicit as warn_explicit from collections.abc import Sequence from types import ModuleType, TracebackType -from typing import Any, Generic, Literal, TextIO, TypeVar, overload -from typing_extensions import LiteralString, TypeAlias +from typing import Any, Generic, Literal, TextIO, overload +from typing_extensions import LiteralString, TypeAlias, TypeVar __all__ = [ "warn", @@ -21,7 +21,7 @@ if sys.version_info >= (3, 13): __all__ += ["deprecated"] _T = TypeVar("_T") -_W_co = TypeVar("_W_co", bound=list[WarningMessage] | None, covariant=True) +_W_co = TypeVar("_W_co", bound=list[WarningMessage] | None, default=list[WarningMessage] | None, covariant=True) if sys.version_info >= (3, 14): _ActionKind: TypeAlias = Literal["default", "error", "ignore", "always", "module", "once"] @@ -93,7 +93,7 @@ class catch_warnings(Generic[_W_co]): ) -> None: ... @overload def __init__( - self: catch_warnings[list[WarningMessage] | None], + self, *, record: bool, module: ModuleType | None = None, @@ -110,9 +110,7 @@ class catch_warnings(Generic[_W_co]): self: catch_warnings[list[WarningMessage]], *, record: Literal[True], module: ModuleType | None = None ) -> None: ... @overload - def __init__( - self: catch_warnings[list[WarningMessage] | None], *, record: bool, module: ModuleType | None = None - ) -> None: ... + def __init__(self, *, record: bool, module: ModuleType | None = None) -> None: ... def __enter__(self) -> _W_co: ... def __exit__(