[New-bugs-announce] [issue45117] `dict` not subscriptable despite using `__future__` typing annotations

Ștefan Istrate report at bugs.python.org
Mon Sep 6 12:11:44 EDT 2021


New submission from Ștefan Istrate <stefan.istrate at gmail.com>:

According to PEP 585 (https://www.python.org/dev/peps/pep-0585/#implementation), I expect that typing aliases could simply use `dict` instead of `typing.Dict`. This works without problems in Python 3.9, but in Python 3.8, despite using `from __future__ import annotations`, I get the following error:



$ /usr/local/Cellar/python at 3.8/3.8.11/bin/python3.8
Python 3.8.11 (default, Jun 29 2021, 03:08:07)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> from typing import Any
>>> JsonDictType = dict[str, Any]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable



However, `dict` is subscriptable when not used in an alias:



$ /usr/local/Cellar/python at 3.8/3.8.11/bin/python3.8
Python 3.8.11 (default, Jun 29 2021, 03:08:07)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import annotations
>>> from typing import Any
>>> def f(d: dict[str, Any]):
...     print(d)
...
>>> f({"abc": 1, "def": 2})
{'abc': 1, 'def': 2}

----------
messages: 401149
nosy: stefanistrate
priority: normal
severity: normal
status: open
title: `dict` not subscriptable despite using `__future__` typing annotations
type: behavior
versions: Python 3.8

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


More information about the New-bugs-announce mailing list