astral-sh/ruff

non-pep695-generic-function (UP047) weirdness

Open

#19,155 创建于 2025年7月5日

在 GitHub 查看
 (7 评论) (0 反应) (0 负责人)Rust (2,088 fork)batch import
documentationhelp wanted

仓库指标

Star
 (47,527 star)
PR 合并指标
 (平均合并 3天 8小时) (30 天内合并 573 个 PR)

描述

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

贡献者指南