This is an archive of the discontinued Mercurial Phabricator instance.

mercurial: strip function return type annotations (RFC)
AbandonedPublic

Authored by indygreg on Oct 13 2018, 3:29 AM.

Details

Reviewers
baymax
Group Reviewers
hg-reviewers
Summary

THIS IS SUPER HACKY. DO NOT EVEN THINK ABOUT LANDING YET.

Now that we have a custom module importer on Python 2 and that importer
is using Python 3.7's tokenizer, it is possible for us to analyze the
source code token stream and transform it to valid Python 2.7 source
before it is compiled into a code object.

This commit teaches the token stream transformer to recognize the tokens
for a function return type annotation and to strip those tokens from the
token stream. As far as Python 2's code compiler is concerned, the original
source didn't contain a type annotation!

To prove it works, we define a type annotation in the dispatch module.
Python 2 is able to import the module just fine.

This feature means we could start adding type annotations throughout the
code base while still remaining Python 2 compatible.

But why stop at type annotations? We could also rewrite the token stream
so we can leverage other Python 3 only features. This includes "yield from"
super() without arguments, class definitions without "(object)", etc. We
could also "invert" our source transformer strategy and make the code base
native Python 3 and source transform to remain compatible with Python 2.

Diff Detail

Repository
rHG Mercurial
Lint
Lint Skipped
Unit
Unit Tests Skipped

Event Timeline

indygreg created this revision.Oct 13 2018, 3:29 AM
baymax requested changes to this revision.Jan 24 2020, 12:32 PM

There seems to have been no activities on this Diff for the past 3 Months.

By policy, we are automatically moving it out of the need-review state.

Please, move it back to need-review without hesitation if this diff should still be discussed.

:baymax:need-review-idle:

This revision now requires changes to proceed.Jan 24 2020, 12:32 PM
indygreg abandoned this revision.Jan 24 2020, 11:52 PM