typing_inspect icon indicating copy to clipboard operation
typing_inspect copied to clipboard

get_origin difference between Python 3.6 and Python 3.7

Open mitar opened this issue 6 years ago • 6 comments
trafficstars

I was hoping that using this package would make it easier to migrate between Python versions as it changes typing implementation. So I started using it with Python 3.6 but now that I tried to use working code on Python 3.6 with Python 3.7 it seems there are differences. For example, the following code:

import typing
import typing_inspect

A = typing.TypeVar('A')
B = typing.TypeVar('B')
C = typing.TypeVar('C')

class Base(typing.Generic[A, B]):
    pass

class Foo(Base[A, None]):
    pass

class Bar(Foo[A], typing.Generic[A, C]):
    pass

class Baz(Bar[float, int]):
    pass

print("Bar", typing_inspect.get_origin(Bar))
print("Baz", typing_inspect.get_origin(Baz))
print("Base", typing_inspect.get_origin(Base))
print("Base[float, int]", typing_inspect.get_origin(Base[float, int]))
print("Foo", typing_inspect.get_origin(Foo))
print("Foo[float]", typing_inspect.get_origin(Foo[float]))

In Python 3.6 outputs:

Bar __main__.Bar
Baz __main__.Baz
Base __main__.Base
Base[float, int] __main__.Base
Foo __main__.Foo
Foo[float] __main__.Foo

While in Python 3.7:

Bar None
Baz None
Base None
Base[float, int] <class '__main__.Base'>
Foo None
Foo[float] <class '__main__.Foo'>

I think ideally they should behave the same.

mitar avatar Apr 07 '19 19:04 mitar

Hm, this looks like a bug. I think for bare class objects get_origin() should always return the class object itself. Would you like to submit a PR?

ilevkivskyi avatar Apr 08 '19 10:04 ilevkivskyi

Not sure how to know if it is a bare class?

mitar avatar Apr 08 '19 15:04 mitar

The way it works since 3.7 is at least consistent with typing.get_origin() inside the stdlib which got introduced in 3.8

septatrix avatar Aug 04 '20 14:08 septatrix

Yea, it would be great if then get_origin from this package would work on 3.6 the same as it does on 3.7 and 3.8.

mitar avatar Aug 04 '20 20:08 mitar

Yea, it would be great if then get_origin from this package would work on 3.6 the same as it does on 3.7 and 3.8.

I was always think about this, but it may be a breaking change, so I'm not 100% sure.

ilevkivskyi avatar Aug 05 '20 07:08 ilevkivskyi

I was always think about this, but it may be a breaking change, so I'm not 100% sure.

I suspect there are a lot more people that aren't able to use this (or need hacky workarounds) because of the inconsistency with 3.6, than there are people relying on the existing behavior (if any). So I think fixing it would be best.

Just my $0.02 :)

jph00 avatar Oct 21 '20 17:10 jph00