Annotations, Inheritance and circular reference
reob-info opened this issue · 4 comments
Basically, I have:
class Base:
_sub: list[Sub]
class Sub:
_parent: Base
What creates a circular reference.
It could be solved doing:
class Sub:
pass
class Base:
_sub: list[Sub]
class Sub:
_parent: Base
But in the annotation, I will get the class Sub without his annotations, methods and properties (the first definition is not updated in annotation after the redefinition).
Then, I suggest one of two possible solutions:
- Add a way to forward declaration (it could be implemented by a new reserved word "forward")
- Or, like Ruby, make that the redefinition of a class, open the class to change the definition of class, including new methods, properties and annotations.
It is very important to my system to have the correct type in annotation, because I use this type to create new objects dinamically , but actually is impossible to solve this problem because I can not have the correct definition at both classes: Base and Sub class.
When I use the type annotated to create a new object, as it is, I loose the annotations, methods and properties defined at the bottom definition.
This problem occurs only if we use annotations.
Like Python added the possibility to define annotations, needs to provide one of the solutions pointed (or another) to solve problems like this.
If the decision was to implement a forward reserved word, it could be solved doing:
class Sub:
forward
class Base:
_sub: list[Sub]
class Sub:
_parent: Base
If the second solution was preferred this code will work:
class Sub:
pass
class Base:
_sub: list[Sub]
class Sub:
_parent: Base
I hope Python could implement one of this solutions to provide a better support for annotations, because actually what we have are two classes, with the same name, that walks like a duck, swim like a duck, sound like a duck but they are not the same duck, and they should be.
Thanks.
The framework that we are developping depends strongly that I could get the correct type from annotation to create the correct object based on this.
If the annotation was updated pointing to the redefinition of the class, it will be enough.
Maybe this could be a third possibility to solve the problem.
I hope it could be handled as soon as possible, because change the definition to another class that was a super class of both classes (Base and Sub, in this example) will not permit us to know the correct class to create another object dinamically and probabily we will need to create a hack to handle this, making the code less Pythonic.
This is the incorrect tracker for bugs in python.
You should search for "python type annotation forward reference". For example, use list["Sub"] ("Sub" as a string) and then call typing.get_type_hints(), to retrieve the actual type.
This is the incorrect tracker for bugs in python.
You should search for "python type annotation forward reference". For example, use
list["Sub"]("Sub" as a string) and then calltyping.get_type_hints(), to retrieve the actual type.
Thanks. I will try the solution that you suggested.
PS: For future reference: Where is the correct tracker for bugs in Python?
Where I should post?
I thought that was here because the URL (github.com/python/bugs.python.org) that says exactly it (bugs.pyhton.org), but I am available to send it to the correct address, if you can say me where.
Thank you again.
@reob-info the current issue tracker for Python is at https://bugs.python.org