Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Concept
In Python, a constrained list is a list that is restricted to contain only elements of a certain type or set of types. The Python package Pydantic includes a
ConstrainedList
class that can be used to define constrained lists in your code.Here is an example of how you might use the
ConstrainedList
class to define a constrained list of integers:This creates a
ConstrainedList
object calledint_list
that can only contain integers and must have at least 3 items and at most 5 items.You can also use the
ConstrainedList
class to define a constrained list of elements that are instances of a particular class or that are of a particular subclass:This creates a
ConstrainedList
object calledemployee_list
that can only contain instances of the Employee class.The ConstrainedList class includes several other optional parameters that you can use to further constrain the elements of the list, such as
min_items
,max_items
, andunique_items
. You can find more information about the ConstrainedList class in the Pydantic documentation.Explanation
The Feature starts by determining the target length of the list by calling a helper function called
GetTargetLength
, passing in themin_items
andmax_items
attributes of themodel_field
object as arguments. It then initializes an empty list called items.The function then checks if
type_
is a ConstrainedList object by checking if it is a subclass ofpydantic.types.ConstrainedList
. If it is, it sets thelist_types
variable to a tuple of the item types of the ConstrainedList object. If it is not, it setslist_types
to the tuple of argument types oftype_
.The function then enters a loop that continues until the length of items is equal to the target length. Within the loop, it iterates over the items in
list_types
, and for each item, it calls a helper function called_get_value
, passing in the item, themodel_field
object, and theuse_default_values
andoptionals_use_none
arguments. If theunique_items
attribute of themodel_field
object is True and the value returned by_get_value
is already in the items list, it continues to the next iteration of the loop. Otherwise, it appends the value to the items list.After the loop has been completed, the function returns the items list.