Skip to content

Conversation

@sevaho
Copy link
Contributor

@sevaho sevaho commented Feb 26, 2025

Add support for pydantic v2

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 26, 2025

Reviewer's Guide by Sourcery

This pull request introduces a compatibility layer to support both Pydantic v1 and v2. It includes changes to model definitions, field creation, and schema generation to accommodate the differences between the two Pydantic versions. The changes ensure that the application can function correctly with either Pydantic version.

Updated class diagram for ModelField

classDiagram
    class ModelField {
        +name: str
        +type_: Any
        +required: bool
        +default: Any
        +alias: str
        +field_info: FieldInfo
    }
    note for ModelField "This class has been updated to be compatible with both Pydantic v1 and v2. The attributes and methods have been modified to reflect the changes in Pydantic's API."
Loading

Class diagram for GenerateJsonSchema

classDiagram
    class GenerateJsonSchema {
        +ref_template: str
    }
    note for GenerateJsonSchema "This class is used to generate JSON schemas for Pydantic models. It has been updated to be compatible with both Pydantic v1 and v2."
Loading

File-Level Changes

Change Details Files
Add compatibility layer for Pydantic v1 and v2.
  • Introduced a _compat.py file to handle differences between Pydantic versions.
  • Added conditional imports and type definitions based on the Pydantic version.
  • Created compatibility functions for model name mapping.
  • Added RootModel to _compat.py for pydantic v1 support.
natsapi/_compat.py
natsapi/utils.py
natsapi/asyncapi/utils.py
natsapi/encoders.py
natsapi/client/config.py
tests/test_jsonable_encoder.py
Update default values for optional fields in AsyncAPI models.
  • Set default values to None for optional fields in asyncapi/models.py.
  • This ensures that these fields are not required during validation if not provided.
natsapi/asyncapi/models.py
Refactor get_asyncapi to use compat model name map and schema generation.
  • Replaced get_model_name_map with get_compat_model_name_map in get_asyncapi.
  • Utilized GenerateJsonSchema for schema generation.
  • Modified how definitions are obtained using get_definitions.
  • Updated references to pub parameters to use __name__ instead of relying on model_name_map.
natsapi/asyncapi/utils.py
Adjust field creation in create_field to handle Pydantic v2.
  • Modified create_field to conditionally set field_info based on the Pydantic version.
  • Added kwargs to handle differences between pydantic versions.
natsapi/utils.py
Update jsonable_encoder to use model_config for json_encoders in pydantic v2.
  • Modified jsonable_encoder to access json_encoders from model_config when using Pydantic v2.
natsapi/encoders.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

We've reviewed this pull request using the Sourcery rules engine. If you would also like our AI-powered code review then let us know.

@sevaho sevaho changed the title Add a compat file as in fastapi to support both pydantic v1 and v2 Add support for both pydantic v1 and v2 Mar 3, 2025
@sevaho sevaho force-pushed the feature/pydantic-v2-release branch from 8010716 to 7e3715a Compare March 4, 2025 08:34
@sevaho sevaho merged commit 8327ec2 into master Mar 4, 2025
10 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