astral-sh/ruff

non-pep695-generic-function (UP047) weirdness

Open

Aperta il 5 lug 2025

Vedi su GitHub
 (7 commenti) (0 reazioni) (0 assegnatari)Rust (47.527 star) (2088 fork)batch import
documentationhelp wanted

Descrizione

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

Guida contributor