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

Fideslang Pydantic V2 Upgrade #11

Merged
merged 49 commits into from
Aug 20, 2024
Merged

Fideslang Pydantic V2 Upgrade #11

merged 49 commits into from
Aug 20, 2024

Conversation

pattisdr
Copy link

@pattisdr pattisdr commented Jun 15, 2024

Closes #PROD-2263

Pydantic 1 -> 2 Migration Guide: link

Replaces: IABTechLab#160 and #9

Description Of Changes

  • Upgrades Pydantic for V2 support and removes support for Pydantic V1 - supporting Pydantic Versions 2.3.0 - 2.7.1
  • Removes Python 3.8 from supported versions

Code Changes

  • Deprecatedparse_obj -> model_validate
  • Replaced validators with field_validators, allow_reuse keywords deleted
  • Replaced root_validators with model_validators
  • For field validators, other fields are no longer available under "values" - instead it is "info.data"
  • Swapped always=True keyword args to add a validate=True arg on the respective fields
  • Existing fields with Optional were given a default value of None, since Optional only means "not required" in V2 if there's a default
  • Replaced the defined class Configs defined in the BaseModel namespace with model_configs
  • orm_mode renamed to from_attributes
  • Removed FidesVersion concept entirely since it wasn't being used as a Pydantic model. Just using Version is sufficient.
  • Deprecated update_forward_refs -> model_rebuild
  • Custom types FidesKeys and FidesCollectionKeys were overhauled in how they are defined in V2
  • Coercing integers on the Fides Model to strings if applicable. For example, this behavior used to happen automatically if an organization_fides_key was 1 for example, it was coerced to "1". Added coerce_numbers_to_str=True to the model.
  • Validation of system privacy declarations egress/ingresses had to be rewritten due to the removal of the each_item keyword.
  • Deprecated __fields_set__ replaced with model_fields_set
  • Lots more assertions on the specific error messages added to the tests to verify we're testing the right thing. Tests on behavior generally didn't change

Steps to Confirm

  • Confirmed through regression testing of Fides and Fidesplus

Pre-Merge Checklist

  • All CI Pipelines Succeeded
  • Documentation Updated
  • Issue Requirements are Met
  • Relevant Follow-Up Issues Created
  • Update CHANGELOG.md

pattisdr and others added 30 commits June 14, 2024 14:23
- Temporarily install bump-pydantic to help with upgrade
- Temporarily install eval_type_backport to get around issues with 'bool | None' that don't appear to be in this codebase.
- Use StringConstraints instead of ConstrainedStr on FidesKey and FidesCollectionKey
…m_mode" has been renamed to "from_attributes"

Affects: FidesModel, Cookies, Evaluation, PrivacyDeclaration, and System.
FidesMeta.valid_data_type. Dataset.valid_meta, Dataflow.verify_type_is_flowable replaced here.
… couldn't make automatically.

Includes: DefaultModel.validate_verion_added, DefaultModel.validate_version_deprecated, DatasetField.validate_object_fields
… a root_validator which is better when performing validation using multiple field values
…ld_validator into a model_validator to better be able to work with privacy declaration and system ingresses and egresses together, since "each_item" has been deprecated.
…st means that None is an allowed value, so you need to specify a default of None to match existing behavior.
…ted keyword argument. I think skip_on_failure=true just didn’t call the root_validator if other validators failed. I am going to remove this instance.
…n't have default values of None, and are often called in a optional context.
…to better match what we had before, so the "values" are a dictionary and not an instance of an object. This is to avoid a lot of errors like "DataFlow" object is not subscriptable that I newly created.

- Update reusable validators to be f"field_validators" instead of "validators".
- Remove allow_reuse=True from all field_validators. This is not a supported keyword and can be removed entirely.
Set validate=default on the fields that were previously setting always=True, as this keyword is also not supported for field validators. This mimics the behavior of always=True in v1.
- Data type in these field validators is a ValidationInfo not a dictionary. The data can be accessed via values.data.get(""_
- Types are not coerced any longer. We were passing in an organization_fides_key of integer 1, but it no longer gets coerced to a string. It either needs to be passed in as a string, or we need to update FidesKey itself to coerce it.
- Return all elements has other validation so it should be null if it's not a list field
- Update the definitions of FidesKey and FidesCollectionKey to be subclassing StringConstraints to try to maintain original validation. Override __get_pydantic_core_schema__
-  Due to other arrangements around when validation is running (mode=before or after) egress key/ingress key does not necessarily exist when validating privacy declaration egress and egress.
…o be when validating. Similarly, likely not defining FidesKey and FidesCollectionKey in the proper ways.

- Have FidesKey and FidesCollectionKey extend str - instead of StringConstraint - still defining a __get_pydantic_core_schema__ to override how it gets validated
parse_obj -> model_validate
update_forward_refs -> model_rebuild
__fields_set__ -> model_fields_set
construct -> model_construct
- Bump xenon and pre-commit in dev-requirements
Also, throw proper error if version is not a ValidVersion.
@pattisdr pattisdr mentioned this pull request Jun 16, 2024
9 tasks
… still work.

- Place self-reference check before matching parent check when validating data categories and data uses because a self-reference would actually fail in the matching parent check, but the error is less clear there.
- Also fix typo in existing error message
…nded to test.

- Assert exception thrown in test for duplicate collections errors. Fix integer dataset fides keys because this was failing first and preventing the duplicate entries found error from being thrown
- Add more assertions on the error message itself to ensure we're testing the right point of failure
Where I'm using model_validator with after model, adjust so it's an instance method and is receiving an instance and returning the instance
…ten the error message I'm checking to get the gist of the check.
src/fideslang/default_taxonomy/utils.py Show resolved Hide resolved
requirements.txt Show resolved Hide resolved
src/fideslang/models.py Show resolved Hide resolved
.github/workflows/pr_checks.yml Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
src/fideslang/validation.py Show resolved Hide resolved
src/fideslang/validation.py Show resolved Hide resolved
tests/conftest.py Show resolved Hide resolved
tests/fideslang/test_relationships.py Show resolved Hide resolved
tests/fideslang/test_validation.py Outdated Show resolved Hide resolved
… given recurring issues where sometimes the data that is validated is a dictionary but it can also be the instance itself.
… when it is validated. Use this for System.privacy_policy and System.legitimate_interest_disclosure_url.
…serializer warnings. We are intentionally serializing these as strings.
@pattisdr pattisdr changed the title [Draft] Fideslang Pydantic V2 Upgrade Fideslang Pydantic V2 Upgrade Jul 13, 2024
@pattisdr pattisdr requested a review from adamsachs July 15, 2024 14:46
@pattisdr pattisdr force-pushed the fideslang_pydantic_v2_upgrade branch from 3fbff01 to b27bf03 Compare July 17, 2024 05:10
@pattisdr pattisdr marked this pull request as ready for review July 24, 2024 00:49
@pattisdr pattisdr requested a review from galvana July 26, 2024 14:43
Copy link

@galvana galvana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the self-review, it really helped understand why the changes were made. This also served as a nice introduction to the types of changes you made in Fides and Fidesplus.

@pattisdr pattisdr merged commit 6c0528b into main Aug 20, 2024
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants