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

Make Mypy & JSON models friends #18

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft

Make Mypy & JSON models friends #18

wants to merge 12 commits into from

Conversation

sedwards2009
Copy link

@sedwards2009 sedwards2009 commented Apr 16, 2020

This is some investigation work for making jsonmodel play nice with Mypy.

At the moment:

  • Mypy can now understand that models have fields and to check assignments for valid field names.
  • Mypy can now understand the data type of (some basic) model fields.

This is incomplete of course, but the progress is good.

It may be possible to make ListField accept a generics type and to eliminate this new duplication of code with the ListField constructor.

It seems unlikely that there is a way of explaining to Mypy that model constructors accept keyword arguments which correspond to the names of the model's fields. In this case manually adding a __init__() method with the correct types would work too.

The run_mypy.sh script can be used to put some of the code through mypy.

@ChrisTerBeke ChrisTerBeke marked this pull request as draft April 16, 2020 13:33
jsonmodels/fields.py Outdated Show resolved Hide resolved
@sedwards2009
Copy link
Author

This package looks useful for supporting Mypy generics and reducing redundant args to constructors. https://github.com/ilevkivskyi/typing_inspect i.e. you specify the generic types and the rest is automatic.

@sedwards2009
Copy link
Author

http://mypy-lang.blogspot.com/2019/03/extending-mypy-with-plugins.html -> "Bundled plugins for standard library", describes how a plugin to mypy is used to add virtual typed methods to a class. This is basically the same problem we have in jsonmodels with respect to the model constructors.

@DanielSchiavini
Copy link

Related work: jazzband#165

from mypy.plugin import Plugin, AttributeContext, FunctionContext
from mypy.types import Type as MypyType

class JSONModelsPlugin(Plugin):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool beans 👀

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

Successfully merging this pull request may close these issues.

2 participants