Skip to content

Commit

Permalink
Fix bug when mypy plugin fail on construct method call (pydantic#2767)
Browse files Browse the repository at this point in the history
* Fix bug when mypy plugin fail on construct method call

* Update type annotation for __config__ field

* Remove type ignore

* Update changes/2753-uriyyo.md

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
  • Loading branch information
2 people authored and jpribyl committed Oct 7, 2021
1 parent ff158b3 commit c71483d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
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()

0 comments on commit c71483d

Please sign in to comment.