Skip to content

Commit

Permalink
Fix issue with unittest.mock deprecation warnings (#8262)
Browse files Browse the repository at this point in the history
Co-authored-by: Jérémy Phetphoumy <jeremy.phetphoumy@mediactivegroup.com>
  • Loading branch information
ibleedicare and Jérémy Phetphoumy committed Dec 4, 2023
1 parent f5b109f commit bd60cfe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 6 additions & 0 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -249,6 +249,12 @@ def __fields__(self) -> dict[str, FieldInfo]:
warnings.warn('The `__fields__` attribute is deprecated, use `model_fields` instead.', DeprecationWarning)
return self.model_fields # type: ignore

def __dir__(self) -> list[str]:
attributes = list(super().__dir__())
if '__fields__' in attributes:
attributes.remove('__fields__')
return attributes


def init_private_attributes(self: BaseModel, __context: Any) -> None:
"""This function is meant to behave like a BaseModel method to initialise private attributes.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_deprecated.py
Expand Up @@ -332,6 +332,14 @@ class Model(BaseModel):
assert m.__fields_set__ == {'x'}


def test_fields_dir():
class Model(BaseModel):
x: int
y: int = 2

assert '__fields__' not in dir(Model)


@pytest.mark.parametrize('attribute,value', [('allow', 'allow'), ('ignore', 'ignore'), ('forbid', 'forbid')])
def test_extra_used_as_enum(
attribute: str,
Expand Down
13 changes: 4 additions & 9 deletions tests/test_main.py
Expand Up @@ -38,7 +38,6 @@
GenerateSchema,
GetCoreSchemaHandler,
PrivateAttr,
PydanticDeprecatedSince20,
PydanticUndefinedAnnotation,
PydanticUserError,
SecretStr,
Expand Down Expand Up @@ -2978,12 +2977,9 @@ class Bar(BaseModel):


def test_help(create_module):
# since pydoc/help access all attributes to generate their documentation,
# this triggers the deprecation warnings.
with pytest.warns(PydanticDeprecatedSince20):
module = create_module(
# language=Python
"""
module = create_module(
# language=Python
"""
import pydoc
from pydantic import BaseModel
Expand All @@ -2994,8 +2990,7 @@ class Model(BaseModel):
help_result_string = pydoc.render_doc(Model)
"""
)

)
assert 'class Model' in module.help_result_string


Expand Down
12 changes: 4 additions & 8 deletions tests/test_root_model.py
Expand Up @@ -591,21 +591,17 @@ class Model(RootModel):


def test_help(create_module):
# since pydoc/help access all attributes to generate their documentation,
# this triggers the deprecation warnings.
with pytest.warns(PydanticDeprecatedSince20):
module = create_module(
# language=Python
"""
module = create_module(
# language=Python
"""
import pydoc
from pydantic import RootModel
help_result_string = pydoc.render_doc(RootModel)
"""
)

)
assert 'class RootModel' in module.help_result_string


Expand Down

0 comments on commit bd60cfe

Please sign in to comment.