Skip to content

py/objtype: Add basic __init_subclass__ metaclass support. #15511

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

Closed
wants to merge 12 commits into from

Conversation

AJMansfield
Copy link
Contributor

@AJMansfield AJMansfield commented Jul 20, 2024

Summary

This PR adds partial support for the __init_subclass__ data model method defined in PEP487, and gates this added features behind a new feature flag, MICROPY_PY_METACLASSES.
While this PR does not implement "full" metaclasses, it does implement the main mechanism used in practice for most types of meta-class-like class customization.

The behavior implemented here varies from the CPython behavior for two reasons:

  • Due to tests/cpydiff/core_class: Document issue with super in classmethod. #15509, user-written __init_subclass__ implementations are not able to recursively call super().__init_subclass__() the way PEP487 specifies. Therefore, this implementation applies a workaround: instead of invoking __init_subclass__ only for the last base class, it invokes it on all direct base classes.
  • Micropython does not support passing kwargs as bases of a class, therefore __init_subclass__ currently has no kwargs functionality.

This PR uses #15503 as its base to ensure the __init_subclass__ code remains in the correct order with __set_name__ in order to match CPython. It also adds MICROPY_PY_METACLASSES as an alternative condition for enabling #15503's functionality, for the rare cases of metaclass code that relies on __set_name__ class customization but doesn't need the per-lookup overhead of descriptors.

Testing

This PR includes a test verifying this feature, and three cpydiff's precisely outlining the mismatch with CPython. I've also manually verified the functionality using the unix port.

Trade-offs and Alternatives

This increases code size, but only when the corresponding feature flag is enabled.

Copy link

codecov bot commented Jul 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.43%. Comparing base (e1fe62f) to head (8cf652d).
Report is 639 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15511   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files         161      161           
  Lines       21281    21302   +21     
=======================================
+ Hits        20948    20969   +21     
  Misses        333      333           

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

Copy link

github-actions bot commented Jul 20, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +536 +0.064% standard[incl +64(data)]
      stm32:  +236 +0.060% PYBV10
     mimxrt:  +248 +0.068% TEENSY40
        rp2:  +240 +0.027% RPI_PICO_W
       samd:  +260 +0.098% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 22, 2024
@dpgeorge
Copy link
Member

Thanks for this, it looks like a pretty useful feature (I've never used it myself though in CPython).

Especially if MicroPython will never support full CPython metaclasses, this is a good (and more modern) approach to it.

@AJMansfield AJMansfield marked this pull request as draft July 24, 2024 20:25
@AJMansfield
Copy link
Contributor Author

I'm currently looking to see how to address the issue with the init_subclass recursion now given #15533.

@dpgeorge
Copy link
Member

I'm currently looking to see how to address the issue with the init_subclass recursion now given #15533.

Oh, that's interesting, that the fix in 15533 breaks this PR.

I think 15533 is necessary and should be merged. It's a very simple fix and fixes an edge case that's very difficult to notice in a running program, let alone debug and work around.

@AJMansfield
Copy link
Contributor Author

AJMansfield commented Jul 25, 2024

Oh, that's interesting, that the fix in 15533 breaks this PR.

No, mostly it just makes one of the behavioral compromises in the original version of this PR obsolete and unnecessary -- since it was possible to fix that fairly quickly, merging the original half-measure is unnecessary.

For that matter, it actually fixes cpydiff/core_class_initsubclass_recursive, so I'll be moving it back to tests -- though frustratingly, it wasn't quite enough to fix cpydiff/core_class_initsubclass_multi the way I'd originally hoped, even if I think I understand why now.

I think the fix for that will be tweaking to super_attr to special-case __init_subclass__ lookups to default to a no-op function instead of failing. (Alternatively, we could just add a default __init_subclass__ implementation to base object; I just think it's better to limit the scope of the performance impact and tweak super instead.)

@dpgeorge
Copy link
Member

For that matter, it actually fixes cpydiff/core_class_initsubclass_recursive, so I'll be moving it back to tests

Very good! Then merging #15533 is the right way to move forward.

though frustratingly, it wasn't quite enough to fix cpydiff/core_class_initsubclass_multi the way I'd originally hoped, even if I think I understand why now.

I think the fix for that will be tweaking to super_attr to special-case __init_subclass__ lookups to default to a no-op function instead of failing. (Alternatively, we could just add a default __init_subclass__ implementation to base object; I just think it's better to limit the scope of the performance impact and tweak super instead.)

Hmm, I'm not sure at this stage what the best option is... do what you think is best then we can review it.

Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
@AJMansfield
Copy link
Contributor Author

AJMansfield commented Feb 21, 2025

Retracting in favor of further discussion and consensus about how to implement this part of PEP 487.

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.

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