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

Add advanced source transformations to reduce type checking overhead #111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Commits on Jun 17, 2024

  1. Add advanced source transformations to reduce type checking overhead

    The new 'munge' module performs transformations on the source code.
    It uses the AST (abstract syntax tree) representation of Python code
    to recognize some idioms such as `if STATIC_TYPING:` and transforms
    them into alternatives that have zero overhead in mpy-compiled files
    (e.g., `if STATIC_TYPING:` is transformed into `if 0:`, which is eliminated
    at compile time due to mpy-cross constant-propagation and dead branch
    elimination)
    
    The code assumes the input file is black-formatted. In particular, it
    would malfunction if an if-statement and its body are on the same line:
    `if STATIC_TYPING: print("boo")` would be incorrectly munged.
    jepler committed Jun 17, 2024
    Configuration menu
    Copy the full SHA
    3002d23 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    ac0e070 View commit details
    Browse the repository at this point in the history
  3. Add circuitpython-munge script

    this script can either show the munged version of a Python program,
    or show the diff.
    
    Typical output:
    ```diff
    $ circuitpython-munge src/bundle/libraries/helpers/requests/adafruit_requests.py --diff
    --- src/bundle/libraries/helpers/requests/adafruit_requests.py
    +++ src/bundle/libraries/helpers/requests/adafruit_requests.munged.py
    @@ -33,7 +33,7 @@
    
     """
    
    -__version__ = "0.0.0+auto.0"
    +__version__ = "munged-version"
     __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Requests.git"
    
     import errno
    @@ -41,7 +41,7 @@
    
     import json as json_module
    
    -if not sys.implementation.name == "circuitpython":
    +if 0:
         from ssl import SSLContext
         from types import ModuleType, TracebackType
         from typing import Any, Dict, Optional, Tuple, Type, Union
    ```
    jepler committed Jun 17, 2024
    Configuration menu
    Copy the full SHA
    a173e39 View commit details
    Browse the repository at this point in the history