-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
xarray.concat for datasets fails when object is a child class of Dataset #6827
Comments
In the example the naming and meaning of the arguments of the Subclassing is often more reliable when it adheres to the Liskov Substitution Principle which states that objects of a superclass should be replaceable with objects of its subclasses and in other words: a subclass should only add functionality and not reduce it. There are some other workarounds though: If all you need is a convenient custom constructor then you could add a Another option is to convert your where your subclass may define as a convenience method:
|
Thank you for the answer. I'll look into implementing one of your suggestions (they're much better than what I had in mind). I still don't understand the change that causes the error, though. If you want to instantiate a Dataset object, which you clearly do according to the args you pass in, why did that line have to be changed from explicitly calling Dataset? (By "you" I don't mean you personally, unless you actually made the change :) ) |
Because we want to get out the same type of Dataset (subclassed or not) as we input. That's what the T_Dataset type implies. And since we forced a Dataset at the end that wasn't the case. |
Shall we close? It seems like the current code is what we want. |
I'll leave that with you guys to decide. I've reported it, it's been looked at, and it has a record of it. So if the code is as you intended, I don't mind if the issue is closed. |
@gabicca given that we do say in our docs that we don't recommend subclassing xarray objects, I'm going to close this issue as a downstream "subclass at your own risk" problem. If you would like it to be easier to subclass, we would welcome your input here. |
What happened?
The following error is thrown:
This is coming from the change merged as part of this PR:
https://github.com/pydata/xarray/pull/6784/files (line 593)
When "datasets" is a list of class objects which are the child of Dataset,
type(datasets[0])
will return that class as the type. This class however, doesn't necessary hasattrs
in its own init, hence the code breaks.I'm not convinced that this is a correct behaviour, and if you want to initialize the Dataset class, you should revert that line of change.
What did you expect to happen?
Code to run without any errors.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment
xarray==2022.6.0
python: 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:27:05)
The text was updated successfully, but these errors were encountered: