Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Parameterized types of generic classes sometimes shared between class instances #590

Description

In the below, both intval and strval come to be int, even though hovering over boxedint and boxedstr show Box[int] and Box[str] respectively.

from typing import TypeVar, Generic

_T = TypeVar("_T")

class Box(Generic[_T]):
    def __init__(self, v: _T):
        self.v = v
    
    def get(self) -> _T:
        return self.v


boxedint = Box(1234)
intval = boxedint.get()

boxedstr = Box("foo")
strval = boxedstr.get()

Activity

  1. changed the title [-]Parameterized types of generics sometimes shared between intances[/-] [+]Parameterized types of generics sometimes shared between instances[/+] on Feb 8, 2019
  2. changed the title [-]Parameterized types of generics sometimes shared between instances[/-] [+]Parameterized types of generic classes sometimes shared between class instances[/+] on Feb 8, 2019
  3. MikhailArkhipov commented on Feb 8, 2019

    @MikhailArkhipov

    Most probably relates to the -> _T tooltip on get()

  4. MikhailArkhipov commented on Feb 9, 2019

    @MikhailArkhipov

    OK, got it. The problem was that when specific type was created from template, it reused same method type instances since creation of the specific type just added references and then setting the result type set it to all specific types and the template.

    Better solution is to remember supplied type arguments in the specific type (i.e. what the type was created from) and have function determine return type dynamically from them. This way we don't need to clone function type for the specific class. It is also provides better encapsulation since class type then knows less about its method type innards.

    Similarly, dynamically serving return value documentation will eliminate issue that tooltip over method of the specific class shows -> _T.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions