This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: constant folding affects annotations despite 'from __future__ import annotations'
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Carl.Friedrich.Bolz, lukasz.langa, pablogsal
Priority: normal Keywords: patch

Created on 2020-01-05 13:26 by Carl.Friedrich.Bolz, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
x.py Carl.Friedrich.Bolz, 2020-01-05 13:26
Pull Requests
URL Status Linked Edit
PR 17866 merged pablogsal, 2020-01-06 13:56
Messages (6)
msg359341 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2020-01-05 13:26
PEP 563 interacts in weird ways with constant folding. running the following code:

```
from __future__ import annotations

def f(a: 5 + 7) -> a ** 39:
    return 12

print(f.__annotations__)
```

I would expect this output:

```
{'a': '5 + 7', 'return': 'a ** 39'}
```

But I get:


```
{'a': '12', 'return': 'a ** 39'}
```
msg359372 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-05 18:59
Łukasz, should we disable AST optimizations in annotations if "from __future__ import annotations" is used?
msg359373 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-05 19:04
Yo be honest, this would add a lot of complexity as "from __future__ import annotations" is a compiler directive (CO_FUTURE_ANNOTATIONS) and bringing this to the AST level would be somehow disrupting. 

I would be -1 to disable AST optimizations in annotations.
msg359421 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2020-01-06 12:57
I don't have a particularly deep opinion on what should be done, just a bit of weirdness I hit upon while implementing the PEP in PyPy. fwiw, we implement it as an AST transformer that the compiler runs before running the optimizer to make sure that the AST optimizations don't get applied to annotions. The transformer replaces all annotations with a Constant ast node, containing the unparsed string.
msg359426 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-06 13:57
>> I don't have a particularly deep opinion on what should be done, just a bit of weirdness I hit upon while implementing the PEP in PyPy. fwiw, we implement it as an AST transformer that the compiler runs before running the optimizer to make sure that the AST optimizations don't get applied to annotions. The transformer replaces all annotations with a Constant ast node, containing the unparsed string.


I have given it a try in PR 17866 and it was not as invasive as I imagine, so if Łukasz agrees, we can go ahead :)
msg364576 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-03-18 23:02
New changeset d112c600ab3f5e2910345bcc4bfc39439917ed87 by Pablo Galindo in branch 'master':
bpo-39220: Do not optimise annotation if 'from __future__ import annotations' is used (GH-17866)
https://github.com/python/cpython/commit/d112c600ab3f5e2910345bcc4bfc39439917ed87
History
Date User Action Args
2022-04-11 14:59:24adminsetgithub: 83401
2020-03-18 23:02:39pablogsalsetversions: + Python 3.9, - Python 3.7
2020-03-18 23:02:35pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-03-18 23:02:13pablogsalsetmessages: + msg364576
2020-01-06 13:57:56pablogsalsetmessages: + msg359426
2020-01-06 13:56:35pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request17283
2020-01-06 12:57:35Carl.Friedrich.Bolzsetmessages: + msg359421
2020-01-05 19:04:06pablogsalsetmessages: + msg359373
2020-01-05 18:59:41pablogsalsetnosy: + pablogsal, - BTaskaya
messages: + msg359372
2020-01-05 18:54:37BTaskayasetnosy: + lukasz.langa
2020-01-05 18:54:21BTaskayasetnosy: + BTaskaya, - lukasz.langa
2020-01-05 18:54:07pablogsalsetnosy: + lukasz.langa
2020-01-05 13:26:51Carl.Friedrich.Bolzcreate