44from typing import Any , Optional , Type
55from inspect import isclass
66
7- from pydantic .error_wrappers import ErrorWrapper
8- import pydantic .fields
9- from pydantic .typing import is_literal_type
7+ from pydantic .v1 . error_wrappers import ErrorWrapper
8+ import pydantic .v1 . fields
9+ from pydantic .v1 . typing import is_literal_type
1010
1111from .._types import OpenJDModel , ResolutionScope
1212from .._format_strings import FormatString , FormatStringError
104104# 4. Since this validation is a pre-validator, we basically have to re-implement a fragment of Pydantic's model parser for this
105105# depth first traversal. Thus, you'll need to know the following about Pydantic v1.x's data model and parser to understand this
106106# implementation:
107- # a) All models are derived from pydantic.BaseModel
107+ # a) All models are derived from pydantic.v1. BaseModel
108108# b) pydantic.BaseModel.__fields__: dict[str, pydantic.ModelField] is injected into all BaseModels by pydantic's BaseModel metaclass.
109109# This member is what gives pydantic information about each of the fields defined in the model class. The key of the dict is the
110110# name of the field in the model.
@@ -228,7 +228,7 @@ def _validate_model_template_variable_references(
228228 value_symbols = _collect_variable_definitions (cls , values , current_scope , symbol_prefix )
229229
230230 # Recursively validate the contents of FormatStrings within the model.
231- # Note: cls.__fields__: dict[str, pydantic.fields.ModelField]
231+ # Note: cls.__fields__: dict[str, pydantic.v1. fields.ModelField]
232232 for field_name , field_model in cls .__fields__ .items ():
233233 field_value = values .get (field_name , None )
234234 if field_value is None :
@@ -248,7 +248,7 @@ def _validate_model_template_variable_references(
248248 # Add in all of the symbols passed down from the parent.
249249 validation_symbols .update_self (symbols )
250250
251- if field_model .shape == pydantic .fields .SHAPE_SINGLETON :
251+ if field_model .shape == pydantic .v1 . fields .SHAPE_SINGLETON :
252252 _validate_singleton (
253253 errors ,
254254 field_model ,
@@ -258,7 +258,7 @@ def _validate_model_template_variable_references(
258258 validation_symbols ,
259259 (* loc , field_name ),
260260 )
261- elif field_model .shape == pydantic .fields .SHAPE_LIST :
261+ elif field_model .shape == pydantic .v1 . fields .SHAPE_LIST :
262262 if not isinstance (field_value , list ):
263263 continue
264264 assert field_model .sub_fields is not None # For the type checker
@@ -273,7 +273,7 @@ def _validate_model_template_variable_references(
273273 validation_symbols ,
274274 (* loc , field_name , i ),
275275 )
276- elif field_model .shape == pydantic .fields .SHAPE_DICT :
276+ elif field_model .shape == pydantic .v1 . fields .SHAPE_DICT :
277277 if not isinstance (field_value , dict ):
278278 continue
279279 assert field_model .sub_fields is not None # For the type checker
@@ -300,7 +300,7 @@ def _validate_model_template_variable_references(
300300
301301def _validate_singleton (
302302 errors : list [ErrorWrapper ],
303- field_model : pydantic .fields .ModelField ,
303+ field_model : pydantic .v1 . fields .ModelField ,
304304 field_value : Any ,
305305 current_scope : ResolutionScope ,
306306 symbol_prefix : str ,
@@ -356,7 +356,7 @@ def _validate_singleton(
356356
357357def _validate_general_union (
358358 errors : list [ErrorWrapper ],
359- field_model : pydantic .fields .ModelField ,
359+ field_model : pydantic .v1 . fields .ModelField ,
360360 field_value : Any ,
361361 current_scope : ResolutionScope ,
362362 symbol_prefix : str ,
@@ -375,11 +375,11 @@ def _validate_general_union(
375375 # and attempt to process the value as that type.
376376 assert field_model .sub_fields is not None # For the type checker
377377 for sub_field in field_model .sub_fields :
378- if sub_field .shape == pydantic .fields .SHAPE_SINGLETON :
378+ if sub_field .shape == pydantic .v1 . fields .SHAPE_SINGLETON :
379379 _validate_singleton (
380380 errors , sub_field , field_value , current_scope , symbol_prefix , symbols , loc
381381 )
382- elif sub_field .shape == pydantic .fields .SHAPE_LIST :
382+ elif sub_field .shape == pydantic .v1 . fields .SHAPE_LIST :
383383 if not isinstance (field_value , list ):
384384 # The given value must be a list in this case.
385385 continue
@@ -414,8 +414,8 @@ def _check_format_string(
414414
415415
416416def _get_model_for_singleton_value (
417- field_model : pydantic .fields .ModelField , value : Any
418- ) -> Optional [pydantic .fields .ModelField ]:
417+ field_model : pydantic .v1 . fields .ModelField , value : Any
418+ ) -> Optional [pydantic .v1 . fields .ModelField ]:
419419 """Given a ModelField and the value that we're given for that field, determine
420420 the actual ModelField for the value in the event that the ModelField may be for
421421 a discriminated union."""
@@ -509,7 +509,7 @@ def _collect_variable_definitions( # noqa: C901 (suppress: too complex)
509509 symbol_name = f"{ symbol_prefix } { symbol } "
510510 _add_symbol (symbols ["__self__" ], current_scope , symbol_name )
511511
512- # Note: cls.__fields__: dict[str, pydantic.fields.ModelField]
512+ # Note: cls.__fields__: dict[str, pydantic.v1. fields.ModelField]
513513 for field_name , field_model in cls .__fields__ .items ():
514514 field_value = values .get (field_name , None )
515515 if field_value is None :
@@ -519,11 +519,11 @@ def _collect_variable_definitions( # noqa: C901 (suppress: too complex)
519519 # Literals cannot define variables, so skip this field.
520520 continue
521521
522- if field_model .shape == pydantic .fields .SHAPE_SINGLETON :
522+ if field_model .shape == pydantic .v1 . fields .SHAPE_SINGLETON :
523523 result = _collect_singleton (field_model , field_value , current_scope , symbol_prefix )
524524 if result :
525525 symbols [field_name ] = result
526- elif field_model .shape == pydantic .fields .SHAPE_LIST :
526+ elif field_model .shape == pydantic .v1 . fields .SHAPE_LIST :
527527 # If the shape expects a list, but the value isn't one then we have a validation error.
528528 # The error will get flagged by subsequent passes of the model validation.
529529 if not isinstance (field_value , list ):
@@ -535,7 +535,7 @@ def _collect_variable_definitions( # noqa: C901 (suppress: too complex)
535535 result = _collect_singleton (item_model , item , current_scope , symbol_prefix )
536536 if result :
537537 symbols [field_name ].update_self (result )
538- elif field_model .shape == pydantic .fields .SHAPE_DICT :
538+ elif field_model .shape == pydantic .v1 . fields .SHAPE_DICT :
539539 # dict[] fields can't define symbols.
540540 continue
541541 else :
@@ -566,7 +566,7 @@ def _add_symbol(into: ScopedSymtabs, scope: ResolutionScope, symbol_name: str) -
566566
567567
568568def _collect_singleton (
569- model : pydantic .fields .ModelField ,
569+ model : pydantic .v1 . fields .ModelField ,
570570 value : Any ,
571571 current_scope : ResolutionScope ,
572572 symbol_prefix : str ,
0 commit comments