Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict not subscriptable despite using __future__ typing annotations #89280

Closed
stefanistrate mannequin opened this issue Sep 6, 2021 · 2 comments
Closed

dict not subscriptable despite using __future__ typing annotations #89280

stefanistrate mannequin opened this issue Sep 6, 2021 · 2 comments
Labels
3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@stefanistrate
Copy link
Mannequin

stefanistrate mannequin commented Sep 6, 2021

BPO 45117
Nosy @sweeneyde, @stefanistrate

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-09-07.04:48:42.150>
created_at = <Date 2021-09-06.16:11:44.909>
labels = ['invalid', 'type-bug', '3.8']
title = '`dict` not subscriptable despite using `__future__` typing annotations'
updated_at = <Date 2021-09-07.04:48:42.146>
user = 'https://github.com/stefanistrate'

bugs.python.org fields:

activity = <Date 2021-09-07.04:48:42.146>
actor = 'Dennis Sweeney'
assignee = 'none'
closed = True
closed_date = <Date 2021-09-07.04:48:42.150>
closer = 'Dennis Sweeney'
components = []
creation = <Date 2021-09-06.16:11:44.909>
creator = 'stefanistrate'
dependencies = []
files = []
hgrepos = []
issue_num = 45117
keywords = []
message_count = 2.0
messages = ['401149', '401205']
nosy_count = 2.0
nosy_names = ['Dennis Sweeney', 'stefanistrate']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45117'
versions = ['Python 3.8']

@stefanistrate
Copy link
Mannequin Author

stefanistrate mannequin commented Sep 6, 2021

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 "<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@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}

@stefanistrate stefanistrate mannequin added 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Sep 6, 2021
@sweeneyde
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant