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

Pydantic v2 #163

Closed
wants to merge 25 commits into from
Closed

Pydantic v2 #163

wants to merge 25 commits into from

Conversation

felipao-mx
Copy link
Member

@felipao-mx felipao-mx commented Aug 24, 2024

here is a proposal to make clabe-python compatible with both Pydantic v1.10.x+ and v2.x.x. The main idea is to support projects using Pydantic v2. At the same time, projects using Pydantic v1 can make the transition smoothly.

Key features:

  • When Pydantic v1 is installed in your project, it will use the older Clabe class with legacy validators.
  • When Pydantic v2 is installed in your project, it will use the new Clabe class with Pydantic v2 features. This takes advantage of pure pydantic-core validators and improve performance.
  • Remove support for Python 3.7.
  • Adoption of PDM as the package manager.

Disclaimer:
Trying to mix Pydantic V1 validator in Pydantic V2 BaseModel subclasses (or viceversa) will give you weird issues so it is not recommended. You should migrate your old Pydantic v1 validators to V2.

Summary by CodeRabbit

  • New Features

    • Introduced a Clabe class for validating CLABE numbers, enhancing functionality and usability.
    • Added support for multiple Pydantic versions, allowing flexibility in integration.
  • Documentation

    • Updated README.md to include installation instructions for PDM and enhanced usage examples.
  • Tests

    • Improved test suite with parameterized testing for invalid CLABE inputs and compatibility checks against different Pydantic versions.
  • Chores

    • Updated project configuration files for modern dependency management using PDM.
    • Revised .gitignore to exclude PDM-related files and improve project cleanliness.
  • Versioning

    • Updated package version to 1.3.0.dev0, indicating upcoming enhancements and features.

Copy link

coderabbitai bot commented Aug 24, 2024

Walkthrough

The project underwent significant updates, primarily transitioning to the PDM (Python Development Master) for package management across various GitHub Actions workflows. Enhancements were made to the testing framework, with the introduction of a new Clabe class for managing CLABE numbers using Pydantic integration. The README was modified for clarity, and configuration files were updated to reflect modern Python practices and improved dependency management.

Changes

File(s) Change Summary
.github/workflows/licenses.yml, .github/workflows/release.yml, .github/workflows/test.yml Updated to use PDM for package management, replaced pip commands, and upgraded action versions for improved performance.
.gitignore Added entries for PDM-related files and macOS system files to keep the repository clean.
Makefile Transitioned to using PDM for virtual environment management and dependency installation, enhancing build processes.
README.md Clarified CLABE library usage, updated installation instructions, and improved examples for better usability.
clabe/__init__.py, clabe/types/__init__.py, clabe/types/clabes.py, clabe/types/clabes_legacy/clabes.py Adjusted import statements and added new Clabe class for CLABE number validation and integration with Pydantic.
clabe/utils.py Introduced a utility function to check the Pydantic version for compatibility.
clabe/version.py Updated the version from 1.2.14 to 1.3.0.dev0, signaling a new development stage.
pyproject.toml Established project configuration with dependencies, build system, and testing settings for streamlined development.
requirements-legacy.txt Introduced legacy requirements for Pydantic, ensuring backward compatibility.
tests/test_types.py Enhanced testing suite with parameterized tests, improved structure, and compatibility checks for Pydantic versions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant GitHub Actions
    participant PDM
    participant Pydantic
    participant CLABE

    User->>GitHub Actions: Trigger release
    GitHub Actions->>PDM: Install dependencies
    PDM->>Pydantic: Validate input
    Pydantic->>CLABE: Handle CLABE number
    CLABE-->>Pydantic: Return results
    GitHub Actions-->>User: Complete release
Loading

🐇 In the garden where changes bloom,
I hop with joy, dispelling gloom.
PDM now leads the way, oh so bright,
With CLABE to guide us, all feels right!
Dependencies dance, tests in a row,
With every new version, our project will grow! 🌷


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 13

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 7f1b861 and 96dc177.

Files ignored due to path filters (2)
  • pdm-legacy.lock is excluded by !**/*.lock
  • pdm.lock is excluded by !**/*.lock
Files selected for processing (15)
  • .github/workflows/licenses.yml (1 hunks)
  • .github/workflows/release.yml (1 hunks)
  • .github/workflows/test.yml (1 hunks)
  • .gitignore (1 hunks)
  • Makefile (2 hunks)
  • README.md (3 hunks)
  • clabe/init.py (1 hunks)
  • clabe/types/init.py (1 hunks)
  • clabe/types/clabes.py (1 hunks)
  • clabe/types/clabes_legacy/clabes.py (3 hunks)
  • clabe/utils.py (1 hunks)
  • clabe/version.py (1 hunks)
  • pyproject.toml (1 hunks)
  • requirements-legacy.txt (1 hunks)
  • tests/test_types.py (2 hunks)
Additional context used
Ruff
clabe/utils.py

4-4: Missing return type annotation for public function is_pydantic_v1

(ANN201)

clabe/types/clabes_legacy/clabes.py

32-32: Missing type annotation for self in method

(ANN101)


60-60: Missing type annotation for self in method

(ANN101)

clabe/types/clabes.py

14-14: Missing type annotation for self in method

(ANN101)


20-20: Missing type annotation for self in method

(ANN101)


25-25: Missing type annotation for cls in classmethod

(ANN102)


25-25: Trailing comma missing

Add trailing comma

(COM812)


38-38: Missing type annotation for cls in classmethod

(ANN102)


38-38: Trailing comma missing

Add trailing comma

(COM812)


50-50: Missing type annotation for cls in classmethod

(ANN102)


53-53: Use key not in dict instead of key not in dict.keys()

Remove .keys()

(SIM118)


55-55: Trailing comma missing

Add trailing comma

(COM812)


59-59: Trailing comma missing

Add trailing comma

(COM812)

tests/test_types.py

24-24: Wrong type passed to first argument of @pytest.mark.parametrize; expected tuple

Use a tuple for the first argument

(PT006)


30-30: Trailing comma missing

Add trailing comma

(COM812)


38-38: Trailing comma missing

Add trailing comma

(COM812)


46-46: Trailing comma missing

Add trailing comma

(COM812)


54-54: Trailing comma missing

Add trailing comma

(COM812)

actionlint
.github/workflows/licenses.yml

24-24: shellcheck reported issue in this script: SC2086:info:1:55: Double quote to prevent globbing and word splitting

(shellcheck)


24-24: shellcheck reported issue in this script: SC2086:info:1:70: Double quote to prevent globbing and word splitting

(shellcheck)

Markdownlint
README.md

9-9: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


9-9: null
Bare URL used

(MD034, no-bare-urls)


34-34: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


35-35: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


36-36: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


44-44: null
Fenced code blocks should be surrounded by blank lines

(MD031, blanks-around-fences)

Additional comments not posted (25)
requirements-legacy.txt (1)

1-1: Approved: Dependency specification for Pydantic v1.10.

This line correctly specifies the dependency on Pydantic version 1.10, aligning with the PR's objectives to support legacy versions.

clabe/version.py (1)

1-1: Approved: Version update to 1.3.0.dev0.

The version update to 1.3.0.dev0 accurately reflects the transition to a new development phase with significant changes or enhancements.

clabe/types/__init__.py (1)

1-8: Approved conditional import logic for Pydantic compatibility.

The conditional import based on the Pydantic version is a smart approach to maintain compatibility with both versions of Pydantic. This ensures that the library can be used flexibly across different environments.

However, the use of type: ignore might suppress important type-checking errors.

Consider verifying the type-checking behavior to ensure that it does not hide any critical issues.

clabe/__init__.py (1)

12-12: Approved change to absolute import for clarity.

Changing from a relative import to an absolute import (from clabe.types import Clabe) enhances the clarity and maintainability of the import paths. This is a good practice, especially in a modular project structure.

.github/workflows/release.yml (1)

3-17: Approved workflow changes for modernized release process.

The changes to the GitHub Actions workflow, including the shift from a push to a release trigger and the adoption of PDM (pdm build), align well with modern Python practices and the project's needs. These modifications should enhance the reliability and specificity of the release process.

However, it's crucial to verify that the new workflow triggers correctly and functions as expected in the release context.

Consider running test releases or dry runs to ensure that the workflow behaves as intended.

.gitignore (2)

122-122: Approved addition of .pdm-python.

This change is appropriate as it helps keep the repository clean by ignoring the PDM virtual environment directory.


123-123: Approved addition of .pdm-build/.

Excluding build artifacts from version control is a best practice, and this entry ensures that PDM build outputs are not tracked.

pyproject.toml (1)

1-61: Approved pyproject.toml setup.

The file is well-structured and correctly sets up the project with modern Python practices, including dependency management and configurations for various tools.

.github/workflows/licenses.yml (2)

18-18: Approved upgrade to actions/checkout@v4.

Upgrading to a newer version of this action is beneficial for performance and stability.


20-20: Approved transition to pdm-project/setup-pdm@v4.

This change correctly reflects the project's shift to using PDM for Python package management, aligning with modern practices.

.github/workflows/test.yml (6)

9-9: Update to actions/checkout@v4 approved.

This update is expected to bring performance improvements and new features. Ensure to verify for any potential breaking changes.


11-11: Transition to pdm-project/setup-pdm@v4 approved.

This change aligns with the shift towards using PDM for Python environment management. Verify that the environment is correctly set up and integrates well with the project's requirements.


15-17: Updated dependency installation commands approved.

The new two-step process for setting the Python environment and installing dependencies should enhance the robustness of the setup. Verify that all dependencies are correctly installed and function as intended across different Python versions.


25-25: Updated Python version matrix approved.

Expanding the matrix to include Python 3.8, 3.9, and 3.10 is a positive step towards ensuring compatibility with newer Python versions. Verify that the project functions correctly across these versions.


37-37: Consolidation of test commands under make test approved.

This change simplifies the testing process and ensures consistency in command execution. Verify that the make test command in the Makefile is correctly configured to handle both testing and coverage reporting.

Also applies to: 52-52


54-56: Update to codecov/codecov-action@v4 approved.

The update enhances security and ensures correct uploading of coverage reports. Verify that the coverage reporting process functions as expected with the new action version and token configuration.

Makefile (2)

34-41: Approve the changes to the format and lint targets.

The updates to run code formatting and linting within the pdm context are correctly implemented and promote consistency.


63-63: Approve the simplification of the .PHONY declaration.

The changes reflect the consolidation of the installation and testing processes under the new pdm commands.

README.md (4)

Line range hint 1-8: Introduction and Basic Usage sections are well-documented.

These sections clearly explain the purpose of the library and provide straightforward examples for basic operations. The formatting and presentation are consistent and user-friendly.

Also applies to: 58-85

Tools
Markdownlint

9-9: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


9-9: null
Bare URL used

(MD034, no-bare-urls)


87-115: Excellent integration of the Clabe class with Pydantic.

The introduction of the Clabe class as a custom type for Pydantic models is a significant enhancement. The example provided is clear, demonstrating both successful and error scenarios, which greatly aids in understanding how to use the class effectively.


18-18: Updated Python version requirement is clear and justified.

Specifying Python 3.8 or higher aligns with modern Python practices and the library's needs. This update is clearly communicated.


32-53: Installation and testing instructions are comprehensive and clear.

The detailed steps for using PDM and setting up virtual environments for different Pydantic versions are well-explained. The testing instructions are straightforward, ensuring that developers can easily run and verify tests.

Tools
Markdownlint

34-34: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


35-35: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


36-36: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


44-44: null
Fenced code blocks should be surrounded by blank lines

(MD031, blanks-around-fences)

clabe/types/clabes_legacy/clabes.py (2)

32-32: Approved: Explicit return type annotation for __init__.

The addition of -> None is a standard practice for constructors in Python, enhancing readability and type clarity.

The static analysis tool's flag for a missing type annotation for self (ANN101) is not applicable in Python, as self does not require an explicit type annotation.

Tools
Ruff

32-32: Missing type annotation for self in method

(ANN101)


60-60: Approved: Return type annotation for bank_code.

Explicitly defining the return type as str improves type safety and clarity.

The static analysis tool's flag for a missing type annotation for self (ANN101) is also not applicable here, as self does not require an explicit type annotation in property methods.

Tools
Ruff

60-60: Missing type annotation for self in method

(ANN101)

tests/test_types.py (1)

77-90: Approved: Comprehensive test for JSON schema validation.

The test is well-structured and correctly uses TypeAdapter to generate and verify the JSON schema of the Clabe model. Skipping this test for Pydantic v1 is appropriate and ensures compatibility.

clabe/utils.py Outdated Show resolved Hide resolved
.github/workflows/licenses.yml Show resolved Hide resolved
clabe/types/clabes.py Show resolved Hide resolved
clabe/types/clabes.py Show resolved Hide resolved
clabe/types/clabes.py Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
tests/test_types.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Aug 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (22b0c68) to head (63c6bd0).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #163   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         9    +3     
  Lines           85       125   +40     
=========================================
+ Hits            85       125   +40     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
clabe/__init__.py 100.00% <100.00%> (ø)
clabe/types/__init__.py 100.00% <100.00%> (ø)
clabe/types/clabes.py 100.00% <100.00%> (ø)
clabe/types/clabes_legacy/clabes.py 100.00% <100.00%> (ø)
clabe/types/clabes_legacy/errors.py 100.00% <ø> (ø)
clabe/utils.py 100.00% <100.00%> (ø)
clabe/version.py 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f1b861...63c6bd0. Read the comment docs.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 96dc177 and 63c6bd0.

