◐ Shell
clean mode source ↗

Message 404740 - Python tracker

I had some time to debug this. It happens because we treat classes and functions differently.

The difference between `get_type_hints(B)` and `get_type_hints(B.__init__)` is that we use different `global` contexts there:
- For `B` we use `__mro__` entries and extract `globals` and `locals` from there: https://github.com/python/cpython/blob/86dfb55d2e091cf633dbd7aabcd49d96fb1f9d81/Lib/typing.py#L1792-L1796
- For `B.__init__` we use simplier logic https://github.com/python/cpython/blob/86dfb55d2e091cf633dbd7aabcd49d96fb1f9d81/Lib/typing.py#L1822-L1828 It does not know anything about `__mro__` and super contexts

Funny thing, this problem goes away if we remove `@dataclass` decorator and convert our examples into regular classes:

```
# a.py
from __future__ import annotations

import collections


class A:
  x: collections.OrderedDict
  def __init__(self, x: collections.OrderedDict) -> None:
      ...
```

and

```
# b.py
from __future__ import annotations

import a
import typing

class B(a.A):
  pass

print(typing.get_type_hints(B))  
# {'x': <class 'collections.OrderedDict'>}
print(typing.get_type_hints(B.__init__))
# {'x': <class 'collections.OrderedDict'>, 'return': <class 'NoneType'>}
```

I am going to try to solve this with something really simple (if no one else is working on it).