Skip to content

TypeVar with constraints not resolving __getitem__ #3059

Closed

Description

@grievejia

Describe the Bug

Minimal repro:

from typing import TypedDict, TypeVar, Generic

class DeviceInfo(TypedDict):
    name: str
    address: str
    rssi: int


class ExtendedDeviceInfo(TypedDict):
    name: str
    address: str
    rssi: int
    manufacturer: str


NAME = "name"
ADDRESS = "address"

T = TypeVar("T", DeviceInfo, ExtendedDeviceInfo)


class DeviceIndex(Generic[T]):
    def __init__(self) -> None:
        self.by_name: dict[str, list[T]] = {}

    def add(self, device: T) -> None:
        name = device[NAME]  # FP: Cannot index into T (object has no __getitem__)
        self.by_name.setdefault(name, []).append(device)

    def lookup(self, device: T) -> str:
        return device[ADDRESS]  # FP: Cannot index into T

Mypy does error, but for a different reason. Ty and Pyright are both OK with this.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AVZjEwARCAGMGAGkHCAaqkqyA4jHQxKkgDrpdEqKjhw6omADdJMAJLowuABRDiI8VICUiXXR910qGhhEOjgGSm9fVExMSng4YNDw9F86SmMIYNYGXRz0AyMTAFF8BnVMVwsrW3snYTFJBk8In39AhLDmuiiYuPaklLS4DM50bOTfGgwAVzBUKSnYyj7c3QA5AEEAWUK6AF46bRBWmEPdddFRACVCgGUbvYOQbtjjU710AQfnGEVKB0OBIdZGZLBIbHZcLJiqUsBVQeD7O4VnlDMZTJUwbZyvgHGoNFoJABtAQAXSa4x85TAdAA%2BjTWBAGHSHHAYFAwO46ABaAB8dFWuA0XgpKRCbLAhGwpBpx2CmAahMSsigEFCxJJJIewAAvrkUlSutEWeLZOV4cEBJzefzBUFOiljg8zVZCRttpq6ABiOgAMQACsEAMIYdC4fisbEjBi4QR0By4bAAKxgUjoAAsjH4Y3S2DAGIyYDQ6UiRSlWezJdLjoRWQwqagplAGA5jrJCWTCKhiC4sA5nWCS50DVBcLgANZTYjG9mmjFBQRWvmJYWi3yxBgLZL9mCE85XW43D3e-1BkNhkaRrIxgQgaQgKb5qBwEjkRAgb0AVUfjNIdDAUzyfNBTgXIDXsShJiZdAphobBNAcfBMlGRcQjCFc1zzTc-0OVYYLgpY6GAfBdRAXRbxAMhYjAKBSEIaMaCgChvT9UgqJokIMBwAg6AkQVIDYBZUCA9BCF0b0bhgGB0wYBhiHiAB6eTKJgajaN4Nh5PUeTMFwCQ4Hk3i7AgATKCEiBBXkv9eC6cxUGgVBsFgHi%2BOMwThLoXBiGE59dDIBg00FLlzE0IZBQeQ4AGZCAARgAJjeEBtTvOZ82Cn1oBgCg0CwPAiDIRKgA

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    usabilityUsability & readiness issues identified with running Pyrefly on top OSS projects
    on Apr 7, 2026
  2. asukaminato0721 commented on May 11, 2026

    @asukaminato0721
    Collaborator

    claim

  3. yangdanny97 commented on Jun 2, 2026

    @yangdanny97
    Collaborator

    This is because the way we preserve both T & the bound when doing attribute access is via a special AttributeBase, which stores both the quantified and the bound. The bound is represented as a ClassType, so if the bound isn't a ClassType our handling doesn't work.

    If the bounds here were list, dict, etc. it wouldbe fine. Tuple I'm not sure. But the issue is that TypedDict is not a class.

    quantified_bound_class is the relevant function

  4. yangdanny97 commented on Jun 2, 2026

    @yangdanny97
    Collaborator

    The current logic of calling getitem won't work perfectly since tuple/typeddict have special semantics there, so maybe it needs to be special cased somehow, as in asuka's PR

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