Skip to content
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

Use __slots__ as a shorthand for NamedTuple? #1211

Closed
gvanrossum opened this issue Feb 9, 2016 · 3 comments
Closed

Use __slots__ as a shorthand for NamedTuple? #1211

gvanrossum opened this issue Feb 9, 2016 · 3 comments

Comments

@gvanrossum
Copy link
Member

We found an internal use case where we want to combine NamedTuple with another base class. The idea is that the other base class implements some functionality on top of NamedTuple. But since NamedTuple is a type-factory function you can't subclass it, and mypy doesn't support other functions that wrap it either (let alone a metaclass to do so). Maybe it would be possible to arrange things so that this:

class C(B):
    __slots__ = {'a': int, 'b': str}
    ...

is more or less equivalent to this?

C1 = NamedTuple('C1', [('a': int, 'b': str)])
class C(B, C1):
    ...

I realize this doesn't quite work because the dict doesn't have ordering, but maybe there's still something we could do here.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 9, 2016

It would not be quite equivalent to NamedTuple because the class wouldn't be tuple-like. However, it wouldn't be hard to teach mypy to infer attributes from __slots__. If mypy had a type system plugin system this could be a good candidate for writing an internal plugin, as the dictionary in __slots__ idiom is non-standard.

(Note also that mypy supports having NamedTuple(...) as an immediate base class.)

@gvanrossum
Copy link
Member Author

Also see #1240.

@ddfisher ddfisher added this to the Future milestone Mar 1, 2016
@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@hauntsaninja
Copy link
Collaborator

This never seemed to go anywhere / mypy has plugins now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants