Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing type arguments inside __init__ #519

Closed
mitar opened this issue Dec 16, 2017 · 2 comments
Closed

Accessing type arguments inside __init__ #519

mitar opened this issue Dec 16, 2017 · 2 comments

Comments

@mitar
Copy link
Contributor

mitar commented Dec 16, 2017

It seems this is not possible?

Example:

import typing

T = typing.TypeVar('T')

class Foo(typing.Generic[T]):
    def __init__(self):
        print(type(self).__args__)

A = Foo[int]
A()

I would expect that this prints out int as __args__, but it does not. It seems because runtime class is Foo, not Foo[int].

@ilevkivskyi
Copy link
Member

It seems because runtime class is Foo, not Foo[int].

Yes, and this is intentional. The original class is stored in __orig_class__ this is however not an official API. Or you can better use typing_inspect.get_generic_type(obj) which is a thin wrapper around this API.

@mitar
Copy link
Contributor Author

mitar commented Dec 16, 2017

Oh, thank you for quick response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants