-
Notifications
You must be signed in to change notification settings - Fork 2
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
Metaclass approach #3
Metaclass approach #3
Conversation
This is an alternative take to get metaclasses mostly working. We continue to use ExtensionArray.__len__ as what EA authors must impelment. We define size and shape in terms of it. We have some limited handling of respecting previously defined shape and size.
Added a deprecation warning for subclasses defining shape or size. Thoughts on how to proceed? Does this seem reasonable enough to merge into your PR branch? |
I'll review this later today. Tentatively looks reasonable. |
elif self._ExtensionArray__expanded_dim == 0: | ||
result = length | ||
else: | ||
result = 1 |
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.
im not sure i follow. is expanded_dim an indicator or is it a patched ndim or something else? Is 1 hard-coded here because we support only (N, 1) and (1, N)?
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.
expanded_dim
is an indicator.
If array._expanded_dim
== 1, that means array.shape
is (1, N)
and len(array)
is 1
.
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 see this is clarified below)
The metaclass part of this makes sense. The changes to the size/len/shape interface I'm not sure about. I've pulled it locally and will poke at it a bit, see if I can break it by writing a really circular EA subclass. It looks like this approach involves telling downstream authors "don't define My inclination is to tell authors "implement If I were to merge this branch, does this reach the zero goal for affect on downstream authors? |
Yes.
Do you think they'll need to implement size at all? Previously, it wasn't part of the EA interface.
In theory, yes. They may have a (non-visible) DepreciationWarning about removing custom |
Just to be clear on this point
|
If in
I get a bunch of recursion errors in extension/array tests. Is there anything in the interface spec that would prevent a downstream author from doing this? |
Hmm, no I don't think so... I'll think about this a bit. |
The reason I landed on |
Fletcher, at least, defines size:
https://github.com/xhochy/fletcher/blob/1b68f0d7dcddf2289824a678a039959354a8cb0a/fletcher/base.py#L483
Though it's not in terms of .shape or __len__
…On Thu, Aug 15, 2019 at 11:11 AM jbrockmendel ***@***.***> wrote:
Do you think they'll need to implement size at all? Previously, it wasn't
part of the EA interface.
The reason I landed on size as what we ask authors to implement is
because that's the one I can't imagine needing to override.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3?email_source=notifications&email_token=AAKAOIQAVCHC7BPWLYU5UNTQEV5ZRA5CNFSM4ILYDXZ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4MH3NQ#issuecomment-521698742>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKAOISAOSF6HQSNTB2U2JDQEV5ZRANCNFSM4ILYDXZQ>
.
|
Yah I don't think the problematic cases are going to be common, but I'm not comfortable ruling them out completely. |
cc @jorisvandenbossche & @jreback Quick summary: This PR to Brock's PR adds a metaclass. Downstream EA authors don't have to do anything assuming that they define So I think we can't be 100% sure we'll not break downstream EA authors with this. It's not hard to come up with cases where shape, size, and len can be defined in terms of each other. But is this a problem in practice?
Are there any others we're aware of? If this isn't actually a problem in practice, then I'm OK with ignoring the downsides. I would like to
WDYT? |
I think the restrictions around the definitions of shape, len, size seem OK (assuming that's the only actual impact on existing EAs). So from that point of view, this seems a good approach. That said, I am personally not a huge fan of a metaclass that so profoundly alters the behaviour of a class. This is rather un-transparent to the developer of an ExtensionArray, IMO (also in pandas). |
Yeah, I'm growing less fond of this approach as I start to appreciate how invasive it is. |
@jreback I'd like to draw your attention back here before long. Recap: The "base" PR is pandas-dev#27142. This is Tom's proof of concept to do the same thing using a metaclass approach. We eventually figured out that the metaclass approach has two problems:
Problem 1) here is going to apply to pretty much any live-patching approach we try to take. I think we need to change the EA interface to:
Yah it would be nice if this were unnecessary, but this is a "rip the band-aid off" situation. |
yeah i briefly reviewed this. I am on-board generally with this approach. As commented on the original PR, this would be more easily grok-able if the override methods were a bit more modularly defined (as free functions), and then simply called (as opposed to being implemented at the call site). |
@jreback the relevant question ATM is not how to patch/override methods, but how to avoid potential circularity: #3 (comment) |
I don't think the But its not really clear to me if you still want to pursue the metaclass approach? For me the bigger issue with this is what I mentioned above in #3 (comment) about complexity / non-transparency of what is happening with your EA. And if I understand Jeff's comment on pandas-dev#27142 (comment) correctly, that is for a non-metaclass approach, with which you see to agree? |
This pull request is stale because it has been open for thirty days with no activity. Please update or respond to this comment if you're still interested in working on this. |
This is an alternative take to get metaclasses mostly working.
We continue to use ExtensionArray.len as what EA authors must
implement. We define size and shape in terms of it. We have some limited
handling of respecting previously defined shape and size. I'm working a bit
to make this more robust.
This removed
ExtensionArray._shape
in favor of a (name mangled) attribute definingwhich dimension is "expanded" (None, 0, or 1). That seemed a bit safer than a
._shape
attribute.
cc @jbrockmendel.