Returns true if a field with the given schema should have a title set based on the field name. Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title (e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses).
(self, schema: CoreSchemaOrField)
| 2170 | return name.title().replace('_', ' ').strip() |
| 2171 | |
| 2172 | def field_title_should_be_set(self, schema: CoreSchemaOrField) -> bool: |
| 2173 | """Returns true if a field with the given schema should have a title set based on the field name. |
| 2174 | |
| 2175 | Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title |
| 2176 | (e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses). |
| 2177 | |
| 2178 | Args: |
| 2179 | schema: The schema to check. |
| 2180 | |
| 2181 | Returns: |
| 2182 | `True` if the field should have a title set, `False` otherwise. |
| 2183 | """ |
| 2184 | if _core_utils.is_core_schema_field(schema): |
| 2185 | if schema['type'] == 'computed-field': |
| 2186 | field_schema = schema['return_schema'] |
| 2187 | else: |
| 2188 | field_schema = schema['schema'] |
| 2189 | return self.field_title_should_be_set(field_schema) |
| 2190 | |
| 2191 | elif _core_utils.is_core_schema(schema): |
| 2192 | if schema.get('ref'): # things with refs, such as models and enums, should not have titles set |
| 2193 | return False |
| 2194 | if schema['type'] in {'default', 'nullable', 'definitions'}: |
| 2195 | return self.field_title_should_be_set(schema['schema']) # type: ignore[typeddict-item] |
| 2196 | if _core_utils.is_function_with_inner_schema(schema): |
| 2197 | return self.field_title_should_be_set(schema['schema']) |
| 2198 | if schema['type'] == 'definition-ref': |
| 2199 | # Referenced schemas should not have titles set for the same reason |
| 2200 | # schemas with refs should not |
| 2201 | return False |
| 2202 | return True # anything else should have title set |
| 2203 | |
| 2204 | else: |
| 2205 | raise PydanticInvalidForJsonSchema(f'Unexpected schema type: schema={schema}') # pragma: no cover |
| 2206 | |
| 2207 | def normalize_name(self, name: str) -> str: |
| 2208 | """Normalizes a name to be used as a key in a dictionary. |
no test coverage detected