Skip to content

Commit 63e42db

Browse files
uriyyoPrettyWood
andauthored
Fix bug when mypy plugin fail on construct method call (pydantic#2767)
* 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 <[email protected]> Co-authored-by: Eric Jolibois <[email protected]>
1 parent 0c2f69c commit 63e42db

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

changes/2753-uriyyo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug when `mypy` plugin fails on `construct` method call for `BaseSettings` derived classes.

pydantic/env_settings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import warnings
33
from pathlib import Path
4-
from typing import AbstractSet, Any, Callable, Dict, List, Mapping, Optional, Tuple, Union
4+
from typing import AbstractSet, Any, Callable, ClassVar, Dict, List, Mapping, Optional, Tuple, Type, Union
55

66
from .config import BaseConfig, Extra
77
from .fields import ModelField
@@ -114,7 +114,7 @@ def customise_sources(
114114
) -> Tuple[SettingsSourceCallable, ...]:
115115
return init_settings, env_settings, file_secret_settings
116116

117-
__config__: Config # type: ignore
117+
__config__: ClassVar[Type[Config]] # type: ignore
118118

119119

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

171171
if field.is_complex():
172172
try:
173-
env_val = settings.__config__.json_loads(env_val) # type: ignore
173+
env_val = settings.__config__.json_loads(env_val)
174174
except ValueError as e:
175175
raise SettingsError(f'error parsing JSON for "{env_name}"') from e
176176
elif (
@@ -179,7 +179,7 @@ def __call__(self, settings: BaseSettings) -> Dict[str, Any]:
179179
and any(f.is_complex() for f in field.sub_fields)
180180
):
181181
try:
182-
env_val = settings.__config__.json_loads(env_val) # type: ignore
182+
env_val = settings.__config__.json_loads(env_val)
183183
except ValueError:
184184
pass
185185
d[field.alias] = env_val

tests/mypy/modules/plugin_success.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import ClassVar, Optional, Union
22

3-
from pydantic import BaseModel, Field, create_model
3+
from pydantic import BaseModel, BaseSettings, Field, create_model
44
from pydantic.dataclasses import dataclass
55

66

@@ -162,3 +162,10 @@ class Config:
162162

163163
class ModelWithSelfField(BaseModel):
164164
self: str
165+
166+
167+
class SettingsModel(BaseSettings):
168+
pass
169+
170+
171+
settings = SettingsModel.construct()

0 commit comments

Comments
 (0)