Skip to content
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

Not supported: from pydantic.v1 import BaseModel #1006

Open
LukasGold opened this issue Oct 17, 2024 · 0 comments
Open

Not supported: from pydantic.v1 import BaseModel #1006

LukasGold opened this issue Oct 17, 2024 · 0 comments

Comments

@LukasGold
Copy link

LukasGold commented Oct 17, 2024

Describe the bug
Even though from pydantic import BaseModel (in Pydantic v1 and v2) is supported, the import from pydantic.v1 import BaseModel does not seem to be supported by the Plugin

To Reproduce
Steps to reproduce the behavior:

  1. Open PyCharm
  2. Create a new file
  3. Copy the following example:
from pydantic import BaseModel

class Person(BaseModel):
    first_name: str
    last_name: str

john = Person()  # Test position 1


from pydantic.v1 import BaseModel

class Person2(BaseModel):
    first_name: str
    last_name: str

john2 = Person2()  # Test position 2
  1. Move the cursor inside the parenthesis of the init (john and john2) and press the key bind for basic code completion (Ctrl + Space)
  2. Compare the behavior at those two positions --> See screenshots

Expected behavior
Parameter info and basic code completion for child classes of pydantic.v1.BaseModel - Behavior just like for Pydantic v1 and v2

Screenshots
Unexpected behavior / Bug:
Screenshot 2024-10-17 112358
Expected behavior:
Screenshot 2024-10-17 112341

Environments (please complete the following information):

  • IDE: PyCharm 2024.2.3 (Professional Edition) - Build #PY-242.23339.19
  • OS: Windows 11 - BuildNumber 22631
  • Pydantic Version 2.8.2, also tested on 2.9.1
  • Plugin version 0.4.15

Additional context
We are in the transition of our python package from pydantic v1 to pydantic v2. As suggested in the migration guide , we are using

from pydantic.v1 import BaseModel

Unfortunately, the parameter info and basic code completion, available with BaseModel (imported via from pydantic import BaseModel from Pydantic v1 or v2), are not available anymore.

Temporary / Hacky workaround
For type checking to work in VS Code, we have created a decorator:

if TYPE_CHECKING:
    from dataclasses import dataclass as _basemodel_decorator
    from pydantic import BaseModel  # Added line
else:
    _basemodel_decorator = lambda x: x  # noqa: E731

I have added a line to this decorator, with which the Pydantic Plugin works again, but the code checker outputs deprecation warning and other error associated with the migration from Pydantic v1 to v2.

I don't know if this is overly complex to fix / implement, but I would be incredibly happy to be able to use this awesome Plugin for our python package again!

Best regards, Lukas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant