-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
py/objtype: Add type.__bases__ attribute. #5139
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
Conversation
c539a7f
to
0055b73
Compare
Sorry about the force push there, fixed a bad pointer NULL comparison (which should fix the preexisting tests). Didn't wish to introduce another commit for that, though it might have been cleaner and more obvious. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong feelings about whether MICROPY_CPYTHON_COMPAT
is the right flag to gate this feature. It's on by default, and only disabled in the really minimal ports. The whole feature costs 84 (or maybe 72, see comment) bytes on STM32 (e.g. PYBV11).
Based on your comment, is it a feature you'd only want for unit testing? In which case perhaps only the Unix port needs it (or specifically your board config).
This is actually a feature I'm using on the microcontroller since I don't have different code generation between tests and application at this stage. |
Code size changes:
I tried |
3cfd9ac
to
33cccab
Compare
Squish squash. |
f12dadf
to
606f5bd
Compare
Squash tests. |
606f5bd
to
13ec0ba
Compare
It looks like One way to fix this would be to modify all built-in types (eg str, bytes, dict, etc) so that they explicitly have |
You are correct, I didn't consider this case. I'll make some tests for it and go about fixing it. Can't say I'll get it done this weekend but I will look in to it. |
Another way would be to special case object to have an empty tuple
This is a good motivation to go with the longer route but do we want to include that kind of change in this patch? |
13ec0ba
to
6a90a05
Compare
Alright - I've branched this pull request in to two streams, this is the simplist approach, treat I'm now exploring the other route, redefining what it meants for |
Keeping track of branches here for review purposes. This pull request: https://github.com/nevercast/micropython/tree/proposal/bases The simplist stream: https://github.com/nevercast/micropython/tree/proposal/bases_special This pull request before branching https://github.com/nevercast/micropython/tree/proposal/bases_clean |
6a90a05
to
14ad0ed
Compare
Pushed fix for bad compare between pointers. |
Just while I'm doing this, I notice it starts to get to be a pretty bloated change.
A lot of types to add default Should I go and make all the built-in types like CPython ? I've started writing tests checking that all the builtins .bases match, but I'm wondering if we should default to |
I do like that idea, it would eliminate the special test for
Yes indeed it does...
Yes I think that's the best way forward at this point, to just make small improvements. So, as it currently stands I think this PR is good to go in. Later on we can improve/optimise things by explicitly populating the |
I'm happy for this PR to land in |
14ad0ed
to
f69490a
Compare
One last squash to drop the extra commit message. |
@@ -1014,6 +1014,21 @@ STATIC void type_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { | |||
dest[0] = MP_OBJ_NEW_QSTR(self->name); | |||
return; | |||
} | |||
if (attr == MP_QSTR___bases__) { | |||
mp_obj_t parent_obj = self->parent ? MP_OBJ_FROM_PTR(self->parent) : MP_OBJ_FROM_PTR(&mp_type_object); | |||
if (self == &mp_type_object) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two blocks can be swapped. I'll do that now.
f69490a
to
c664908
Compare
c664908
to
c6c6918
Compare
Thanks, merged in 8f9e2e3 with a minor change to the test to skip if the feature is not enabled. |
Whoops. Thank you! |
To help with this in the future see #5227 |
Move OneWire to `onewireio`
This addendum to micropython#5139 allows actually turning off onewireio. (Not currently used by any board.)
Proposal: Add
__bases__
attribute to types so that the inheritance heirachy can be inspected. Related to #5106 and #4368I have not implemented
.__base__
nor have I made any changes to anythingMRO
related. This is simply a read-only attribute that supports single and multiple inheritance.For me, the intent of this addition is so I can type check objects in my application to ensure they inherit from a common base class.
It should be noted that I would like to add documentation and tests for this, once we finalize the draft. I'm not proposing we ignore housekeeping.
Example: