Skip to content

Missing attribute false positive from TypeVar bounded with type #2614

@grievejia

Description

@grievejia

Describe the Bug

Minimal repro:

"""
When a TypeVar has a bound (e.g., bound=type), pyrefly resolves attribute
access on the TypeVar to `object` instead of the bound type. This causes
false positives when accessing attributes that exist on the bound type
but not on `object`.
"""
from typing import TypeVar

# TypeVar bound to `type` -- accessing `__name__`
# `type` has a `__name__` attribute, so cls.__name__ should be valid
# when T is bound to `type`. Pyrefly resolves T as `object` instead.
T = TypeVar("T", bound=type)
def get_class_name(cls: T) -> str:
    return cls.__name__  # FP: Object of class `object` has no attribute `__name__` 

No other type checker has this behavior.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeSIAOlTZegOoAWM6ABKqwCqnEwBqqAE6tGqOO1Z4ArllYAKGIQDmhADSTcMzAF4ALjxgBKdWUEwwUUqzNxcUAG7x2u3YIjYpumHVQBjX-DiuGy6zFwGAsK6uKwABrjYAFYwvrqxrBDocF6omKy4YKyhMBpaRQaEXIwQ4r6oUnDwdGCoUI2sxLhwELoQjuIA7sxsfgFw3ehKzq7unk6hqLqsMPg1S8FFYdKy%2Brx0HkvouOts8UkpaYR01DdU6GCCuAC25cSZUxBPnYJL3LyR13QAGJwv8hKUdjFYrsYOkALRw9j%2BQLvOIAfTR6FQTxgGNidBB0IM6VE4g4sQxWJxeOmbgOMHUtlYvjahEp2NxaNYcEYmigeWwJXsrQgmAJrCGLC4GXE2zy0TiMNilQACqQzBYrDY7P1pWI4glkql0plsjBcld0JxWNpQfwhHJqJxqOo5XoDIY6JhzKwlDBdGiWWI4JiOXIWXBEFxDKw4QA%2BbmuRB0Vip6z%2BqSCNgRtmh6lc1gggBiKqjAHlzql8oUg%2BMDZW0iJ9UdabMvOi85z0iBVCBPNA4CRyIgQCCAKq9KA9KxgGSpCDBOCA72FMC4QRPRaYqRPQWCOT4KOZXQx%2BOJwTJthpsy6TNsMDUAByO73UeA%2BAAvjd0D2QKZzJYhDRE8UAUCCaoapY3IYDgBDMsEkBKJmiwLugloggAyjAJSMC4xCRgA9AR-6aoQ65KARLAEZguC%2BHABG%2BAhEBIYIKHBARrBrsIqDCtAqDYLA8H3MxyG9BsuDEGJWSWmQoTBHCjiCN0Gy2tQADMhAAIwAEzfiAH69n4vSOEW0AwBQaBYHgRBkPpQA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

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