Skip to content

Commit

Permalink
unittest.mock deprecation warnings - close #8153
Browse files Browse the repository at this point in the history
  • Loading branch information
ibleedicare committed Nov 30, 2023
1 parent 667cd37 commit a69a9e3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -57,6 +57,12 @@ def __setitem__(self, k: str, v: object) -> None:

@dataclass_transform(kw_only_default=True, field_specifiers=(PydanticModelField,))
class ModelMetaclass(ABCMeta):
def __dir__(self) -> list[str]:
attributes = list(super().__dir__())
if '__fields__' in attributes:
attributes.remove('__fields__')
return attributes

def __new__(
mcs,
cls_name: str,
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
6 changes: 3 additions & 3 deletions tests/test_main.py
Expand Up @@ -2,6 +2,7 @@
import platform
import re
import sys
import warnings
from collections import defaultdict
from copy import deepcopy
from dataclasses import dataclass
Expand Down Expand Up @@ -38,7 +39,6 @@
GenerateSchema,
GetCoreSchemaHandler,
PrivateAttr,
PydanticDeprecatedSince20,
PydanticUndefinedAnnotation,
PydanticUserError,
SecretStr,
Expand Down Expand Up @@ -2980,7 +2980,7 @@ 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):
with warnings.catch_warnings():
module = create_module(
# language=Python
"""
Expand All @@ -2995,7 +2995,7 @@ class Model(BaseModel):
help_result_string = pydoc.render_doc(Model)
"""
)

warnings.simplefilter('error')
assert 'class Model' in module.help_result_string


Expand Down
5 changes: 3 additions & 2 deletions tests/test_root_model.py
@@ -1,4 +1,5 @@
import pickle
import warnings
from datetime import date, datetime
from typing import Any, Dict, List, Optional, Union

Expand Down Expand Up @@ -593,7 +594,7 @@ 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):
with warnings.catch_warnings():
module = create_module(
# language=Python
"""
Expand All @@ -605,7 +606,7 @@ def test_help(create_module):
help_result_string = pydoc.render_doc(RootModel)
"""
)

warnings.simplefilter('error')
assert 'class RootModel' in module.help_result_string


Expand Down

0 comments on commit a69a9e3

Please sign in to comment.