Skip to content

implement from __future__ import annotations - #6117

Merged
dhalbert merged 3 commits into
adafruit:mainfrom
dhalbert:__future__
Mar 8, 2022
Merged

dhalbert merged 3 commits into
adafruit:mainfrom
dhalbert:__future__

Conversation

@dhalbert

@dhalbert dhalbert commented Mar 4, 2022 •

Copy link
Copy Markdown
Collaborator

For type annotation purposes, we would like to write code that uses:

from __future__ import annotations

This from __future__ is part of PEP 563, which allows postponed evaluation of annotations, so that forward references work without stringifying the annotations. It is available in CPython 3.7 and up, and a lot of CPython .py code uses it. We would like to use it in code that can be used in both CPython and CircuitPython.

MicroPython (and CircuitPython) already effectively implement PEP 563 in a no-op way by totally ignoring all annotations. However, MicroPython does not provide a from __future__ import ... at all. This PR implements a no-op __future__ module with a single member annotations. In CPython, members of __future__ are _Feature objects, with version information. In this PR, annotations is just True.

This adds 100 bytes to the Trinket M0 build and presumably other builds. I think this is worth it to allow compatible code.

Note that we cannot do this workaround:

try:
    from __future__ import annotations
except ImportError:
    pass

because in CPython, a from __future__ must be absolutely the first statement in a source file. We don't check this restriction, and I don't think it's necessary.

@dhalbert
dhalbert requested a review from jepler March 4, 2022 19:19
tannewt
tannewt previously requested changes Mar 5, 2022

@tannewt tannewt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! One fix for the doc build.

Comment thread shared-bindings/__future__/__init__.c
@jepler

jepler commented Mar 7, 2022

Copy link
Copy Markdown

Is fine by me, once the CI is content.

@dhalbert
dhalbert requested a review from tannewt March 8, 2022 14:23
@dhalbert

dhalbert commented Mar 8, 2022

Copy link
Copy Markdown
Collaborator Author

@jepler ready to re-review, thanks.

@jepler jepler left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dhalbert
dhalbert merged commit 05a4d24 into adafruit:main Mar 8, 2022
@dhalbert dhalbert added this to the 7.2.x milestone Mar 8, 2022
@dhalbert
dhalbert deleted the __future__ branch March 8, 2022 17:30
@dhalbert

dhalbert commented Mar 8, 2022

Copy link
Copy Markdown
Collaborator Author

Consider backporting to 7.2.x.

dhalbert added a commit that referenced this pull request Mar 11, 2022
Backport from __future__ import annotations from #6117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants