Skip to content

Type variable substitution in type instances #156

Closed

Description

@gvanrossum

[In http://bugs.python.org/issue25087 Stefan Krah wrote:]

If a type scheme is instantiated, should the type variables in the
class body be substituted? This is an example (typed by hand on
a locked down Windows machine, may contain errors):

alpha = TypeVar('alpha')
beta = TypeVar('beta')

class ABTuple(Generic[alpha, beta]):
    def __init__(self, a : alpha, b : beta):
        self.value = (a, b)

get_type_hints(ABTuple.__init__)
 ==> {'b': ~beta, 'a': ~alpha}


IntIntTuple = ABTuple[int, int]

IntIntTuple
 ==> __main__.ABTuple[int, int]

get_type_hints(IntIntTuple.__init__)
{'b': ~beta, 'a': ~alpha}
      ^^^^^^      ^^^^^^

Since the type has been specialized, these should ideally be 'int'.

Activity

  1. gvanrossum commented on Sep 13, 2015

    @gvanrossum
    MemberAuthor

    Unfortunately this info is hard to retrieve at run time. The object returned by the expression IntIntTuple.__init__ is a plain function -- it only knows that it was defined in ABTuple. You could write a custom function though that is passed in separately the IntIntTuple class and the string '__init__' -- it can then do the expansion for you after introspection of IntIntTuple. (See also #136 (comment) and further comments).

  2. added a commit that references this issue on Oct 29, 2016
    50d2b4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions