Skip to content

UP047 Fails to indentify functions that have Callable in their signature #19863

Closed

Description

@Kroppeb

Summary

I noticed something weird with UP047, foo triggers a warning while foo2 does not.

from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")


def foo(t: list[T]) -> T:
    return t[0]


def foo2(t: list[T], c: Callable[[T], None]) -> T:
    c(t[0])
    return t[1]

Playground: https://play.ruff.rs/cd7af9a8-0532-40fe-83c7-4282a658808c

Version

v0.12.7

Activity

  1. added
    bug
    rule
    on Aug 11, 2025
  2. MichaReiser commented on Aug 11, 2025

    @MichaReiser
    Member

    Thanks for opening an issue. This was also discussed in our Discord (conversation).

    Specifically, @AlexWaygood replied

    There was a bug in collections.abc.Callable on Python 3.9 IIRC, which means it isn't safe to replace typing.Callable with collections.abc.Callable on all Python versions (it works most of the time on py39, but not in some edge cases, I think?)
    Should be safe on the newest Python versions, though. Can't remember exactly when the bug was fixed

  3. ntBre commented on Aug 11, 2025

    @ntBre
    Contributor

    I talked to Alex, and I don't think that Python bug should affect this rule, which is only replacing the type of generics, not importing Callable from a different module. I believe he was thinking of deprecated-import (UP035), which will replace typing.Callable with collections.abc.Callable after Python 3.10.

  4. AlexWaygood commented on Aug 11, 2025

    @AlexWaygood
    Member

    Yup, @ntBre is correct — my bad! sorry about that 🫠

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugrule

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions