-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Auto-completion when instantiating BaseModel objects #650
Comments
adding auto-completion support would be huge! |
I would also love to see this.
However, one potential issue is that parsing is performed during initialization, so you may purposely want to avoid using annotated types as type hints on the initialization kwargs. I do think it would be nice to have a type-hinted If I recall correctly, there has been some discussion of replacing the use of |
I would love this too. I don't know how pycharm builds suggestions, does it use mypy or it's own parsing? A mypy plugin as suggested in #460 would fix this case in mypy, but would it help in pycharm? Maybe @pauleveritt who I think uses pydantic and works for pycharm might know? I'm at pycharm europe now which is sponsored by pycharm so maybe I can find a real human being to ask. |
Okay, I dug into this just a little:
Separately, I think it might be possible to achieve this by "tricking" the IDE in a variety of ways. I played with various approaches involving This behaves as though undecorated at runtime, but seems to behave properly with both PyCharm and mypy (retaining all desired autocomplete behavior for the BaseModel class). @marlonjan and @timonbimon you might find this useful in the short term? @samuelcolvin I don't know how great this trick is as a long term solution, but for the sake of "paving the way" for such a capability it would probably makes sense to push in the direction of changing the use of the |
Just spoken to pycharm in person, short answer is that we would need to build a pycharm extension in Java or kotlin to do this, attrs or dataclass code is open source and would be a good starting point. |
I want to support auto-completion on PyCharm. I have searched issues and source code for attrs and dataclass. Also, we can find related commit(source code) by the issue number(PY-26354) in intellij-community repository. JetBrains developer implemented auto-completion for attrs and dataclass by request. if you already know the information, then I'm sorry. |
This would be a great feature / plugin! |
Jet brains people were very nice but said pydantic is not popular enough for them to build the plugin themselves, however they will help if we have any issues. |
Yes, I'm watching on this ticket. I'm here at EuroPython with the PyCharm team. If anybody has any questions about compiling PyCharm from GitHub, making a plugin, or what the specifics about a pydantic plugin, let me know. |
@pauleveritt Btw, I have patched PyCharm and build it for auto-completion. It's an experimental challenge yet. Console show errors :( |
@koxudaxi Would you be willing to share your implementation? |
@koxudaxi sorry for the delay in replying, I'm working the PyCharm booth at EuroPython. Kudos for getting as far as you have! I can probably get someone from the PyCharm team to look at your code if you can put it in a gist or in a fork. As you might imagine, PyCharm tries to be careful about the things it commits to supporting. Over the years it adds up, and we have to keep supporting things even when most people stop using them. So it's better to do it as a plugin and, if it becomes popular, we could discuss a merge. |
@dmontagu sorry, I have forgotten it. |
@pauleveritt Also, I use Jetbrains products every day, which increase development speed. If I contribute to your community, I'm happy. Thank you. |
Thanks to all of you for taking a look at this!! |
Thank you @koxudaxi and if you want me to get someone from the team to review, let me know. I personally am very, very happy...VERY happy...to see you work on this. |
@pauleveritt |
@koxudaxi Related, I've made some progress on a mypy plugin, linked on this issue: #460. It definitely doesn't behave properly under all circumstances yet, but it at least works for me to type-check |
I use the plugin in the pro version.
Sorry, I don't know it. I must learn how to build a plugin for each edition. I have checked your mypy-plugin, I feel a great start. :) |
Yeah, I was pleasantly surprised how easy it was to work with once I got started. Related, there also appears to be some good infrastructure for calling mypy runs from within python, which I think would enable us to more easily/incrementally add mypy tests (in particular, expected fails) than the current system. I would be surprised if the mypy plugin worked directly with PyCharm, so it would probably still be nice to build an autocomplete plugin for PyCharm. @koxudaxi if you can think of any specific requests for the mypy plugin beyond type checking the |
@dmontagu |
great work @koxudaxi. Feel free to ping me here if there's anything I can do to help make plugin development easier in pydantic. |
The PyCharm plugin for Pydantic is now available on JetBrains Plugins Repository. The Plugin page is https://plugins.jetbrains.com/plugin/12861-pydantic Also, I am developing the plugin on this repository https://github.com/koxudaxi/pydantic-pycharm-plugin |
Fantastic, well done. I've installed it and at first glance it seems to do everything I need. |
@koxudaxi would you consider submitting a PR to add this to the documentation? |
@samuelcolvin OK, I will create the PR. |
Hey everyone, Does anyone know if a similar solution exists within VsCode? Auto-completion when instantiating BaseModel objects would be awesome there as well. |
I just hit the completion problem when using Sublime with LSP and python-language-server. Took me a while to figure out that completion for methods in class inheriting from Any good workarounds until this is resolved? pyls logs if helpful
|
i'm using vscode and would really like to have auto completion for creating models. would it make sense to have a class inherit from BaseModel and be decorated with dataclass ? from dataclasses import dataclass
from pydantic import BaseModel
@dataclass
class User(BaseModel):
id: int
name: str
age: float |
I doubt that code above would work properly, but what you can try is to make fake from typing import TYPE_CHECKING
from pydantic import BaseModel
if TYPE_CHECKING:
from dataclasses import dataclass
else:
def dataclass(model):
return model
@dataclass
class User(BaseModel):
id: int
name: str
age: float |
The thing is I don't want to trick my team mates when I trick the type checker, it will make it look like they are actually dataclasses... i tried using just the pydantic dataclass but that gives this result: from pydantic.dataclasses import dataclass
@dataclass
class User:
id: int
name: str
age: float could be great if the pydantic dataclass would do that maybe, and then i could just use `@dataclass from pydantic possibly (when fastapi fully supports it) |
using @MrMrRobat advice i currently went with this workaround which seems to work with vscode and is not terribly misleading: created an auto_complete_only.py helper and aliased @ dataclass to @ autocomplete from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dataclasses import dataclass
else:
def dataclass(model):
return model
autocomplete = dataclass |
Could even be simpler (tho untested): from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dataclasses import dataclass as autocomplete
else:
def autocomplete(model):
return model 🤷♂️ |
Oh god, you must be kidding me. Pyright is so incredibly buggy... |
@josx, as I understand, it's more a cosmetic issue, but you can try this workaround to use workaround above 🙃 without warnings: from typing import TYPE_CHECKING
# trick PyRight into thinking that it might be both False or True
RUNTIME = not TYPE_CHECKING
if RUNTIME:
def autocomplete(model):
return model
else:
from dataclasses import dataclass as autocomplete I didn't test it, but it should work. |
I'm closing this as pycharm has an excellent plugin built by @koxudaxi and vscode support is discussed in microsoft/python-language-server#1898 It sounds like any further solution either requires changes to the language servers or third party plugins. If there are any tweaks pydantic can make to make the jobs of language servers easier, please create a new issue. If anyone succeeds in building a useful plugin, please create a PR to update the docs. |
Very nice to find that a workaround exists even if somewhat hacky. I came up with this simple extension of the idea – here adapted for With this, you can import from typing import TYPE_CHECKING
from pydantic import BaseModel as _BaseModel
# trick PyRight into thinking that it might be both False or True
RUNTIME = not TYPE_CHECKING
if RUNTIME:
def base_model(model):
return model
else:
from dataclasses import dataclass as base_model
@base_model
class BaseModel(_BaseModel): ... |
@koxudaxi Hey man, great job! |
For anybody following this issue, the fix has been merged and available in version |
When will |
Next few days. |
@samuelcolvin now I'm using SQLModel and autocompletion it's not with Pycharm work |
workaround to make pyright check Pydantic code.
Feature Request
Hi! Currently auto-completion does not work well when calling the initializer of classes that inherit from pydantic.BaseModel, at least in IntelliJ / PyCharm.
Are there any plans to improve that? Auto-completion works nicely with dataclasses from the python 3.7 standard library. Same applies to highlighting type mismatches or missing arguments in the call. Here is a comparison of the behavior in IntelliJ:
Code example to reproduce the screenshots:
Thanks for creating pydantic! Best regards,
Marlon
The text was updated successfully, but these errors were encountered: