Skip to content

covariant warnings.catch_warnings type-parameter with default #13675

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 2 commits into from
Mar 20, 2025

Conversation

jorenham
Copy link
Contributor

This changes the type parameter of warnings.catch_warnings in two ways:

  • set covariant=True — as it's only used within output positions — and renamed it accordingly
  • have it default to its upper bound so that it isn't required anymore to pass a type-argument to a class that is not subscriptable at runtime

The invariance was causing issues within the numpy stubs for a generic a subclass:
https://github.com/numpy/numpy/blob/0aafd8553a986c7d39dde489ac5d42e53322b090/numpy/testing/_private/utils.pyi#L138-L147 to be precise

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/recwarn.py:170: error: Unused "type: ignore" comment  [unused-ignore]
+ src/_pytest/recwarn.py:229: error: Return type "WarningsRecorder" of "__enter__" incompatible with return type "Optional[list[WarningMessage]]" in supertype "catch_warnings"  [override]

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks!

@srittau srittau merged commit c8bdfcb into python:main Mar 20, 2025
55 checks passed
@jorenham jorenham deleted the generic-warnings.catch_warnings branch March 20, 2025 12:27
@cdce8p cdce8p mentioned this pull request Apr 4, 2025
mmingyu pushed a commit to mmingyu/typeshed that referenced this pull request May 16, 2025
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.

2 participants