astral-sh/ruff

non-pep695-generic-function (UP047) weirdness

Aberta

#19.155 aberto em 5 de jul. de 2025

 (7 comentários) (0 reação) (0 responsável)Rust (2.088 forks)batch import
documentationhelp wanted

Métricas do repositório

Stars
 (47.527 estrelas)
Métricas de merge de PR
 (Mesclagem média 3d 8h) (573 fundiu PRs em 30d)

Description

Summary

Proposed fix seems to break the typing, as it would remoev the existing type boundaries of a TypeVar:

import sys
from typing import TypeVar


class Class1:
    param = 1


class Class2:
    param = 2


type AnyofTwo = Class1 | Class2
SameClassT = TypeVar("SameClassT", Class1, Class2)


def print_foo(arg: AnyofTwo) -> None:
    sys.stderr.write(f"{arg.param}\n")


def universal_func_1(arg: SameClassT) -> SameClassT: # RUFF /* W: Generic function `universal_func_1` should use type parameters
    print_foo(arg)
    return arg


def universal_func_2[T](arg: T) -> T:
    """
    This is how `ruff check --fix` would modify `universal_func_1()`.
    """
    print_foo(arg) # MYPY /* E: Argument 1 to "print_foo" has incompatible type "T"; expected "Class1 | Class2"  [arg-type]
    return arg

$ ruff check test_pyrefly_01.py
test_pyrefly_01.py:21:5: UP047 [*] Generic function `universal_func_1` should use type parameters
   |
21 | def universal_func_1(arg: SameClassT) -> SameClassT:
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP047
22 |     print_foo(arg)
23 |     return arg
   |
   = help: Use type parameters

Found 1 error.
[*] 1 fixable with the --fix option.

Version

ruff 0.12.1

Guia do colaborador