Skip to content

Method access of generic class has Unknown rather than generic type. #6907

@draustin

Description

@draustin

Describe the bug

In strict mode, method access on a generic class (not an instance) gives a type is partially unknown error. The problem arose for me calling __init__ of a generic superclass (example 1 below). But I found that it is not specific to __init__ - it happens with any method.

I tried supply the type as an argument i.e A[int].__init__. This passes type checking but doesn't actually call the method at runtime.

I would expect a (unbound) method to be treated similarly to a generic (non-method) function i.e. using the @ notation. My example 2 below illustrates the point.

Code or Screenshots

Example 1 - calling superclass __init__:

from typing import Generic, TypeVar

T = TypeVar("T")

class A(Generic[T]):
    def __init__(self, x:T):
        ...

class B(A[int]):
    def __init__(self, x:int):
        A.__init__(self, x) # Type of "__init__" is partially unknown

(Pyright playgrround)

Example 2 - inconsistency with (non-method) generic function:

from typing import Generic, TypeVar

T = TypeVar("T")

def g(a: T, x:T):
    ...

# Revealed type is "(variable) def g2(a: T@g, x: T@g) -> None".
g2 = g

class A(Generic[T]):
    def f(self, x: T):
        ...

# Partially unknown type.
# I would expect revealed type to be something like "f(self: A[T@f], x: T@f)".
f = A.f

Playground

VS Code extension or command-line
Pyright playgrround, strict mode, latest version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    as designedNot a bug, working as intendedbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions