pydantic-pycharm-plugin
pydantic-pycharm-plugin copied to clipboard
Custom dataclass support
Hey,
We are using custom pydantic dataclass that wrapped original pydantic dataclass. It contains some helper serializing function etc. as extra. And generally behaving as pydantic dataclass (same arguments, same return types).
And we import directly our dataclass decorator like below;
from my_package import dataclass
@dataclass
class MyModel:
name : str
# it's warning as 'Unexpected Arguments'
my_model = MyModel(name='Tom')
As workaround we can import as below and get rid of warning.
if TYPE_CHECKING:
from pydantic.dataclasses import dataclass
else:
from my_package import dataclass
But it should be same file with file that defined of model class. I want to add one time this conditional statement and centralized location for example __init__.py file of custom packege like below.
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pydantic.dataclasses import dataclass
else:
from .dataclass import dataclass
__all__ = [
"dataclass",
]
I guess plugin examine only import on file which model defined. I think not important structure of new dataclass because also if we import original dataclass in different module and then import this module and use dataclass like chain, again plugin not recognize pydantic dataclass. It can be supported? Or can you give a new workaround tip for centrailzed importations.
Thank you for creating this issue.
We need to support an option for custom dataclass type in the pyproject.toml
But, the implementation affects the performance of the PyCharm.
I will think about whether to be able to realize.
Is there any progress? Actually no need custom dataclass support. Need to recognizing dataclass on indirect importations like following.
# base/__init__.py
from pydantic.dataclasses import dataclass
__all__ = [
"dataclass",
]
And use dataclass via import from base module.
# models/some_model.py
from .base import dataclass
@dataclass
class MyModel:
...
Thank you.
Is there any progress on this issue?
I'm sorry for my late reply. I'm busy for private reasons 🙇 OK, I will fix it. But, I'm afraid the change may affect the performance to check imported dataclass type 🤔
@resulyrt93 @enescagl I have checked the code. The short answer is PyCharm doesn't support custom dataclass.
I explain the detail.
The plugin uses the feature of 3rd party dataclass in the PyCharm.
And, The plugin define pydantic.dataclases.dataclass is custom dataclass.
https://github.com/koxudaxi/pydantic-pycharm-plugin/blob/c4ae6cfca58112ee914bb9b8d91b27618c841844/src/com/koxudaxi/pydantic/PydanticParametersProvider.kt#L20
But, your custom dataclass is not registered in the plugin.
If your class has unknown dataclass then PyCharm doesn't treat your class as dataclass. https://github.com/JetBrains/intellij-community/blob/03ec7fd6af396582689e562be0a61cf738f8a05d/python/python-psi-impl/src/com/jetbrains/python/codeInsight/stdlib/PyDataclassTypeProvider.kt#L129
We should create an issue for the feature request. https://youtrack.jetbrains.com/issues/PY
Hey, @koxudaxi
Actually there is no need recognizing of custom dataclass. If plugin could support indirect importations like here, we can use all custom dataclasses.
@resulyrt93
The plugin gives MyModel to PyCharm, And PyCharm detects this is class is dataclass.
The plugin can't touch the importation way when PyCharm checks the decorator.
I write additional context.
Pydantic returns a fake Dataclass model in a recent version.
https://github.com/samuelcolvin/pydantic/blob/d7a8272d7e0c151b0bd43df596be02e0d436ebdf/pydantic/dataclasses.py#L231-L241
And, PyCharm detects Pydantci dataclass as the fake Dataclass.
I added the code to ignore the fake Dataclass.
https://github.com/koxudaxi/pydantic-pycharm-plugin/blob/b5f757f36d71ceb0cb6b5a71c23933f4423c2815/src/com/koxudaxi/pydantic/PydanticDataclassTypeProvider.kt#L10-L20
I back to the custom dataclass support.
The problem may come from the fake dataclass 🤔
I suppose you are right, it's about PyCharm.
I tried with python builtin dataclass, pycharm gives warning even on this.

Thank you for checking it. I guess PyCharm doesn't resolve the imported module. It means PyCharm checks only the local module names.
There is a bug report in jetbrains about this topic. Link is here. I hope it will pan out. 🙏