This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pekka.klarck
Recipients gvanrossum, levkivskyi, pekka.klarck
Date 2018-09-05.14:58:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1536159492.14.0.56676864532.issue34568@psf.upfronthosting.co.za>
In-reply-to
Content
While studying the types in the typing module more, I noticed they have a special `__origin__` attribute which seems to contain the "real" type they represent. I was able to make my type conversion code to work by adding these lines:

    if hasattr(type_, '__origin__'):
        type_ = type_.__origin__

All our tests pass with this simple fix, but I'm slightly worried using it because `__origin__` doesn't seem to be documented. This means I'm not sure is my usage OK and, more importantly, makes me worried that another change in typing changes the behavior or removes the attribute altogether. Hopefully someone with more insight on this can comment my worries. Perhaps the attribute should also be documented as discussed earlier: https://github.com/python/typing/issues/335

I'd also be a little bit happier with the above fix if I could write it like

    if isinstance(type_, typing.SomeBaseType):
        type_ = type_.__origin__

but apparently types in the typing module don't have any public base class. I guess odds that some unrelated class would have `__origin__` defined is small enough that using `hasattr(type_, '__origin__')` is safe.
History
Date User Action Args
2018-09-05 14:58:12pekka.klarcksetrecipients: + pekka.klarck, gvanrossum, levkivskyi
2018-09-05 14:58:12pekka.klarcksetmessageid: <1536159492.14.0.56676864532.issue34568@psf.upfronthosting.co.za>
2018-09-05 14:58:12pekka.klarcklinkissue34568 messages
2018-09-05 14:58:12pekka.klarckcreate