New submission from ReOb <contact.r...@gmail.com>:

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 three possible solutions:

1. Update the reference to class in annotations if the class was redefined
2. Add a way to forward declaration (it could be implemented by a new reserved 
word "forward")
3. 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 have the correct type in annotation, because we use 
this type to create new objects dinamically, but actually is impossible to 
solve this problem because we can not have the correct definition at both 
classes: Base and Sub class.

When we use the type annotated to create a new object, as it is, we loose the 
annotations, methods and properties defined at the redefinition of the class.

This problem occurs only if we use annotations.

Like Python added the possibility to define annotations, needs to provide one 
of these 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 first or third 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 these 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.

The framework that we are developping depends strongly that we could get the 
correct type from annotation to create the correct object based on this.

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.

Thanks.

----------
components: Parser
messages: 394038
nosy: lys.nikolaou, pablogsal, reob-info
priority: normal
severity: normal
status: open
title: Annotations, Inheritance and Circular Reference
type: resource usage
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44192>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to