astral-sh/ruff

non-pep695-generic-function (UP047) weirdness

Open

#19,155 建立於 2025年7月5日

在 GitHub 查看
 (7 留言) (0 反應) (0 負責人)Rust (47,527 star) (2,088 fork)batch import
documentationhelp wanted

描述

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

貢獻者指南