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

Allow adding custom methods to TypedDict #4201

Closed
mitar opened this issue Nov 2, 2017 · 5 comments
Closed

Allow adding custom methods to TypedDict #4201

mitar opened this issue Nov 2, 2017 · 5 comments

Comments

@mitar
Copy link

mitar commented Nov 2, 2017

I would like to extend TypedDict with extra abstract methods into, for example, class MyParams, which would then allow that those who will be extending MyParams, will be able to both specify types of values they want to list as params, but also have to implement abstract methods.

Currently, if I do:

class MyParams(TypedDict):
    @abstractmethod
    def compute(self):
        pass

A subclass like Foo = MyParams('Foo', {'a': int}) fails because it seems MyParams lost its special power to initialize it in this way. If I use class-based syntax, it works, but method compute is still not available on the instance (nor required to be defined, but this is clear problem because metaclass is not abstract metaclass).

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Nov 2, 2017

The initial design has a strong requirement for TypedDicts to be very fast and memory-efficient. This is why at runtime TypedDict instances are just normal dicts:

>>> from mypy_extensions import TypedDict
>>> class TD(TypedDict):
...     x: int
...     y: int
...
>>> td = TD({'x': 1, 'y': 2})
>>> type(td)
<class 'dict'>

therefore, you just can't call any additional methods. I therefore propose to close this, unless we want to re-design it. Maybe we can open an issue at typing tracker to give an informative error if someone tries to add methods to TypedDict.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 2, 2017

Yeah, this is out of scope for TypedDict. TypedDict has been designed for a very specific use case where the runtime representation is just a regular dict object. If you want to define methods, you have to use a real class, unfortunately.

@JukkaL JukkaL closed this as completed Nov 2, 2017
@mitar
Copy link
Author

mitar commented Nov 6, 2017

Hm, but my own class will not have a nature that mypy will know which types are stored under which keys, and to check for totality? Does this mean I will also have to roll out my own mypy plugin to be able to type check such class?

@mitar
Copy link
Author

mitar commented Nov 6, 2017

I mean, ideally, I should be able to specify to TypedDict which base class (extending dict) it is used at runtime, instead of dict. But dict should be the default for sure.

@ilevkivskyi
Copy link
Member

@mitar

Hm, but my own class will not have a nature that mypy will know which types are stored under which keys, and to check for totality? Does this mean I will also have to roll out my own mypy plugin to be able to type check such class?

This is just another request for Intersection and/or something like @apply_protocol discussed in #2087

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

No branches or pull requests

3 participants