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
NamedTuple
can't inherit from another class
#116241
Comments
We previously discussed this as something some users might want in #88089 (comment). At that point in time, we decided not to allow multiple inheritance with arbitrary classes, just with
If there's a need for using mixin classes with |
One immediate issue I see is what to do about |
Thanks for the answer, I understand. I don't really have answer on the various points you raised. But I can provide more context on my use-case. In our codebase, we're defining some protocol, to handle saving various object types (Dataset, Model,...): class Checkpointable(abc.ABC):
@abc.abstractmethod
def __kd_restore__(self, x):
...
class Dataset(Checkpointable):
...
class State(Checkpointable):
...
def restore[T: Checkpointable](path: PathLike, obj: T) -> T:
... To save multiple object at once, I was trying to add some wrapper: class TopLevel(typing.NamedTuple, Checkpointable):
state: State
ds: Dataset The reason I was trying to use named state, ds = restore('/tmp/', TopLevel(state, ds)) vs out = restore('/tmp/', TopLevel(state, ds))
state = out.state
ds = out.ds My actual use-case is more complicated with more indirections, variables. But that's the main idea |
I updated #31781. It is just removing 4 lines of code which forbid multiple inheritance. |
The currently working workaround is to use an immediate class: class TopLevel(collections.namedtuple('TopLevel', ('state', 'ds')), Checkpointable):
pass or class TopLevel(typing.NamedTuple):
state: State
ds: Dataset
class TopLevel(TopLevel, Checkpointable):
pass Is such complication necessary or we can omit an immediate step? The issue with |
Feature or enhancement
Proposal:
Currently this code is failing:
Raising:
But this code is not:
I believe those 2 snippets are mostly equivalent (the final
B
class behave the same), so it's confusing and force boilerplate thatclass B(A, typing.NamedTuple):
fail.Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: