MCPcopy Index your code
hub / github.com/fastapi/fastapi / ModelField

Class ModelField

fastapi/_compat/v2.py:114–244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

112
113@dataclass
114class ModelField:
115 field_info: FieldInfo
116 name: str
117 mode: Literal["validation", "serialization"] = "validation"
118 config: ConfigDict | None = None
119
120 @property
121 def alias(self) -> str:
122 a = self.field_info.alias
123 return a if a is not None else self.name
124
125 @property
126 def validation_alias(self) -> str | None:
127 va = self.field_info.validation_alias
128 if isinstance(va, str) and va:
129 return va
130 return None
131
132 @property
133 def serialization_alias(self) -> str | None:
134 sa = self.field_info.serialization_alias
135 return sa or None
136
137 @property
138 def default(self) -> Any:
139 return self.get_default()
140
141 def __post_init__(self) -> None:
142 with warnings.catch_warnings():
143 # Pydantic >= 2.12.0 warns about field specific metadata that is unused
144 # (e.g. `TypeAdapter(Annotated[int, Field(alias='b')])`). In some cases, we
145 # end up building the type adapter from a model field annotation so we
146 # need to ignore the warning:
147 if shared.PYDANTIC_VERSION_MINOR_TUPLE >= (2, 12):
148 from pydantic.warnings import UnsupportedFieldAttributeWarning
149
150 warnings.simplefilter(
151 "ignore", category=UnsupportedFieldAttributeWarning
152 )
153 # TODO: remove after setting the min Pydantic to v2.12.3
154 # that adds asdict(), and use self.field_info.asdict() instead
155 field_dict = asdict(self.field_info)
156 annotated_args = (
157 field_dict["annotation"],
158 *field_dict["metadata"],
159 # this FieldInfo needs to be created again so that it doesn't include
160 # the old field info metadata and only the rest of the attributes
161 Field(**field_dict["attributes"]),
162 )
163 self._type_adapter: TypeAdapter[Any] = TypeAdapter(
164 Annotated[annotated_args], # ty: ignore[invalid-type-form]
165 config=self.config,
166 )
167
168 def get_default(self) -> Any:
169 if self.field_info.is_required():
170 return Undefined
171 return self.field_info.get_default(call_default_factory=True)

Callers 2

get_definitionsFunction · 0.85
get_model_fieldsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…