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 issue unittest.mock deprecation warnings #8262

Merged
merged 3 commits into from Dec 4, 2023
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
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):
sydney-runkle marked this conversation as resolved.
Show resolved Hide resolved
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