Message324642
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. |
|
Date |
User |
Action |
Args |
2018-09-05 14:58:12 | pekka.klarck | set | recipients:
+ pekka.klarck, gvanrossum, levkivskyi |
2018-09-05 14:58:12 | pekka.klarck | set | messageid: <1536159492.14.0.56676864532.issue34568@psf.upfronthosting.co.za> |
2018-09-05 14:58:12 | pekka.klarck | link | issue34568 messages |
2018-09-05 14:58:12 | pekka.klarck | create | |
|