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

Fix bug when mypy plugin fail on construct method call #2767

Merged
merged 6 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2753-uriyyo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug when `mypy` plugin fails on `construct` method call for `BaseSettings` derived classes.
8 changes: 4 additions & 4 deletions pydantic/env_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import warnings
from pathlib import Path
from typing import AbstractSet, Any, Callable, Dict, List, Mapping, Optional, Tuple, Union
from typing import AbstractSet, Any, Callable, ClassVar, Dict, List, Mapping, Optional, Tuple, Type, Union

from .config import BaseConfig, Extra
from .fields import ModelField
Expand Down Expand Up @@ -114,7 +114,7 @@ def customise_sources(
) -> Tuple[SettingsSourceCallable, ...]:
return init_settings, env_settings, file_secret_settings

__config__: Config # type: ignore
__config__: ClassVar[Type[Config]] # type: ignore


class InitSettingsSource:
Expand Down Expand Up @@ -170,7 +170,7 @@ def __call__(self, settings: BaseSettings) -> Dict[str, Any]:

if field.is_complex():
try:
env_val = settings.__config__.json_loads(env_val) # type: ignore
env_val = settings.__config__.json_loads(env_val)
except ValueError as e:
raise SettingsError(f'error parsing JSON for "{env_name}"') from e
elif (
Expand All @@ -179,7 +179,7 @@ def __call__(self, settings: BaseSettings) -> Dict[str, Any]:
and any(f.is_complex() for f in field.sub_fields)
):
try:
env_val = settings.__config__.json_loads(env_val) # type: ignore
env_val = settings.__config__.json_loads(env_val)
except ValueError:
pass
d[field.alias] = env_val
Expand Down
9 changes: 8 additions & 1 deletion tests/mypy/modules/plugin_success.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import ClassVar, Optional, Union

from pydantic import BaseModel, Field, create_model
from pydantic import BaseModel, BaseSettings, Field, create_model
from pydantic.dataclasses import dataclass


Expand Down Expand Up @@ -162,3 +162,10 @@ class Config:

class ModelWithSelfField(BaseModel):
self: str


class SettingsModel(BaseSettings):
pass


settings = SettingsModel.construct()