Skip to content

UP040 fix produces invalid syntax when a defaulted TypeVar precedes a non-defaulted TypeVar #27021

Description

@ntBre

Summary

The preview UP040 fix can emit an invalid PEP 695 type-parameter order when a defaulted TypeVar is followed by a non-defaulted TypeVar.

Playground reproduction

from typing import TypeAlias, TypeVar

T = TypeVar("T", default=int)
S = TypeVar("S")

Pair: TypeAlias = tuple[T, S]

With --target-version py313 --preview --select UP040 --unsafe-fixes, Ruff produces:

type Pair[T = int, S] = tuple[T, S]

Ruff then rejects its own output:

invalid-syntax: non default type parameter `S` follows default type parameter

UP040 should not offer a fix in this case unless it can generate a valid and equivalent type-parameter list. This blocks #26948.

This also affects UP046 and UP047.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationspreviewRelated to preview mode features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions