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

feat: add model validation for types #708

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open

Conversation

mvadari
Copy link
Collaborator

@mvadari mvadari commented May 17, 2024

High Level Overview of Change

This PR adds additional validation for models to also check parameter types and ensure they match what is expected. It also fixes issues that were uncovered via adding this additional checking.

Context of Change

It's a common annoyance and improves the user experience.

Type of Change

  • New feature (non-breaking change which adds functionality)

Did you update CHANGELOG.md?

  • Yes

Test Plan

Added tests, and added a few additional tests to improve code coverage in the base_model.py file. CI passes.

@mvadari mvadari force-pushed the model-type-validation branch from cd1e0d0 to bc3e043 Compare May 17, 2024 20:35
@mvadari mvadari force-pushed the model-type-validation branch from bc3e043 to 310363e Compare May 17, 2024 20:39
xrpl/models/base_model.py Show resolved Hide resolved
xrpl/models/base_model.py Outdated Show resolved Hide resolved
xrpl/models/transactions/transaction.py Show resolved Hide resolved
snippets/paths.py Outdated Show resolved Hide resolved
xrpl/models/base_model.py Outdated Show resolved Hide resolved
@mvadari mvadari requested a review from ckeshava June 5, 2024 20:20
@ckeshava
Copy link
Collaborator

ckeshava commented Jun 5, 2024

thanks, this is very interesting work. I feel we could have used this library to enforce type checks, had it been better maintained: https://pypi.org/project/enforce-typing/

@mvadari mvadari linked an issue Sep 17, 2024 that may be closed by this pull request
@mvadari
Copy link
Collaborator Author

mvadari commented Sep 19, 2024

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

Actions performed

Full review triggered.

Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

Warning

Rate limit exceeded

@mvadari has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 27 minutes and 34 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between a6807ac and 272742c.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • xrpl/models/requests/ledger_entry.py (1 hunks)

Walkthrough

The recent changes to the project include updates to the CHANGELOG.md to document the addition of the include_deleted parameter in the ledger_entry request. Notable breaking changes include the removal of Python 3.7 support and the change of the default API version to rippled API v2. Enhancements have been made to the LedgerEntry class by expanding type annotations for xchain_claim_id and xchain_create_account_claim_id to accept integers alongside existing types. Various deprecated parameters have also been removed from the ledger request model.

Changes

Files Change Summary
CHANGELOG.md Updated to reflect the addition of include_deleted parameter, removal of Python 3.7 support, and changes in API version.
xrpl/models/requests/ledger_entry.py Updated type annotations for xchain_claim_id and xchain_create_account_claim_id to include int.
xrpl/models/base_model.py Added _check_type() method for enhanced type validation and modified _get_errors() method to utilize it.
xrpl/models/transactions/oracle_set.py Updated OracleSet model with additional validation checks in _get_errors() method.
xrpl/models/transactions/transaction.py Modified _get_errors() method, added error handling in has_flag(), and updated from_xrpl() for deliver_max.

Possibly related PRs

  • Add include_deleted param to ledger_entry API #721: This PR adds the include_deleted parameter to the ledger_entry API, which is directly related to the changes made in the main PR regarding the same parameter.
  • Update CHANGELOG.md #748: This PR updates the CHANGELOG.md to reflect the addition of the include_deleted parameter and other significant changes, aligning with the updates documented in the main PR.

Suggested reviewers

  • ckeshava
  • shawnxie999

🐇 "With changes made, we hop along,
New fields and types, where we belong.
A ledger bright, with entries clear,
In every line, we spread the cheer!
So let’s embrace this code we share,
For in our project, we show we care!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@mvadari
Copy link
Collaborator Author

mvadari commented Sep 19, 2024

@coderabbitai full review

Copy link
Contributor

coderabbitai bot commented Sep 19, 2024

Actions performed

Full review triggered.

Copy link
Contributor

@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: 2

Outside diff range and nitpick comments (2)
xrpl/models/base_model.py (2)

294-351: Consider adding a docstring to the _check_type method for clarity.

Adding a docstring to _check_type would improve code readability and maintainability by documenting its purpose, parameters, and return value.


307-307: Clarify or remove the comment regarding the mypy issue.

The comment # unsure what the problem with mypy is here may not be helpful. If there's a mypy warning, consider either resolving it or providing a more detailed explanation to assist future maintainers.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 17aad31 and c745f69.

Files selected for processing (12)
  • CHANGELOG.md (1 hunks)
  • snippets/paths.py (1 hunks)
  • tests/unit/models/test_base_model.py (18 hunks)
  • tests/unit/models/transactions/test_check_cash.py (1 hunks)
  • tests/unit/models/transactions/test_oracle_set.py (1 hunks)
  • tests/unit/models/transactions/test_xchain_claim.py (1 hunks)
  • xrpl/models/base_model.py (2 hunks)
  • xrpl/models/requests/ledger_entry.py (1 hunks)
  • xrpl/models/transactions/oracle_set.py (2 hunks)
  • xrpl/models/transactions/pseudo_transactions/enable_amendment.py (2 hunks)
  • xrpl/models/transactions/transaction.py (2 hunks)
  • xrpl/utils/xrp_conversions.py (3 hunks)
Additional context used
Gitleaks
tests/unit/models/test_base_model.py

448-448: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)


471-471: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

(generic-api-key)

Additional comments not posted (22)
tests/unit/models/transactions/test_check_cash.py (1)

9-9: Verify and update other occurrences of _CHECK_ID in the codebase.

The change to the _CHECK_ID constant, moving from a numeric format to a hexadecimal string format, appears to be valid and aligns with the provided summary.

However, please ensure that any other occurrences of _CHECK_ID in the test files or the main codebase are updated to match this new format for consistency.

Run the following script to verify the usage of _CHECK_ID:

Verification successful

Verification complete: _CHECK_ID format change is consistent

The change to the _CHECK_ID constant, moving from a numeric format to a hexadecimal string format, has been consistently applied across the codebase. The new format is defined once and used in multiple locations without any instances of the old format remaining.

  • Definition: _CHECK_ID = "838766BA2B995C00744175F69A1B11E32C3DBC40E64801A4056FCBD657F57334"
  • Usage: Found in 4 other locations, likely in function calls or assignments

No further action is required regarding this change.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all occurrences of `_CHECK_ID` match the new hexadecimal string format.

# Test: Search for the `_CHECK_ID` usage. Expect: Only occurrences of the new format.
rg --type python -A 5 $'_CHECK_ID'

Length of output: 64


Script:

#!/bin/bash
# Description: Verify all occurrences of `_CHECK_ID` in Python files

# Find Python files and search for _CHECK_ID
fd '\.py$' --exec rg -n '_CHECK_ID' {} \;

# Search for the old numeric format (assuming it was a number like 19048)
echo "Searching for potential old numeric format:"
fd '\.py$' --exec rg -n '_CHECK_ID\s*=\s*\d+' {} \;

# Search for the new hexadecimal string format
echo "Searching for the new hexadecimal string format:"
fd '\.py$' --exec rg -n '_CHECK_ID\s*=\s*"[0-9A-F]{64}"' {} \;

Length of output: 683

snippets/paths.py (1)

2-2: The addition of a blank line at the beginning of the file is a trivial formatting change that does not affect the code's functionality or logic. No review comment is necessary.

tests/unit/models/transactions/test_xchain_claim.py (1)

71-71: LGTM!

The change from string to integer for the destination_tag parameter is correct and aligns with the expected input type. This improves the type safety and robustness of the test_successful_claim_destination_tag function.

xrpl/utils/xrp_conversions.py (2)

39-39: LGTM!

The change from type() to isinstance() for type checking is a good refactor. It improves code robustness and maintainability without altering the core functionality.


87-87: LGTM!

Similar to the previous function, the change from type() to isinstance() for type checking is a good refactor. It improves code robustness and maintainability without altering the core functionality.

xrpl/models/transactions/pseudo_transactions/enable_amendment.py (2)

5-5: LGTM!

The import statement is correct and necessary to support the expanded type definition of the flags attribute.


104-104: Verify the impact of the expanded flags type definition.

The change to the flags attribute type is logically correct and enhances the flexibility of the attribute. The default value and documentation comment remain valid.

However, this change may affect how the flags attribute is utilized throughout the codebase. Please ensure that:

  1. Existing code that interacts with the flags attribute has been updated to handle the new dictionary type.
  2. The expanded type definition is properly validated and handled in the relevant methods of the EnableAmendment class.

You can use the following script to search for potential areas that may require updates:

Verification successful

Verification complete: flags attribute change appears isolated

The expanded type definition for the flags attribute in the EnableAmendment class seems to have minimal impact on the codebase. Our analysis shows:

  1. No direct read or write access to the flags attribute was found outside the class definition.
  2. No method definitions were found that use flags as a parameter.
  3. The flags attribute is defined as Union[Dict[str, bool], int, List[int]] = 0, consistent with the original review comment.

The change appears to be isolated to the EnableAmendment class definition. However, to ensure complete verification:

  • Review any code that instantiates or processes EnableAmendment objects to confirm compatibility with the new type definition.
  • Verify that the expanded type aligns with the intended use of the flags attribute as described in the documentation.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `flags` attribute in the codebase.

# Test 1: Search for read access of the `flags` attribute. 
# Expect: Relevant code should handle the expanded type definition.
rg --type python -A 5 $'\.flags'

# Test 2: Search for write access to the `flags` attribute. 
# Expect: Relevant code should conform to the expanded type definition.
rg --type python -A 5 $'flags\s*='

Length of output: 131


Script:

#!/bin/bash
# Description: Verify the usage of the `flags` attribute in the codebase using ast-grep

# Test 1: Search for read access of the `flags` attribute
echo "Read access of flags attribute:"
ast-grep --lang python --pattern '$_.$_flags'

# Test 2: Search for write access to the `flags` attribute
echo "Write access to flags attribute:"
ast-grep --lang python --pattern '$_.flags = $_'

# Test 3: Search for method definitions that use the flags attribute as a parameter
echo "Method definitions with flags as parameter:"
ast-grep --lang python --pattern 'def $_($_, flags: $_, $_): $$$'

# Test 4: Search for the EnableAmendment class definition
echo "EnableAmendment class definition:"
ast-grep --lang python --pattern 'class EnableAmendment($_): $$$'

Length of output: 5906

xrpl/models/transactions/oracle_set.py (2)

23-26: LGTM!

Explicitly casting EPOCH_OFFSET to an integer improves type clarity and safety without altering the underlying value or logic.


149-149: LGTM!

Updating the error message to use consistent terminology improves clarity and readability without altering the underlying meaning.

xrpl/models/requests/ledger_entry.py (2)

259-259: LGTM!

The change to include int as an additional valid type for xchain_claim_id attribute in LedgerEntry class looks good. It aligns with the type annotation update in the XChainClaimID class and enhances the flexibility of the LedgerEntry class in handling different data types for claims.


261-261: LGTM!

The change to include int as an additional valid type for xchain_create_account_claim_id attribute in LedgerEntry class looks good. It aligns with the type annotation update in the XChainCreateAccountClaimID class and enhances the flexibility of the LedgerEntry class in handling different data types for account claim IDs.

tests/unit/models/transactions/test_oracle_set.py (1)

329-329: LGTM!

The updated error message improves clarity by using consistent terminology for the ripple epoch reference. This change enhances readability without altering the test's functionality.

CHANGELOG.md (1)

21-21: Excellent addition to model validation!

Checking parameter types as part of the validation process aligns perfectly with the PR objective. This enhancement can help catch type mismatches early, reducing potential runtime errors and improving overall data integrity.

xrpl/models/transactions/transaction.py (2)

Line range hint 267-267:


Line range hint 368-383: LGTM!

The added exception handling for invalid self.flags type enhances the method's reliability and error reporting.

tests/unit/models/test_base_model.py (7)

87-95: Effective test for invalid types in test_bad_type

The test correctly verifies that the Payment model raises an XRPLModelException when provided with incorrect types for the account, amount, and destination fields.


96-104: Validation of flags field type in test_bad_type_flags

This test appropriately checks that passing a string instead of an integer to the flags field results in an XRPLModelException, enhancing type safety.


106-114: test_bad_type_enum accurately tests invalid enum values

The test ensures that providing an invalid value to the subcommand enum in the PathFind model raises an XRPLModelException, which is essential for input validation.


425-437: Detection of nonexistent fields in test_nonexistent_field

This test correctly checks that the TrustSet model raises an XRPLModelException when an unexpected field (bad_field) is included, reinforcing model strictness.


440-440: Please address previous comment regarding clarity in test_bad_literal

A previous reviewer suggested adding a comment to highlight the bad_literal attribute in this test case for clarity. Please consider incorporating this to improve understandability.


821-843: Tests for input case sensitivity in from_xrpl methods are comprehensive

The added test test_request_input_from_xrpl_accepts_camel_case correctly ensures that BaseModel.from_xrpl only accepts inputs with camelCase or PascalCase keys, raising exceptions for snake_case keys. This reinforces consistency and correctness in input data handling.


845-862: Verification of key case sensitivity in Transaction.from_xrpl

The test test_transaction_input_from_xrpl_accepts_only_camel_case appropriately verifies that Transaction.from_xrpl rejects inputs with snake_case keys, aligning with expected input formats and preventing potential parsing issues.

xrpl/models/base_model.py Outdated Show resolved Hide resolved
tests/unit/models/test_base_model.py Show resolved Hide resolved
Copy link
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (1)
xrpl/models/requests/ledger_entry.py (1)

260-262: LGTM! Consider documenting the type expansion in the field's docstring.

The addition of int type support is consistent with xchain_claim_id and improves API uniformity. Consider updating the docstring to explicitly mention that both string and integer values are accepted.

Add type information to the docstring:

     xchain_create_account_claim_id: Optional[
         Union[int, str, XChainCreateAccountClaimID]
     ] = None
+    """
+    The XChainCreateAccountClaimID associated with a cross-chain account create.
+    Accepts both string and integer values.
+    """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 71b0a04 and 28963b4.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • xrpl/models/requests/ledger_entry.py (1 hunks)
🔇 Additional comments (1)
xrpl/models/requests/ledger_entry.py (1)

259-259: LGTM! Type expansion for xchain_claim_id is well-justified.

The addition of int type support maintains backward compatibility while improving developer experience by accepting native integer values, consistent with similar fields like oracle_document_id.

Let's verify the consistency of integer type support across the codebase:

✅ Verification successful

Type expansion for xchain_claim_id is consistent with codebase patterns

The addition of int type to xchain_claim_id is verified to be consistent with the codebase:

  • All XChain transaction types (XChainCommit, XChainClaim, XChainAddClaimAttestation) already support Union[int, str] for their xchain_claim_id fields
  • The XChainClaimID class is properly referenced in the type union, maintaining compatibility with object-based usage
  • Test fixtures show both string ("b0", "af") and numeric (1) claim IDs being used
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other XChain-related claim ID fields to ensure consistent type handling
ast-grep --pattern 'xchain_claim_id: Union[$_]'

Length of output: 49


Script:

#!/bin/bash
# Let's try a broader search for xchain_claim_id fields and their type annotations
rg "xchain_claim_id.*:" -A 1

# Also search for XChainClaimID to understand its definition and usage
rg "XChainClaimID" -A 2

Length of output: 7766

CHANGELOG.md Outdated
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support for the DeliverMax field in Payment transactions
- Support for the `feature` RPC
- Improved validation for models to also check param types
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Move entry to "Unreleased" section and enhance description.

The changelog entry for model validation should be moved to the "Unreleased" section since this is a new feature that hasn't been released yet. Additionally, consider expanding the description to provide more context about the types of validation being added.

Apply this diff to move and enhance the entry:

-### Added
-- Improved validation for models to also check param types

## [[Unreleased]]

### Added
+ - Added type validation for model parameters to catch type mismatches early and improve error reporting
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- Improved validation for models to also check param types
### Added
## [[Unreleased]]
### Added
- Added type validation for model parameters to catch type mismatches early and improve error reporting

Copy link
Contributor

@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: 0

🧹 Outside diff range and nitpick comments (1)
xrpl/models/base_model.py (1)

295-354: LGTM! Consider enhancing error messages for better debugging.

The type checking implementation is thorough and handles all major type scenarios correctly. However, the error messages could be more descriptive for better debugging.

Consider enhancing error messages to include the actual value in addition to its type:

-            return {attr: f"{attr} is {type(value)}, expected {expected_type}"}
+            return {attr: f"{attr} is {value} of type {type(value)}, expected {expected_type}"}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 28963b4 and d894086.

📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • xrpl/models/base_model.py (2 hunks)
  • xrpl/models/transactions/oracle_set.py (2 hunks)
🔇 Additional comments (5)
xrpl/models/base_model.py (2)

363-370: LGTM! Comprehensive validation implementation.

The updated _get_errors method now provides both required field validation and thorough type checking using the new _check_type method.


407-409: LGTM! Clean and readable implementation.

The updated __repr__ method uses a clean list comprehension approach for building the string representation.

xrpl/models/transactions/oracle_set.py (1)

23-26: LGTM! Ensures consistent integer type for epoch offset.

Using int() for the EPOCH_OFFSET calculation ensures consistent typing and aligns with the improved type validation.

CHANGELOG.md (2)

Line range hint 1-24: LGTM! The changelog format is correct.

The file correctly follows the Keep a Changelog format and Semantic Versioning guidelines. The "Unreleased" section is properly structured with appropriate subsections for different types of changes.


27-27: 🛠️ Refactor suggestion

Move and enhance the model validation entry.

The changelog entry needs to be moved to the "Unreleased" section since this is a new feature that hasn't been released yet. Additionally, the description should be enhanced to better reflect the PR's purpose of adding type validation for model parameters.

Apply this diff to move and enhance the entry:

-### Added
-- Improved validation for models to also check param types

## [[Unreleased]]

### Added
+ - Added type validation for model parameters to catch type mismatches early and improve error reporting

Copy link
Contributor

@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: 2

🧹 Outside diff range and nitpick comments (1)
xrpl/models/base_model.py (1)

312-312: Remove unclear comment or resolve the mypy issue.

The comment # unsure what the problem with mypy is here indicates uncertainty and may confuse future maintainers. Consider resolving the mypy issue or removing the comment before merging.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d894086 and 591513e.

📒 Files selected for processing (1)
  • xrpl/models/base_model.py (2 hunks)
🔇 Additional comments (2)
xrpl/models/base_model.py (2)

323-323: Duplicate comment regarding list type handling in _check_type.

This line has been previously discussed and addressed in past review comments. The handling of list types in the type checking function seems appropriate.


351-352: Verify that isinstance correctly handles all expected types.

The check isinstance(value, expected_type) may not properly handle complex types like generics or type aliases. Ensure that all expected types are correctly validated, and consider augmenting the type checking logic if necessary.

Run the following script to test how isinstance behaves with various expected types:

✅ Verification successful

Type checking implementation is robust and handles complex types correctly

Based on the codebase analysis, the _check_type method in base_model.py has comprehensive type checking that properly handles:

  • Union types through get_origin and get_args
  • Lists with generic type parameters
  • Dictionaries
  • Enum types
  • Literal types
  • BaseModel subclasses
  • Any type
  • Basic Python types

The implementation goes beyond simple isinstance checks and includes proper handling of generic types from the typing module. The type checking is thorough and covers all expected use cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Test isinstance with different expected types.

python3 - << EOF
from typing import List, Union, Dict, Any

def test_isinstance():
    test_cases = [
        (1, int),
        ("string", str),
        ([1, 2, 3], list),
        ([1, 2, 3], List[int]),
        ({"key": "value"}, dict),
        ({"key": "value"}, Dict[str, str]),
        (None, type(None)),
        (None, Union[str, None]),
    ]

    for value, expected_type in test_cases:
        try:
            result = isinstance(value, expected_type)
        except TypeError as e:
            result = f"TypeError: {e}"
        print(f"isinstance({value!r}, {expected_type}) -> {result}")

test_isinstance()
EOF

Length of output: 109


Script:

#!/bin/bash
# Let's analyze the codebase to understand how expected_type is used
# First, let's find where expected_type is defined or used
rg "expected_type" -A 3 -B 3

# Then, let's look for type hints and validation patterns
ast-grep --pattern 'def validate_$_($$$) -> $_: $$$'

# Also search for type-related imports
rg "^from typing import"

Length of output: 15075

@@ -340,6 +404,6 @@ def __eq__(self: Self, other: object) -> bool:
return isinstance(other, BaseModel) and self.to_dict() == other.to_dict()

def __repr__(self: Self) -> str:
"""Returns a string representation of a BaseModel object"""
"""Returns a string representation of a BaseModel object."""
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add missing __repr__ method definition to prevent syntax errors.

The docstring on line 407 is not associated with any function, suggesting that the __repr__ method definition is missing. This will cause a syntax error.

Apply this diff to define the __repr__ method correctly:

+    def __repr__(self: Self) -> str:
         """Returns a string representation of a BaseModel object."""
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"""Returns a string representation of a BaseModel object."""
def __repr__(self: Self) -> str:
"""Returns a string representation of a BaseModel object."""

Comment on lines +348 to +350
if issubclass(expected_type, BaseModel) and isinstance(value, dict):
return {}

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure proper validation of BaseModel subclasses when value is a dict.

In the condition if issubclass(expected_type, BaseModel) and isinstance(value, dict):, the method returns {} without validating the contents of value. This may allow invalid data to pass through without proper checks. Consider attempting to instantiate expected_type with value to ensure it is valid.

Apply this diff to perform validation:

 if issubclass(expected_type, BaseModel) and isinstance(value, dict):
-    return {}
+    try:
+        expected_type.from_dict(value)
+        return {}
+    except XRPLModelException as e:
+        return {attr: f"{attr} is invalid: {e}"}

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (2)
xrpl/models/transactions/transaction.py (1)

Line range hint 371-385: LGTM! Consider adding type hints for better clarity.

The error handling for invalid flag types is a good addition that aligns with the PR's type validation objectives.

Consider adding a type hint for self.flags in the error message for better clarity:

-            raise XRPLModelException("self.flags is not an int, dict, or list")
+            raise XRPLModelException(f"self.flags is {type(self.flags)}, expected int, dict, or list")
xrpl/models/base_model.py (1)

301-303: Add return type annotation for better type safety.

The method is missing a return type annotation which would improve type safety and documentation.

-    def _check_type(
-        self: Self, attr: str, value: Any, expected_type: Type[Any]  # noqa: ANN401
-    ) -> Dict[str, str]:
+    def _check_type(
+        self: Self, attr: str, value: Any, expected_type: Type[Any]  # noqa: ANN401
+    ) -> Dict[str, str]:
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 591513e and a6807ac.

📒 Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • xrpl/models/base_model.py (2 hunks)
  • xrpl/models/requests/ledger_entry.py (1 hunks)
  • xrpl/models/transactions/transaction.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • xrpl/models/requests/ledger_entry.py
  • CHANGELOG.md
🔇 Additional comments (4)
xrpl/models/transactions/transaction.py (2)

Line range hint 419-422: LGTM! Clear error handling for unsigned transactions.

The error handling for unsigned transactions is well-implemented and provides a clear error message.


