Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support constrained lists #75

Merged
merged 4 commits into from
Dec 21, 2022
Merged

✨ Support constrained lists #75

merged 4 commits into from
Dec 21, 2022

Conversation

yezz123
Copy link
Owner

@yezz123 yezz123 commented Dec 21, 2022

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:

from pydantic import ConstrainedList

int_list = ConstrainedList(int, min_items=3, max_items=5)

This creates a ConstrainedList object called int_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:

from pydantic import ConstrainedList

class Employee:
    def __init__(self, name: str, salary: int):
        self.name = name
        self.salary = salary

employee_list = ConstrainedList(Employee)

This creates a ConstrainedList object called employee_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, and unique_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 the min_items and max_items attributes of the model_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 of pydantic.types.ConstrainedList. If it is, it sets the list_types variable to a tuple of the item types of the ConstrainedList object. If it is not, it sets list_types to the tuple of argument types of type_.

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, the model_field object, and the use_default_values and optionals_use_none arguments. If the unique_items attribute of the model_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.

@yezz123 yezz123 added enhancement New feature or request Python labels Dec 21, 2022
@yezz123 yezz123 self-assigned this Dec 21, 2022
@codecov-commenter
Copy link

codecov-commenter commented Dec 21, 2022

Codecov Report

Base: 100.00% // Head: 99.79% // Decreases project coverage by -0.20% ⚠️

Coverage data is based on head (d98e579) compared to base (34ec025).
Patch coverage: 92.85% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff             @@
##              main      #75      +/-   ##
===========================================
- Coverage   100.00%   99.79%   -0.21%     
===========================================
  Files           23       23              
  Lines          944      966      +22     
===========================================
+ Hits           944      964      +20     
- Misses           0        2       +2     
Impacted Files Coverage Δ
ormdantic/generator/_lazy.py 97.10% <88.23%> (-2.90%) ⬇️
ormdantic/handler/__init__.py 100.00% <100.00%> (ø)
ormdantic/handler/random.py 100.00% <100.00%> (ø)
tests/test_generator.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@yezz123 yezz123 merged commit 75286cd into main Dec 21, 2022
@yezz123 yezz123 deleted the ref/generator branch December 21, 2022 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants