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

constant folding affects annotations despite 'from __future__ import annotations' #83401

Closed
cfbolz mannequin opened this issue Jan 5, 2020 · 6 comments
Closed

constant folding affects annotations despite 'from __future__ import annotations' #83401

cfbolz mannequin opened this issue Jan 5, 2020 · 6 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@cfbolz
Copy link
Mannequin

cfbolz mannequin commented Jan 5, 2020

BPO 39220
Nosy @cfbolz, @ambv, @pablogsal
PRs
  • bpo-39220: Do not optimise annotation if 'from __future__ import annotations' is used #17866
  • Files
  • x.py
  • 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 2020-03-18.23:02:35.443>
    created_at = <Date 2020-01-05.13:26:51.895>
    labels = ['interpreter-core', '3.9']
    title = "constant folding affects annotations despite 'from __future__ import annotations'"
    updated_at = <Date 2020-03-18.23:02:39.849>
    user = 'https://github.com/cfbolz'

    bugs.python.org fields:

    activity = <Date 2020-03-18.23:02:39.849>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-03-18.23:02:35.443>
    closer = 'pablogsal'
    components = ['Interpreter Core']
    creation = <Date 2020-01-05.13:26:51.895>
    creator = 'Carl.Friedrich.Bolz'
    dependencies = []
    files = ['48827']
    hgrepos = []
    issue_num = 39220
    keywords = ['patch']
    message_count = 6.0
    messages = ['359341', '359372', '359373', '359421', '359426', '364576']
    nosy_count = 3.0
    nosy_names = ['Carl.Friedrich.Bolz', 'lukasz.langa', 'pablogsal']
    pr_nums = ['17866']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue39220'
    versions = ['Python 3.9']

    @cfbolz
    Copy link
    Mannequin Author

    cfbolz mannequin commented Jan 5, 2020

    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'}
    

    @cfbolz cfbolz mannequin added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jan 5, 2020
    @pablogsal
    Copy link
    Member

    Łukasz, should we disable AST optimizations in annotations if "from __future__ import annotations" is used?

    @pablogsal
    Copy link
    Member

    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.

    @cfbolz
    Copy link
    Mannequin Author

    cfbolz mannequin commented Jan 6, 2020

    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.

    @pablogsal
    Copy link
    Member

    > 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 :)

    @pablogsal
    Copy link
    Member

    New changeset d112c60 by Pablo Galindo in branch 'master':
    bpo-39220: Do not optimise annotation if 'from __future__ import annotations' is used (GH-17866)
    d112c60

    @pablogsal pablogsal added 3.9 only security fixes and removed 3.7 (EOL) end of life labels Mar 18, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant