[issue43646] ForwardRef name conflict during evaluation

Christodoulos Tsoulloftas report at bugs.python.org
Sun Mar 28 05:52:49 EDT 2021


New submission from Christodoulos Tsoulloftas <chris at komposta.net>:

Consider two modules with the same name forward references with the same type construct


./a.py


```
from typing import Optional


class Root:
    a: Optional["Person"]


class Person:
    value: str

```


./b.py

```
from typing import Optional


class Root:
    b: Optional["Person"]


class Person:
    value: str

```

There is a naming conflict, I think due to caching, and the type hint of the second property points to the first one.


```
>>> from typing import get_type_hints, Optional
>>> from a import Root as RootA, Person as PersonA
>>> from b import Root as RootB, Person as PersonB
>>> 
>>> roota_hints = get_type_hints(RootA)
>>> rootb_hints = get_type_hints(RootB)
>>> 
>>> print(roota_hints)
{'a': typing.Optional[a.Person]}
>>> print(rootb_hints)
{'b': typing.Optional[a.Person]}
>>> 
>>> assert roota_hints["a"] == Optional[PersonA]
>>> assert rootb_hints["b"] == Optional[PersonB]  # fails, points to PersonA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>", line 1, in <module>
AssertionError
>>> 

```


The behavior started in python 3.10, I am not sure which alpha version, I am using 3.10.0a6+

----------
components: Library (Lib)
messages: 389634
nosy: tefra
priority: normal
severity: normal
status: open
title: ForwardRef name conflict during evaluation
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43646>
_______________________________________


More information about the Python-bugs-list mailing list