Skip to content

Commit 2c61bfd

Browse files
Viicossydney-runkle
authored andcommitted
Fix evaluation of stringified annotations during namespace inspection (#10347)
1 parent 3d364cb commit 2c61bfd

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Diff for: pydantic/_internal/_model_construction.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from abc import ABCMeta
1212
from functools import lru_cache, partial
1313
from types import FunctionType
14-
from typing import Any, Callable, ForwardRef, Generic, Literal, NoReturn
14+
from typing import Any, Callable, Generic, Literal, NoReturn
1515

1616
import typing_extensions
1717
from pydantic_core import PydanticUndefined, SchemaSerializer
@@ -30,6 +30,7 @@
3030
from ._schema_generation_shared import CallbackGetCoreSchemaHandler
3131
from ._signature import generate_pydantic_signature
3232
from ._typing_extra import (
33+
_make_forward_ref,
3334
eval_type_backport,
3435
is_annotated,
3536
is_classvar,
@@ -436,6 +437,8 @@ def inspect_namespace( # noqa C901
436437
is_valid_privateattr_name(ann_name)
437438
and ann_name not in private_attributes
438439
and ann_name not in ignored_names
440+
# This condition is a false negative when `ann_type` is stringified,
441+
# but it is handled in `set_model_fields`:
439442
and not is_classvar(ann_type)
440443
and ann_type not in all_ignored_types
441444
and getattr(ann_type, '__module__', None) != 'functools'
@@ -446,7 +449,9 @@ def inspect_namespace( # noqa C901
446449
frame = sys._getframe(2)
447450
if frame is not None:
448451
ann_type = eval_type_backport(
449-
ForwardRef(ann_type), globalns=frame.f_globals, localns=frame.f_locals
452+
_make_forward_ref(ann_type, is_argument=False, is_class=True),
453+
globalns=frame.f_globals,
454+
localns=frame.f_locals,
450455
)
451456
if is_annotated(ann_type):
452457
_, *metadata = typing_extensions.get_args(ann_type)

Diff for: tests/test_forward_ref.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,12 @@ def test_class_var_as_string(create_module):
581581
582582
class Model(BaseModel):
583583
a: ClassVar[int]
584+
_b: ClassVar[int]
584585
"""
585586
)
586587

587-
assert module.Model.__class_vars__ == {'a'}
588+
assert module.Model.__class_vars__ == {'a', '_b'}
589+
assert module.Model.__private_attributes__ == {}
588590

589591

590592
def test_json_encoder_str(create_module):

0 commit comments

Comments
 (0)