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

get_origin difference between Python 3.6 and Python 3.7 #34

Open
mitar opened this issue Apr 7, 2019 · 6 comments
Open

get_origin difference between Python 3.6 and Python 3.7 #34

mitar opened this issue Apr 7, 2019 · 6 comments

Comments

@mitar
Copy link

mitar commented Apr 7, 2019

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.

@ilevkivskyi
Copy link
Owner

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?

@mitar
Copy link
Author

mitar commented Apr 8, 2019

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

@septatrix
Copy link

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

@mitar
Copy link
Author

mitar commented Aug 4, 2020

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.

@ilevkivskyi
Copy link
Owner

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.

@jph00
Copy link

jph00 commented Oct 21, 2020

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 :)

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

4 participants