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
BaseSettings - pylance complains for not setting default values to fields #3753
Comments
Duplicate of #3617 – it's related to the use of |
I see. I never had problems with class Model(BaseModel):
optional_field1: Optional[int] = None
optional_field2: Optional[str] = ""
optional_field3: Optional[str] = Field(default="") # using positional arg for default here does not work |
Well, apparently not just using Field, as mentioned in #3767 |
A workaround for this
|
I've emailed Eric Traut, the developer of pyright to ask about this. Hopefully he provides some way to disable dataclass transforms on subclasses of |
Here's the response from @erictraut about this, here's our conversation: me:
Here's Eric's response:
In Summary: There's no way to disable the dataclass transform logic on classes which inherit from There are therefore two work around: settings = Settings.parse_obj({})
# or
settings = Settings() # pyright: ignore I don't this is is too bad - because of pydantic's lax/coercion approach to data validation, you'll already need to do a fair bit of ignoring pyright, this is no exception. I've created a PR #3972 to test pyright against pydantic code in CI so we can make sure the behaviour doesn't change (or at least is formalised) |
feedback welcome on #3972 |
I think the PR is good so far, however I don't think the proposed solution settings = Settings.parse_obj({}) makes it very obvious what is happening. Perhaps adding a dedicated method like one of the following would be handy. Idea 1: @classmethod
def from_env(cls):
return cls.parse_obj({}) Idea 2: @classmethod
def parse_env(cls):
return cls.parse_obj({}) Idea 3: @classmethod
def parse_env(cls, defaults=None):
obj = {} if defaults is None else defaults
return cls.parse_obj(obj) I know adding this might be a stretch and might also introduce some issues later down the line, when the underlying issues get resolved in the future. EDIT: These issues are not primarily part of pydantic. |
humm, this was my initial idea, however:
I'd be happy to remove the |
Not only this has the chance to resolve the type checking issue, but some users might prefer this, as this will explicitly note that the settings object is constructed from environment variables (which might not be immediately obvious for users new to Pydantic, typing
How terrible idea it would be to keep the "from_env" method separate from the class, receiving BaseSettings instance just as an argument? from pydantic.env_settings import build_from_env
settings = build_from_env(Settings) # returns Settings
# or
settings = Settings() It's not as elegant as |
This is now the recommended way to set default values. Doing it the previous way causes pyright to assuming you're accidentally omitting required values. Doing it this way is somehow able to tell pyright that these fields are actually optional/have defaults and do not need to be specified. See [here][0] for details. Change seems to have been introduced by [this][1]. [0]: pydantic/pydantic#3753 (comment) [1]: pydantic/pydantic#2721
Hi! I am a huge fan. I am just encountering an error at the moment. The code below used to have no issue with
pylance
using previouspydantic
version. But, right now I can no longer create an instance without having to pass values to fields that are supposed to be set only in.env
or loaded from environment. It is really nice that it now offers autocomplete (same goes toBaseModel
) and it actually still works. It's just that I really do not like the view of squiggly lines 😄 Would there be a quick fix for this?These are my
vscode
settings, btw.Thank you in advance for your response.
The text was updated successfully, but these errors were encountered: