Skip to content

Generic function accidentally captured as closure default argument #2872

Closed

Description

@grievejia

Describe the Bug

Minimal repro:

from typing import Any
import bisect

class Worker:
    def __init__(self) -> None:
        self.heartbeats: list[float] = []
        self.event = self._create_event_handler()  # pyrefly FP: TypeVar not in scope

    def _create_event_handler(self):
        heartbeats = self.heartbeats

        def event(
            timestamp: float | None = None,
            insort: Any = bisect.insort,
        ) -> None:
            if timestamp is not None:
                insort(heartbeats, timestamp)

        return event

Pyrefly is the only type checker that wants to emit error on this code.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BBdKQA66Ljz51sEODADGDMWPlRUcOHQDqvANYxKiMXRN1MMMHQD6V1hAY2AFHKhgAlHQC0APjoA5XHQYI3RTMLoXMEIACxhUPmw4hjhEOihZBgBtMChcVAYAXToAXjpMguNwk0jCGAA3GHR%2BUpqreUokmCt6xodojExYSkcPOgBiOjIOnNI6ADEABVSAFWYYADV4unRcflYI%2BVxiGGVQ03NLNo78rp6mq36sIecYVzcQqpNY%2BIZE-M0Wq8ot8Ekk4KdPmYLHQ7gxHJVISYGFx4AxUNxUjk8vwAD7%2BQIwEr4oIAGgRiNYcF4DFSQlmpRkckUhEp1LJZyqHh8xOC5MhEEsyJoqPRxE4mh2-ACQQ%2BiKqrL4jhBvzBJMYKLgaO4bghnw6DAArpRQrCQCSQFMLFBSIQGLQoBQJgtSNNrREBnh8HRDuhIGwjfkIIFCGIJgBlGCE6IMBjEFIAenjlpmhF4bHjjXjmFw8jg8Z9foDyMC8boYF4dFQdVQ0FQ2Fg3sChcogcCdCOxfQcBD6DIDGigU8DUocCDoVKIhAAGZCABGABMk7EmQM1BHFXQBvE3GpMEwnkwEA6iggDSJAHIIGwdh1z2JJZ4OgBHA1HveefSkTyoeTyeAAuhzwAd3idA73QEAAF9zR-ZEGjmaAYAoNAsE9EhyEgoA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    usabilityUsability & readiness issues identified with running Pyrefly on top OSS projects
    on Mar 23, 2026
  2. tejasreddyvepala commented on Apr 7, 2026

    @tejasreddyvepala
    Contributor

    @grievejia Root cause identified and fix ready.

    DefaultValue (the type that stores the inferred type of a parameter's default value) has a manual Visit impl that calls self.ty.recurse(f) instead of self.ty.visit(f). In the Visit framework, recurse only visits children of a node, while visit also yields the node itself when the target type matches. Every derived Visit impl uses visit, but this manual impl used recurse, so the Type stored inside a DefaultValue was invisible to any type-level traversal.

    This meant that when checking whether a class attribute's type contains unbound type variables, the checker found SupportsRichComparisonT and _T (from bisect.insort's signature) inside the default value, but couldn't see the Forall/Overload wrapper that binds them — because that wrapper is the Type node that was being skipped. So the type variables looked unbound, triggering the false positive.

    One-line fix: self.ty.recurse(f) → self.ty.visit(f) in DefaultValue's Visit and VisitMut impls.

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

Metadata

Metadata

Labels

typecheckingusabilityUsability & readiness issues identified with running Pyrefly on top OSS projects

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions