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 26 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.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for the DeliverMax field in Payment transactions.
    • Implemented the feature RPC.
  • Improvements

    • Enhanced validation for models to check parameter types, improving data integrity.
  • Bug Fixes

    • Fixed the handling of empty strings in DIDSet transactions for improved flexibility.
  • Documentation

    • Updated error messages for clarity and consistency.

These changes enhance transaction handling, improve error reporting, and increase the robustness of data validation processes.

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

coderabbitai bot commented Sep 19, 2024

Actions performed

Full review triggered.

Copy link

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 18 minutes and 19 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

Files that changed from the base of the PR and between c745f69 and 786ad8d.

Walkthrough

The recent changes to the project include the addition of support for the DeliverMax field in Payment transactions and the implementation of the feature RPC. Enhancements were made to the validation process for models, ensuring stricter type checking for parameters. Several test cases were added or modified to validate input types and structures, while some constants and error messages were updated for clarity. Additionally, type annotations in various classes were adjusted to allow for broader data types, improving flexibility in handling different inputs.

Changes

Files Change Summary
CHANGELOG.md Added support for DeliverMax field, implemented feature RPC, improved model validation for types.
snippets/paths.py Added a blank line at the beginning of the file.
tests/unit/models/test_base_model.py Added and modified tests for input type validation, renamed existing tests for clarity.
tests/unit/models/transactions/test_check_cash.py Changed _CHECK_ID from an integer to a hexadecimal string.
tests/unit/models/transactions/test_oracle_set.py Updated error message for last_update_time validation.
tests/unit/models/transactions/test_xchain_claim.py Changed destination_tag from string to integer.
xrpl/models/base_model.py Added _check_type method for type validation, modified _get_errors to utilize it.
xrpl/models/requests/ledger_entry.py Updated type annotations for xchain_claim_id and xchain_create_account_claim_id to include int.
xrpl/models/transactions/oracle_set.py Changed EPOCH_OFFSET to an integer and updated error message formatting.
xrpl/models/transactions/pseudo_transactions/enable_amendment.py Expanded flags type to include Dict[str, bool].
xrpl/models/transactions/transaction.py Streamlined error handling in _get_errors and updated has_flag method for better type checking.
xrpl/utils/xrp_conversions.py Replaced type() checks with isinstance() for better input validation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant System
    participant Validator

    User->>System: Initiates Payment with DeliverMax
    System->>Validator: Validate transaction parameters
    Validator-->>System: Parameters valid
    System->>User: Transaction successful
Loading

🐇 "In code we hop, with changes bright,
New fields and tests, a joyful sight.
With types aligned, and errors clear,
We dance in data, with nary a fear!
So let us code, and play all day,
For in this project, we find our way!" 🐇


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 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.

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.

@mvadari
Copy link
Collaborator Author

mvadari commented Sep 19, 2024

@coderabbitai full review

Copy link

coderabbitai bot commented Sep 19, 2024

Actions performed

Full review triggered.

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: 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.

if expected_type_origin is list:
# expected a List, received a List
if not isinstance(value, list):
return {"attr": f"{attr} is {type(value)}, expected {expected_type}"}
Copy link

@coderabbitai coderabbitai bot Sep 19, 2024

Choose a reason for hiding this comment

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

Fix the typo in error dictionary keys to correctly reference the variable attr.

In the return statements on lines 314, 328, and 348, the key of the error dictionary is mistakenly set to the string "attr" instead of the variable attr. This will result in error dictionaries with the key "attr" instead of the actual attribute name, which could lead to confusion when handling errors.

Apply this diff to correct the error:

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

Also applies to: 328-328, 348-348

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 786ad8d

Copy link

Choose a reason for hiding this comment

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

@mvadari Thank you for fixing the issue! 🐰🎉


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

"issuing_chain_door": issuer,
"issuing_chain_issue": {"currency": "XRP"},
},
"public_key": "0342E083EA762D91D621714C394",
Copy link

@coderabbitai coderabbitai bot Sep 19, 2024

Choose a reason for hiding this comment

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

Ensure public_key values do not expose sensitive information

The hard-coded public_key values at lines 448 and 471 may inadvertently contain sensitive data. Please confirm that these are not actual cryptographic keys. If they are placeholders for testing, consider annotating them as such or using clearly non-sensitive values to prevent any security concerns.

Also applies to: 471-471

Tools
Gitleaks

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

(generic-api-key)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a sample string (that isn't even a valid public key), just used as a placeholder.

Copy link

Choose a reason for hiding this comment

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

@mvadari, thank you for the clarification. I've noted that the public_key values in this test file are placeholders and do not represent actual cryptographic keys.

(_/)
(•‿•)


Learnings added
Learnt from: mvadari
PR: XRPLF/xrpl-py#708
File: tests/unit/models/test_base_model.py:448-448
Timestamp: 2024-09-19T20:01:37.621Z
Learning: In `tests/unit/models/test_base_model.py`, the `public_key` values are sample strings used as placeholders and do not contain actual cryptographic keys.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

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