-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
mypy plugin #460
Comments
great idea, yes please. I won't have time to work on it for a while but happy to review a PR if anyone else is keen. |
@samuelcolvin I've implemented an initial relatively-limited-in-scope mypy plugin.
Currently, all it does is replace the signature of the (There is a subtlety here in that you may want to be able to explicitly pass kwargs to the model in your code that it doesn't expect (e.g., if using a Union type), but this bites me much more frequently than it is useful. I think it should probably be a config setting. It's probably possible to make use of the Config in the plugin (e.g., to read off the Having gotten this far, I think it wouldn't be too hard to implement more features and/or more accurate edge-case handling, but it would be nice if we could put together a list of desired features. The |
Current state of the plugin from pydantic import BaseModel, Extra, Schema
class A(BaseModel):
a: int
b: int = Schema(...) # type: ignore
c: int = Schema(None) # type: ignore
d: int = 1
class Config:
allow_mutation = True
extra = Extra.forbid
a_instance = A(a=1, b=1, c=1, d=1, e=1)
# error: Unexpected keyword argument "e" for "A"
a_instance = A(c="")
# error: Missing named argument "a" for "A"
# error: Missing named argument "b" for "A"
# error: Argument "c" to "A" has incompatible type "str"; expected "int"
a_instance.a = 2
# (No errors)
class B(A):
class Config:
allow_mutation = False
extra = Extra.allow
b_instance = B(a=1, b=1, c=1, d=1, e=1)
# (No errors)
b_instance = B(c="")
# error: Missing named argument "a" for "B"
# error: Missing named argument "b" for "B"
# Argument "c" to "B" has incompatible type "str"; expected "int"
b_instance.a = 2
# error: Property "a" defined in "B" is read-only The only things I can think of that remain unhandled are 1) handling generics/create_model, 2) not causing conflicts when assigning to Schema, and 3) maybe handling a couple other config settings. |
Let's start with something small and build further features as we go forward. Very dumb/simple question: how is this plugin distributed? As part of pydantic or as a separate package? |
@dmontagu python sourcefrom datetime import date
from pydantic import BaseModel
class A(BaseModel):
a: date
A(a=date(2019, 1, 1))
A(a=1000)
A(a='2019-01-01') output
This code is valid for pydantic. However, the type is not valid. pydantic cast some object from another type. How should the mypy plugin assert types? Would you tell me your thinking about the case? |
I have configured the plugin to do type-checking on the The way I imagine incorporating the plugin into a code base, is that you whenever you initialize a model using named kwargs (rather than I think it is probably not feasible / maintainable to design the plugin to understand whether one type will be successfully parsed into another. On the other hand, if there is no type-checking on the It would not be hard to modify the plugin to annotate each variable in the But I think this is of substantially lower type-checking value -- for maintenance purposes, I would rather design my code base in such a way that the There is always the option to just not use the plugin as well; that will result in the smallest number of false positives 😄. @koxudaxi Let me know what you think about this. |
Also, @koxudaxi not sure whether you are using the gist version or the PR version of the plugin, but I'll push any updates to the PR version (#722). (I don't think it handles the issue you pointed out any differently, but I'm not sure whether that gist is currently up-to-date, and it will likely not remain up-to-date as I put more effort into the mypy plugin). |
Thank you for your answer. OK, I make the pycharm-plugin which behave in the same way as the mypy plugin. 🙂 I have already used the PR version. |
#722 has been fleshed out pretty extensively, and now includes full docs describing its capabilities. I would appreciate if anyone interested in this could take a look and provide comments. |
Feature Request
I believe that this project could benefit from having a small mypy plugin built for it. Errors like #400 or #280 could be addressed in this plugin.
References
The text was updated successfully, but these errors were encountered: