Coverage for pydantic / _internal / _type_refs.py: 89.58%
72 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
1"""Logic related to reference computation for types."""
3from __future__ import annotations 1pabcdeqfghijsrklmno
5import sys 1pabcdeqfghijsrklmno
6from enum import Enum 1pabcdeqfghijsrklmno
7from typing import TYPE_CHECKING, Any, cast, get_args, get_origin 1pabcdeqfghijsrklmno
9from typing_extensions import Sentinel, TypeAliasType 1pabcdeqfghijsrklmno
11from . import _repr 1pabcdeqfghijsrklmno
12from ._import_utils import import_cached_base_model 1pabcdeqfghijsrklmno
13from ._utils import lenient_issubclass 1pabcdeqfghijsrklmno
15if TYPE_CHECKING: 1pabcdeqfghijsrklmno
16 from pydantic import BaseModel
19def _args_ref(args: tuple[Any, ...]) -> str: 1pabcdeqfghijsrklmno
20 arg_refs: list[str] = [] 1pabcdeqfghijsrklmno
21 for arg in args: 1pabcdeqfghijsrklmno
22 if isinstance(arg, str): 1pabcdeqfghijsrklmno
23 # Handle string args as a special case; we may be able to remove this special handling if we
24 # wrap them in a ForwardRef at some point.
25 arg_ref = f'{arg}:str-{id(arg)}' 1pabcdeqfghijrklmno
26 else:
27 arg_ref = f'{_repr.display_as_type(arg)}:{id(arg)}' 1pabcdeqfghijsrklmno
28 arg_refs.append(arg_ref) 1pabcdeqfghijsrklmno
30 if arg_refs: 1pabcdeqfghijsrklmno
31 return f'[{",".join(arg_refs)}]' 1pabcdeqfghijsrklmno
32 else:
33 return '' 1pabcdeqfghijsrklmno
36def model_type_ref(type_: type[BaseModel], args_override: tuple[Any, ...] | None = None) -> str: 1pabcdeqfghijsrklmno
37 """Produce a reference for Pydantic base models."""
38 try: 1pabcdeqfghijsrklmno
39 generic_metadata = type_.__pydantic_generic_metadata__ 1pabcdeqfghijsrklmno
40 except AttributeError: 1pabcdeqfghijsrklmno
41 # `type_` is `BaseModel`:
42 return f'{type_.__module__}.{type_.__qualname__}:{id(type_)}' 1pabcdeqfghijsrklmno
44 origin = generic_metadata['origin'] or type_ 1pabcdeqfghijsrklmno
45 args = generic_metadata['args'] 1pabcdeqfghijsrklmno
46 if not args and args_override is not None: 1pabcdeqfghijsrklmno
47 args = args_override 1pabcdeqfghijsrklmno
49 # While it is typed as `str`, `__module__` could in theory be `None`:
50 module_name = cast(str | None, origin.__module__) 1pabcdeqfghijsrklmno
51 if module_name is None: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true1pabcdeqfghijsrklmno
52 module_name = '<No __module__>'
54 return f'{module_name}.{origin.__qualname__}:{id(origin)}{_args_ref(args)}' 1pabcdeqfghijsrklmno
57def class_type_ref(type_: Any, origin: type[Any] | None) -> str: 1pabcdeqfghijsrklmno
58 """Produce a reference for an arbitrary class (dataclasses, typed dictionaries, named tuples)."""
59 tp: type[Any] = origin if origin is not None else type_ 1pabcdeqfghijsrklmno
61 # While it is typed as `str`, `__module__` could in theory be `None`:
62 module_name = cast(str | None, tp.__module__) 1pabcdeqfghijsrklmno
63 if module_name is None: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true1pabcdeqfghijsrklmno
64 module_name = '<No __module__>'
66 return f'{module_name}.{tp.__qualname__}:{id(tp)}{_args_ref(get_args(type_))}' 1pabcdeqfghijsrklmno
69_NOT_PROVIDED = Sentinel('_NOT_PROVIDED') 1pabcdeqfghijsrklmno
72def any_class_type_ref(type_: Any, origin: type[Any] | None | _NOT_PROVIDED = _NOT_PROVIDED) -> str: 1pabcdeqfghijsrklmno
73 """Produce a reference for any class kind, including Pydantic models."""
74 BaseModel_ = import_cached_base_model() 1pabcdeqfghijsrklmno
76 if lenient_issubclass(type_, BaseModel_): 1pabcdeqfghijsrklmno
77 return model_type_ref(type_) # pyright: ignore[reportArgumentType] 1pabcdeqfghijsrklmno
79 origin = get_origin(type_) if origin is _NOT_PROVIDED else origin 1pabcdeqfghijrklmno
80 return class_type_ref(type_, origin) # pyright: ignore[reportArgumentType] (https://github.com/microsoft/pyright/issues/11115) 1pabcdeqfghijrklmno
83def any_type_ref(type_: Any, origin: Any | _NOT_PROVIDED = _NOT_PROVIDED) -> str: 1pabcdeqfghijsrklmno
84 """Produce a reference for any object, including non-class objects."""
85 BaseModel_ = import_cached_base_model() 1pabcdeqfghijrklmno
87 if lenient_issubclass(type_, BaseModel_): 1pabcdeqfghijrklmno
88 return model_type_ref(type_) # pyright: ignore[reportArgumentType] 1pabcdeqfghijrklmno
90 origin = get_origin(type_) if origin is _NOT_PROVIDED else origin 1pabcdeqfghijrklmno
91 tp: Any = origin if origin is not None else type_ 1pabcdeqfghijrklmno
93 module_name = getattr(tp, '__module__', None) 1pabcdeqfghijrklmno
94 if module_name is None: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true1pabcdeqfghijrklmno
95 module_name = '<No __module__>'
97 try: 1pabcdeqfghijrklmno
98 qualname = getattr(tp, '__qualname__', f'<No __qualname__: {tp}>') 1pabcdeqfghijrklmno
99 except Exception:
100 qualname = getattr(tp, '__qualname__', '<No __qualname__>')
102 return f'{module_name}.{qualname}:{id(tp)}{_args_ref(get_args(type_))}' 1pabcdeqfghijrklmno
105def enum_type_ref(type_: type[Enum]) -> str: 1pabcdeqfghijsrklmno
106 """Produce a reference for an enum class."""
107 # While it is typed as `str`, `__module__` could in theory be `None`:
108 module_name = cast(str | None, type_.__module__) 1pabcdeqfghijrklmno
109 if module_name is None: 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true1pabcdeqfghijrklmno
110 module_name = '<No __module__>'
112 return f'{module_name}.{type_.__qualname__}:{id(type_)}' 1pabcdeqfghijrklmno
115def type_alias_type_ref(type_: TypeAliasType) -> str: 1pabcdeqfghijsrklmno
116 """Produce a reference for a type alias."""
117 origin: TypeAliasType = get_origin(type_) or type_ 1pabcdeqfghijrklmno
119 module_name = type_.__module__ 1pabcdeqfghijrklmno
120 if module_name is None: 1pabcdeqfghijrklmno
121 module_name = '<No __module__>' 1abcdefghijklmno
123 if sys.version_info >= (3, 15): 1pabcdeqfghijrklmno
124 type_ref = f'{module_name}.{origin.__qualname__}:{id(origin)}' 1deijno
125 else:
126 type_ref = f'{module_name}.{origin.__name__}:{id(origin)}' 1pabcqfghrklm
128 return f'{type_ref}{_args_ref(get_args(type_))}' 1pabcdeqfghijrklmno