implement from __future__ import annotations
#6117
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For type annotation purposes, we would like to write code that uses:
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 memberannotations
. In CPython, members of__future__
are_Feature
objects, with version information. In this PR,annotations
is justTrue
.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:
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.