pydantic-pycharm-plugin icon indicating copy to clipboard operation
pydantic-pycharm-plugin copied to clipboard

Custom dataclass support

Open resulyrt93 opened this issue 3 years ago • 11 comments
trafficstars

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.

resulyrt93 avatar Dec 03 '21 09:12 resulyrt93

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.

koxudaxi avatar Dec 09 '21 17:12 koxudaxi

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.

resulyrt93 avatar Feb 02 '22 13:02 resulyrt93

Is there any progress on this issue?

enescagl avatar Mar 04 '22 20:03 enescagl

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 🤔

koxudaxi avatar Mar 07 '22 15:03 koxudaxi

@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

koxudaxi avatar Mar 07 '22 17:03 koxudaxi

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 avatar Mar 07 '22 17:03 resulyrt93

@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.

koxudaxi avatar Mar 07 '22 17:03 koxudaxi

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 🤔

koxudaxi avatar Mar 07 '22 17:03 koxudaxi

I suppose you are right, it's about PyCharm. I tried with python builtin dataclass, pycharm gives warning even on this. image

resulyrt93 avatar Mar 07 '22 17:03 resulyrt93

Thank you for checking it. I guess PyCharm doesn't resolve the imported module. It means PyCharm checks only the local module names.

koxudaxi avatar Mar 07 '22 18:03 koxudaxi

There is a bug report in jetbrains about this topic. Link is here. I hope it will pan out. 🙏

resulyrt93 avatar May 05 '22 08:05 resulyrt93