Skip to content

py/parse: Recognise const int assignments with type hints. #17643

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

agatti
Copy link
Contributor

@agatti agatti commented Jul 8, 2025

Summary

This PR extends the parser's behaviour to also recognise constant integer assignments even when the assignment itself has an "int" type annotation.

Now declarations like "var: int = const(val)" can be treated as constants by the compiler and thus be folded in the rest of the program as it sees fit. Before this change, that line would generate a variable creation and its value assignment, without any folding being performed.

This fixes #15608.

Testing

This was tested using the same verification methodology described in #15608, and the Unix test suite was ran locally without errors with this change applied.

Trade-offs and Alternatives

I've limited the type checking to int types, which I hope it's all that's needed. It can probably be improved style-wise, but from what I reckon the logic should be correct - I haven't looked at the parser since the rv32 inline asm days.

@agatti agatti changed the title py/parse: Generate const int variables for annotated assignments. py/parse: Recognise const int assignments with type hints. Jul 8, 2025
Copy link

github-actions bot commented Jul 9, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +80 +0.009% standard
      stm32:   +88 +0.022% PYBV10
     mimxrt:   +80 +0.021% TEENSY40
        rp2:   +88 +0.010% RPI_PICO_W
       samd:   +88 +0.033% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +90 +0.020% VIRT_RV32

Copy link

codecov bot commented Jul 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.23%. Comparing base (05342b0) to head (da81718).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17643   +/-   ##
=======================================
  Coverage   98.23%   98.23%           
=======================================
  Files         171      171           
  Lines       22140    22149    +9     
=======================================
+ Hits        21749    21758    +9     
  Misses        391      391           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This commit extends the parser's behaviour to also recognise constant
integer assignments even when the assignment itself has an "int" type
annotation.

Now declarations like "var: int = const(val)" can be treated as
constants by the compiler and thus be folded in the rest of the program
as it sees fit.  Before this change, that line would generate a variable
creation and its value assignment, without any folding being performed.

This fixes micropython#15608.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@agatti
Copy link
Contributor Author

agatti commented Jul 9, 2025

Forgot to add tests... thank you, codecov! I've also had the chance to reword the commit message as it wasn't entirely clear. I'll push the lot once the current test run is over to avoid having to receive loads of cancelled run reports.

Anyway, I wonder how difficult it would be to have something like mpy-tool.py's disassembler available in the tests' running environment. This way the tests could also verify that, for a given bit of code, optimised bytecode was emitted instead of regular code sequences - that would make some sense for this specific PR at least.

@agatti agatti force-pushed the annotated-const branch from 9f923a5 to da81718 Compare July 9, 2025 00:21
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Const with type hints is not optimized
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy