[issue45117] `dict` not subscriptable despite using `__future__` typing annotations

2021-09-06 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

Hi Stefan,

`from __future__ import annotations` only affects annotations -- just the 
things after the colon. It makes it so that annotations are never evaluated, so 
things like this work:

>>> from __future__ import annotations
>>> x: nonsense()()()()[other_nonsense](1<2>3)

The __future__ import is not a wholesale opt-in-to-all-new-typing-features, 
it's just an opt-in-to-not-evaluate-annotations.

dict.__class_getitem__ (which is what gets called when you type dict[str, Any]) 
was not added at all until Python 3.9 (GH-18239), so if you want to *evaluate* 
such expressions, you have to upgrade to 3.9+. In 3.8, use typing.Dict instead 
-- 3.8 is no longer accepting new features.

Thanks for the report, but I'm closing this for now.

--
nosy: +Dennis Sweeney
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45117] `dict` not subscriptable despite using `__future__` typing annotations

2021-09-06 Thread Ștefan Istrate

New submission from Ștefan Istrate :

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@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 "", line 1, in 
TypeError: 'type' object is not subscriptable



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



$ /usr/local/Cellar/python@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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com