Files selected for processing (5)
  • Makefile (2 hunks)
  • README.md (3 hunks)
  • clabe/types/clabes.py (1 hunks)
  • clabe/utils.py (1 hunks)
  • tests/test_types.py (2 hunks)
Additional context used
Ruff
clabe/types/clabes.py

14-14: Missing type annotation for self in method

(ANN101)


20-20: Missing type annotation for self in method

(ANN101)


25-25: Missing type annotation for cls in classmethod

(ANN102)


40-40: Missing type annotation for cls in classmethod

(ANN102)


54-54: Missing type annotation for cls in classmethod

(ANN102)


59-59: Trailing comma missing

Add trailing comma

(COM812)


63-63: Trailing comma missing

Add trailing comma

(COM812)

tests/test_types.py

24-24: Wrong type passed to first argument of @pytest.mark.parametrize; expected tuple

Use a tuple for the first argument

(PT006)


57-57: Trailing comma missing

Add trailing comma

(COM812)

Markdownlint
README.md

35-35: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


36-36: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

Additional comments not posted (12)
clabe/utils.py (1)

4-5: Approved: Function to check Pydantic version.

The function is_pydantic_v1 correctly checks if the installed Pydantic version starts with '1.' and now includes the return type annotation as suggested in the previous review.

clabe/types/clabes.py (1)

14-65: Approved: Implementation of the Clabe class.

The Clabe class is well implemented with methods correctly handling CLABE validations. Type annotations and other improvements suggested in previous reviews have been correctly applied.

Tools
Ruff

14-14: Missing type annotation for self in method

(ANN101)


20-20: Missing type annotation for self in method

(ANN101)


25-25: Missing type annotation for cls in classmethod

(ANN102)


40-40: Missing type annotation for cls in classmethod

(ANN102)


54-54: Missing type annotation for cls in classmethod

(ANN102)


59-59: Trailing comma missing

Add trailing comma

(COM812)


63-63: Trailing comma missing

Add trailing comma

(COM812)

README.md (8)

9-9: Update the link to use Markdown link syntax.

The link to the CLABE Wikipedia page is correctly formatted using Markdown link syntax.


11-14: Introduction of the Clabe class for Pydantic integration.

The documentation clearly explains the purpose and usage of the new Clabe class, highlighting its compatibility with both Pydantic v1.10.x and v2.x.x.


18-18: Update Python version requirement.

The documentation now correctly states that Python 3.8 or higher is required, aligning with the removal of support for Python 3.7.


34-37: Clarify PDM installation instructions.

The instructions for installing PDM are clear and include a link to the official guide, which is helpful for users unfamiliar with PDM.

Tools
Markdownlint

35-35: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


36-36: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


41-43: Detail on creating virtual environments for different Pydantic versions.

The documentation provides clear instructions on how to set up two separate virtual environments for Pydantic versions 1.x.x and 2.x.x, which is crucial for testing compatibility.


46-46: Ensure proper spacing around fenced code blocks.

The spacing around the fenced code block for the make install command is correctly formatted.


54-54: Correct command format in testing section.

The command to run tests is correctly formatted and includes the necessary prefix $ to indicate a shell command.


88-116: Detailed example of using the Clabe type in a Pydantic model.

The example provided demonstrates how to use the Clabe type within a Pydantic model, including handling validation errors. This is an excellent addition for users to understand the practical application of the library.

tests/test_types.py (2)

73-77: Implementation of the test_invalid_clabe function.

The function correctly tests various invalid CLABE inputs against their expected error messages using parameterized tests. This is a significant enhancement to the test suite's clarity and breadth.


80-96: Add new function for JSON schema validation.

The test_get_json_schema function is correctly implemented to validate the JSON schema of the Clabe model, ensuring it adheres to the defined standards. This is a crucial addition for maintaining the integrity of the model's validation logic.

Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Show resolved Hide resolved
tests/test_types.py Show resolved Hide resolved
@felipao-mx felipao-mx requested a review from matin August 25, 2024 00:17
@felipao-mx
Copy link
Member Author

closing this PR since these changes have been implemented in #171 🎯

@felipao-mx felipao-mx closed this Dec 28, 2024
@felipao-mx felipao-mx deleted the pydanticv2 branch December 28, 2024 01:06
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.

1 participant