Line range hint 509-524: LGTM! Consider verifying Payment transaction handling.

The validation for Payment transactions with both amount and deliver_max fields is well-implemented.

Let's verify the Payment transaction handling:

✅ Verification successful

Validation for Payment transactions with amount and deliver_max is properly implemented and tested

The verification shows:

  • The implementation correctly validates Payment transactions with both amount and deliver_max fields
  • Unit tests cover different scenarios:
    • test_payment_txn_API_no_deliver_max
    • test_payment_txn_API_different_amount_and_deliver_max
    • test_payment_txn_API_identical_amount_and_deliver_max
  • Integration tests verify the deliver_max alias field functionality
  • The code properly handles field aliasing by setting amount = deliver_max and removing the deliver_max field before serialization
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for Payment transactions using both amount and deliver_max fields

# Search for test files containing Payment transactions
echo "Searching for test files with Payment transactions..."
rg -l "Payment.*deliver_max" --type py

# Search for actual usage in non-test files
echo "Searching for actual usage in non-test files..."
rg -l "Payment.*deliver_max" --type py -g '!test*'

Length of output: 315


Script:

#!/bin/bash
# Let's try a different approach to verify Payment transaction handling

# Search for Payment transaction tests with broader context
echo "Searching for Payment transaction tests..."
rg "class.*Payment.*Test" -A 10 --type py

# Search for deliver_max field usage
echo "Searching for deliver_max field usage..."
rg "deliver_max" -B 2 -A 2 --type py

# Search for amount field in Payment transactions
echo "Searching for amount field in Payment context..."
rg "amount.*Payment|Payment.*amount" -B 2 -A 2 --type py

Length of output: 10330

xrpl/models/base_model.py (2)

369-376: LGTM! The error collection logic is well-implemented.

The method correctly:

  • Handles required field validation
  • Performs type validation for all fields
  • Accumulates errors in a single dictionary

354-356: 🛠️ Refactor suggestion

Enhance BaseModel validation to verify dict contents.

The current implementation only checks if the value is a dict without validating that it can be converted to the expected BaseModel type.

This issue was previously identified in a past review comment. Apply this diff to validate the dict contents:

-        if issubclass(expected_type, BaseModel) and isinstance(value, dict):
-            return {}
+        if issubclass(expected_type, BaseModel) and isinstance(value, dict):
+            try:
+                expected_type.from_dict(value)
+                return {}
+            except XRPLModelException as e:
+                return {attr: f"{attr} is invalid: {e}"}

Comment on lines +334 to +339
if expected_type_origin is dict:
return (
{}
if isinstance(value, dict)
else {attr: f"{attr} is {type(value)}, expected {expected_type}"}
)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance dict validation to check key and value types.

The current implementation only verifies that the value is a dict without validating its contents against the expected key and value types.

-        if expected_type_origin is dict:
-            return (
-                {}
-                if isinstance(value, dict)
-                else {attr: f"{attr} is {type(value)}, expected {expected_type}"}
-            )
+        if expected_type_origin is dict:
+            if not isinstance(value, dict):
+                return {attr: f"{attr} is {type(value)}, expected {expected_type}"}
+            key_type, value_type = get_args(expected_type)
+            result = {}
+            for k, v in value.items():
+                result.update(self._check_type(f"{attr}[{k}].key", k, key_type))
+                result.update(self._check_type(f"{attr}[{k}].value", v, value_type))
+            return result
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if expected_type_origin is dict:
return (
{}
if isinstance(value, dict)
else {attr: f"{attr} is {type(value)}, expected {expected_type}"}
)
if expected_type_origin is dict:
if not isinstance(value, dict):
return {attr: f"{attr} is {type(value)}, expected {expected_type}"}
key_type, value_type = get_args(expected_type)
result = {}
for k, v in value.items():
result.update(self._check_type(f"{attr}[{k}].key", k, key_type))
result.update(self._check_type(f"{attr}[{k}].value", v, value_type))
return result

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.

Better type validation for classes
2 participants