astral-sh/ruff

non-pep695-generic-function (UP047) weirdness

Open

#19 155 ouverte le 5 juil. 2025

Voir sur GitHub
 (7 commentaires) (0 réactions) (0 assignés)Rust (2 088 forks)batch import
documentationhelp wanted

Métriques du dépôt

Stars
 (47 527 stars)
Métriques de merge PR
 (Merge moyen 3j 8h) (573 PRs mergées en 30 j)

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

Guide contributeur