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 support for dataclasses #966
mypy plugin support for dataclasses #966
Conversation
Codecov Report
@@ Coverage Diff @@
## master #966 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 19 19
Lines 3287 3293 +6
Branches 650 651 +1
=====================================
+ Hits 3287 3293 +6
Continue to review full report at Codecov.
|
- change name of an expected file - update documents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also add a ! warning
to usage/dataclasses.md
saying that as of v1.2 the mypy plugin must be installed to type check pydantic dataclasses.
- update documents
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@dmontagu
|
Huh, that's very weird. If it's just whitespace differences, it seems like it's probably an issue with mypy? If the issue is just extra whitespace, I would just apply |
Thank you for your advice. mypy --config-file tests/mypy/configs/mypy-default.ini --show-error-codes tests/mypy/modules/success.py I use this method to resolve the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good except, as per the names success.py
and plugin_success.py
should pass and therefore have no output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM
@dmontagu are you happy with this? |
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
@koxudaxi It looks to me like the received config isn't currently used by mypy, it's just allowed to be passed without raising an error -- is that right? @samuelcolvin This looks good to me as a first release ensuring compatibility with the config argument. Eventually it would be nice to have the config values affect the type checking where relevant (I guess I'm not totally sure it affects anything mypy would detect anyway), but I don't think that needs to block a release. Looks good to me! |
pydantic/dataclasses.py
Outdated
@@ -22,6 +22,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: | |||
def __validate__(cls, v: Any) -> 'DataclassType': | |||
pass | |||
|
|||
def __call__(self, *args: Any, **kwargs: Any) -> 'DataclassType': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use a TypeVar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you expect this?
def __call__(self: T, *args: Any, **kwargs: Any) -> T:
pass
Also, Did you want to replace other -> 'DataclassType'
like __init__
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's what I meant, I think it would be better (assuming that is the right signature, which I think it is?).
I don't think the __init__
signature needs to change, but maybe the _validate_dataclass
one should.
(Any other places where 'DataclassType'
is currently used probably should; I believe this is what is done for factory classmethods for BaseModel
-- I don't see why it shouldn't be that way for the dataclasses.)
Also, I would recommend using something like DataclassT
as the name of the TypeVar
, to make it slightly more obvious what's happening, and give it bound=DataclassType
. (The TypeVar for BaseModel
in pydantic.main
is called Model
; I think that's nice.)
As far as I can tell, we're just waiting for a tweak to the signature of If not, let me know and I'll merge. |
@samuelcolvin |
Woop woop. Thank you very much. |
* mypy plugin support for dataclassesv * fix styles and types * - change type-hint for `Config` - change name of an expected file - update documents * fix broken a reference of a document. * - update unittest - update documents * fix a document link * Update docs/mypy_plugin.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update docs/mypy_plugin.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Update docs/mypy_plugin.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * remove extra whitespaces on mypy test results * fix output file name of mypy test * Update docs/usage/dataclasses.md Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * use TypeVar for DataclassType
Change Summary
The PR supports
dataclasses
on mypy pluginRelated issue number
#957
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)