Skip to content

False positive "Access a generic instance variable" #10304

@erictraut

Description

@erictraut

Discussed in #10303

Originally posted by UlfurOrn April 11, 2025
Given a generic instance variable on a class which is bound to a specific type. Is it ambiguous to access this variable as long as I treat the variable as the type it is bound to?

Here is some example code to hopefully get my point across:

from pydantic import BaseModel


class MyBaseClass[T : BaseModel]:
    model: type[T]


def func(cls: type[MyBaseClass]):
    print(cls.model)  # Access to generic instance variable through class is ambiguous


class MyModel(BaseModel):
    field: str


class MyClass(MyBaseClass[MyModel]):
    model = MyModel


func(MyClass)

In this example, the model field on the MyBaseClass class is bound to BaseModel.

I would have assumed that as long as I treat access to cls.model in func the same as I would if it were typed as type[BaseModel] that nothing ambiguous has occured.

Is this something that could/should be supported? If not I'd be interested in knowing why just for myself to learn from :)

Example without pydantic dependency:

class MyBaseClass[T : int]:
    number: T


def func(cls: type[MyBaseClass]):
    print(cls.number)  # Access to generic instance variable through class is ambiguous

There are some similar discussion but none that I've found which include the use of a bound generic:

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions