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

Generate mypy types #7

Open
jdahlin opened this issue Oct 18, 2020 · 4 comments
Open

Generate mypy types #7

jdahlin opened this issue Oct 18, 2020 · 4 comments

Comments

@jdahlin
Copy link

jdahlin commented Oct 18, 2020

It would be nice to have the option to generate mypy types in addition to TypeScript definitions.

The use case would allow you to pass around typed dictionaries in Python code that is either passed into or returned from a DRF Serializer.

@adenh93
Copy link
Owner

adenh93 commented Oct 18, 2020

Hi @jdahlin,

Sounds interesting, what would be an example of the input and generated output?

Thank you 👍

@jdahlin
Copy link
Author

jdahlin commented Oct 18, 2020

Something like this, completely untested but to give you an idea.

Input:

class QueueTextGenerationItem(serializers.DictField):
    channel_id = serializers.IntegerField()
    language_code = serializers.CharField()
    product_id = serializers.CharField()
    force_regenerate = serializers.BooleanField(required=False)

class QueueTextGenerationRequest(serializers.Serializer):
    generation_requests = serializers.ListField(child=QueueTextGenerationItem())
    text_status = serializers.CharField()

Output:

from typing import List
from typing_extensions import TypedDict

class QueueTextGenerationItem(TypedDict):
    channel_id: int
    language_code: str
    channel_id: str
    force_regenerate: Optional[bool]

class QueueTextGenerationRequest(TypedDict):
    generation_requests: List[QueueTextGenerationItem]
    text_status: str

@adenh93
Copy link
Owner

adenh93 commented Oct 18, 2020

Hi @jdahlin,

Looks simple enough actually, I'll try knock this one at lunch time today (it's ~8:40 AM here).

Thanks for the suggestion! 👍

@adenh93
Copy link
Owner

adenh93 commented Oct 19, 2020

Hi @jdahlin,

I've determined that this might actually be difficult to implement without introducing breaking changes, so I'll do a bit more analysis.

Ideally, I'll add the ability to support a number of different generators (Typescript, MyPy, etc) based on an abstract class.

Keeping this issue open, since I intend to add this support 👍

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

2 participants