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

implement from __future__ import annotations #6117

Merged
merged 3 commits into from
Mar 8, 2022

Conversation

dhalbert
Copy link
Collaborator

@dhalbert dhalbert commented Mar 4, 2022

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
Copy link
Member

@tannewt tannewt left a comment

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.

shared-bindings/__future__/__init__.c Show resolved Hide resolved
@jepler
Copy link
Member

jepler commented Mar 7, 2022

Is fine by me, once the CI is content.

@dhalbert dhalbert requested a review from tannewt March 8, 2022 14:23
@dhalbert
Copy link
Collaborator Author

dhalbert commented Mar 8, 2022

@jepler ready to re-review, thanks.

Copy link
Member

@jepler jepler left a comment

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
Copy link
Collaborator Author

dhalbert commented Mar 8, 2022

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