Open TiborVoelcker opened 4 weeks ago
This is behaving as it should; it returns the original bases of the class it is called on, not its base classes. Perhaps we should improve the documentation to make it clear that the function is not necessarily completely equivalent to accessing the attribute.
Accessing the attribute directly is also likely to cause incorrect behavior when metaclasses are involved.
Is there a way to get the generic type from the Three
object in my example then?
The __orig_bases__
seems to be exactly what I want, but its use seems to be not really documented/discouraged. Also, the issue with the type checkers remains.
I don't know what your exact use case is, but the general answer would be to iterate over the bases of the class you're interested in and call get_original_bases()
on each base class. Accessing the attribute directly may do what you want in simple cases, but likely returns wrong results in cases like multiple inheritance or custom metaclasses.
Iterating over the bases would work, yes. Thank you.
Also, now I finally get what you meant with "the original bases of the class it is called on, not its base classes".
get_original_bases()
is meant to operate only on the direct bases (parents) of the supplied class. If it does not contain a generic, there is nothing to do, so it just returns the normal bases.
I was confused as I somehow thought that "bases" means "the last parent" / "the end of the class mro".
Reading the docs now, I see that this is specifically said.
I would close this issue, but maybe keep it open for the documentation clarification?
Bug report
Bug description:
The types documentation states for the
types.get_original_bases
function:I need the functionality of
cls.__orig_bases__
, but need to usetypes.get_original_bases
to make the type checker happy.A short MRE:
This is a easy solve, I would be happy to add a PR. Change here:
To show this works:
CPython versions tested on:
3.12
Operating systems tested on:
Windows