Skip to content

[pyupgrade] Fix false positive for TypeVar default before 3.12 (UP046)#23540

Merged
ntBre merged 2 commits intomainfrom
brent/up046-metaclass
Feb 24, 2026
Merged

[pyupgrade] Fix false positive for TypeVar default before 3.12 (UP046)#23540
ntBre merged 2 commits intomainfrom
brent/up046-metaclass

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 24, 2026

Summary

This was reported on Discord:

import abc
import typing
from typing import Generic

import typing_extensions

_Caps = typing_extensions.TypeVar("_Caps", bound=int | str, contravariant=True, default=int | str)


class CameraConnection(typing.Generic[_Caps]):  # okay
    pass


class Camera(Generic[_Caps], metaclass=abc.ABCMeta):  # UP046 false positive
    pass

In short, UP046 was suggesting to use PEP-695 type parameters in the second class on Python versions before 3.13 despite _Caps using a default, which is only supported in PEP-695 type parameters on 3.13 and later.

The fix looks unrelated at first glance, but this early return was preventing the diagnostic from being defused later for having a default.

Test Plan

New test derived from the report

this should not have a diagnostic because it uses a TypeVar with a default
@ntBre ntBre changed the title [pyupgrade] [pyupgrade] Fix false positive for TypeVar default before 3.12 (UP046) Feb 24, 2026
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

🤦 thank you!

@ntBre ntBre marked this pull request as ready for review February 24, 2026 16:46
@ntBre ntBre added bug Something isn't working rule Implementing or modifying a lint rule labels Feb 24, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 24, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre merged commit 5849e54 into main Feb 24, 2026
44 checks passed
@ntBre ntBre deleted the brent/up046-metaclass branch February 24, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants