Skip to content

Generic classes with nested generic classes can't have different TypeVars #5551

Closed
@euresti

Description

@euresti

Looking at the following example

from typing import TypeVar, Generic

class C(Generic[T]):
    def __init__(self, t: T) -> None:
        self.t = t

    class F(Generic[K]):
        def __init__(self, k: K) -> None:
            self.k = k

        def foo(self) -> K:
            return self.k

C.F(17).foo()
C("").F(17).foo()  # error: Argument 1 to "F" has incompatible type "int"; expected "str"

reveal_type(C.F)  # 'def [K] (k: K`1) -> foo.C.F[K`1]'  All Good
reveal_type(C("").F)  # 'def [K] (k: builtins.str*) -> foo.C.F[builtins.str*]'   What?

Seems like specifying the T seems to crush K somehow.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions