Skip to content

Make Mypy & JSON models friends#18

Draft
sedwards2009 wants to merge 12 commits intomasterfrom
mypy
Draft

Make Mypy & JSON models friends#18
sedwards2009 wants to merge 12 commits intomasterfrom
mypy

Conversation

@sedwards2009
Copy link
Copy Markdown

@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
@sedwards2009
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

Related work: jazzband#165

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

class JSONModelsPlugin(Plugin):
Copy link
Copy Markdown

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