Skip to content

add async version of modify#92

Merged
felipao-mx merged 2 commits intomainfrom
modify
Jul 16, 2025
Merged

add async version of modify#92
felipao-mx merged 2 commits intomainfrom
modify

Conversation

@felipao-mx
Copy link
Copy Markdown
Contributor

@felipao-mx felipao-mx commented Apr 16, 2025

Summary by CodeRabbit

  • New Features
    • Introduced an asynchronous method for modifying query results, allowing non-blocking updates to documents.
  • Tests
    • Added new tests to ensure the correct behavior of the asynchronous modify functionality.
  • Chores
    • Updated the version number to 1.1.0.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 16, 2025

Walkthrough

The changes introduce a new asynchronous method, async_modify, to the AsyncQuerySet class in the mongoengine_plus/aio/async_query_set.py module. This method allows for asynchronous modifications of querysets, mirroring the parameters and behavior of the existing synchronous modify method but adapted for async usage. The project version is incremented from '1.0.1' to '1.1.0' in the mongoengine_plus/version.py file to reflect these updates. Additionally, a new asynchronous test function, test_async_modify, is added to tests/aio/test_async_query_set.py. This test verifies the correct behavior of the new async_modify method under various scenarios, including updating, upserting, and removing documents, ensuring that the method operates as intended in different cases. No other files or functionality are affected by these changes.

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 66eae5c and ecf673f.

📒 Files selected for processing (3)
  • mongoengine_plus/aio/async_query_set.py (1 hunks)
  • mongoengine_plus/version.py (1 hunks)
  • tests/aio/test_async_query_set.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
`**/*.py`: Enforce Relative Imports for Internal Modules

Ensure that any imports referencing internal modules use relative paths. However, if modules reside in the main module dir...

**/*.py: Enforce Relative Imports for Internal Modules

Ensure that any imports referencing internal modules use relative paths. However, if modules reside in the main module directories (for example /src or /library_or_app_name) —and relative imports are not feasible—absolute imports are acceptable. Additionally, if a module is located outside the main module structure (for example, in /tests or /scripts at a similar level), absolute imports are also valid.

Examples and Guidelines:

  1. If a module is in the same folder or a subfolder of the current file, use relative imports. For instance: from .some_module import SomeClass
  2. If the module is located under /src or /library_or_app_name and cannot be imported relatively, absolute imports are allowed (e.g., from library_or_app_name.utilities import helper_method).
  3. If a module is outside the main module directories (for example, in /tests, /scripts, or any similarly placed directory), absolute imports are valid.
  4. External (third-party) libraries should be imported absolutely (e.g., import requests).
  • mongoengine_plus/version.py
  • tests/aio/test_async_query_set.py
  • mongoengine_plus/aio/async_query_set.py
`**/*.py`: Rule: Enforce Snake Case in Python Backend
  1. New or Modified Code: Use snake_case for all variables, functions, methods, and class attributes.
  2. Exceptions (Pydantic...

**/*.py:
Rule: Enforce Snake Case in Python Backend

  1. New or Modified Code: Use snake_case for all variables, functions, methods, and class attributes.
  2. Exceptions (Pydantic models for API responses):
    • Primary fields must be snake_case.
    • If older clients expect camelCase, create a computed or alias field that references the snake_case field.
    • Mark any camelCase fields as deprecated or transitional.

Examples

Invalid:

class CardConfiguration(BaseModel):
    title: str
    subTitle: str  # ❌ Modified or new field in camelCase

Valid:

class CardConfiguration(BaseModel):
    title: str
    subtitle: str  # ✅ snake_case for new/modified field

    @computed_field
    def subTitle(self) -> str:  # camelCase allowed only for compatibility
        return self.subtitle

Any direct use of camelCase in new or updated code outside of these exceptions should be flagged.

  • mongoengine_plus/version.py
  • tests/aio/test_async_query_set.py
  • mongoengine_plus/aio/async_query_set.py
`**/*.py`: Use try/except for concise error handling when accessing nested dictionary keys:
try:
    can_ignore_error = data['error']['code'] in ignore_error_codes
excep...</summary>

> `**/*.py`: Use try/except for concise error handling when accessing nested dictionary keys:
> 
> ```python
> try:
>     can_ignore_error = data['error']['code'] in ignore_error_codes
> except KeyError:
>     can_ignore_error = False
> ```
> 
>Avoid Verbose Chained Conditionals:
> ```python
> can_ignore_error = (
>     'code' in data['error']
>     and data['error']['code'] in ignore_error_codes
> )
> ```
> 
> Explanation:
> The try/except approach:
> 
> Reduces code complexity and nesting
> Improves readability by focusing on the "happy path" logic
> Follows Python's "easier to ask forgiveness than permission" (EAFP) idiom
> 
> Severity: Important (Not a Nitpick)
> This pattern significantly improves code maintainability and readability, especially as dictionary access patterns become more complex.

- `mongoengine_plus/version.py`
- `tests/aio/test_async_query_set.py`
- `mongoengine_plus/aio/async_query_set.py`

</details>
<details>
<summary>`**/*.py`: Context
Agave is our internal Python library for processing SQS messages. The @task decorator can automatically convert JSON to Pydantic models.

Rule
Always annotate @t...</summary>

> `**/*.py`: Context
> Agave is our internal Python library for processing SQS messages. The @task decorator can automatically convert JSON to Pydantic models.
> 
> Rule
> Always annotate @task parameters with Pydantic models instead of manually converting dictionaries.
> 
> Correct Pattern
> ```python
> from pydantic import BaseModel
> from agave.tasks.sqs_tasks import task
> 
> class User(BaseModel):
>     name: str
>     age: int
>     
> @task(queue_url=QUEUE_URL, region_name='us-east-1')
> async def task_validator(message: User) -> None:
>     # The message is already a User instance - no conversion needed
>     print(message.name)  # Direct attribute access
> ```
> 
> Incorrect Pattern
> ```python
> from pydantic import BaseModel
> from agave.tasks.sqs_tasks import task
> 
> class User(BaseModel):
>     name: str
>     age: int
>     
> @task(queue_url=QUEUE_URL, region_name='us-east-1')
> async def task_validator(message_data: dict) -> None:  # or unannotated parameter
>     # Unnecessary conversion
>     message = User(**message_data)
>     print(message.name)
> ```
> 
> Explanation
> The Agave @task decorator automatically:
> 
> - Reads JSON messages from SQS queues
> - Converts them to Pydantic model instances when the handler parameter is annotated
> - Performs validation based on the Pydantic model

- `mongoengine_plus/version.py`
- `tests/aio/test_async_query_set.py`
- `mongoengine_plus/aio/async_query_set.py`

</details>
<details>
<summary>`**/*.py`: ## MANDATORY: Use built-in Pydantic validators

### Description
Avoid creating custom validators that duplicate functionality already provided by Pydantic's built-in val...</summary>

> `**/*.py`: ## MANDATORY: Use built-in Pydantic validators
> 
> ### Description
> Avoid creating custom validators that duplicate functionality already provided by Pydantic's built-in validators, pydantic_extra_types package, or third-party Pydantic validator libraries. This improves code maintainability and reduces unnecessary unit tests.
> 
> ### Bad Practice
> ```python
> from pydantic import BaseModel, field_validator
> 
> class MyValidator(BaseModel):
>     location: str
>     
>     @field_validator('location')
>     def validate_location(cls, value: str) -> str:
>         values = value.split(',')
>         if len(values) != 3:
>             raise ValueError('Must provide exactly 3 values for location')
>         # Custom validation logic that duplicates functionality
>         return value
> ```
> 
> ### Good Practice
> ```python
> from pydantic import BaseModel
> from pydantic_extra_types.coordinate import Coordinate
> 
> class MyValidator(BaseModel):
>     location: Coordinate
> ```
> 
> ### Unit Test Guidelines
> Do not write unit tests specifically for validating the behavior of Pydantic's built-in validators. These are already well-tested by the Pydantic library itself.
> 
> #### Tests to Remove
> ```python
> def test_invalid_location():
>     pytest.raises(ValidationError):
>         MyValidator(location='foo,bar')
> ```
> 
> ### Rule Enforcement
> This is a mandatory rule, not a refactoring suggestion. Changes must be implemented when:
> 1. A custom validator replicates functionality already available in Pydantic's ecosystem
> 2. There is a suitable built-in, pydantic_extra_types, or third-party Pydantic validator available
> 
> Actions required:
> 1. Replace custom validators with appropriate existing validators
> 2. Remove unnecessary unit tests that only validate built-in Pydantic validation behavior
> 3. Block PRs that introduce new custom validators when alternatives exist

- `mongoengine_plus/version.py`
- `tests/aio/test_async_query_set.py`
- `mongoengine_plus/aio/async_query_set.py`

</details>

</details><details>
<summary>🧬 Code Graph Analysis (1)</summary>

<details>
<summary>mongoengine_plus/aio/async_query_set.py (1)</summary><blockquote>

<details>
<summary>mongoengine_plus/aio/utils.py (1)</summary>

* `create_awaitable` (6-8)

</details>

</blockquote></details>

</details>

</details>

<details>
<summary>🔇 Additional comments (3)</summary><blockquote>

<details>
<summary>mongoengine_plus/version.py (1)</summary>

`1-1`: **Version bump is appropriate for new feature addition**

The version change from '1.0.1' to '1.1.0' correctly follows semantic versioning principles, where a minor version increment indicates the addition of new functionality (the async_modify method) without breaking existing functionality.

</details>
<details>
<summary>mongoengine_plus/aio/async_query_set.py (1)</summary>

`40-57`: **Implementation correctly follows established pattern**

The implementation of `async_modify` properly follows the same pattern as other async methods in this class by using `create_awaitable` to wrap the synchronous method. All parameters are correctly passed through to maintain the same interface.

The method signature includes all necessary parameters (upsert, full_response, remove, new, array_filters) with appropriate default values, matching the underlying synchronous modify method's functionality.

</details>
<details>
<summary>tests/aio/test_async_query_set.py (1)</summary>

`71-110`: **Tests are comprehensive and well-structured**

The test function thoroughly covers the behavior of the async_modify method across multiple scenarios:
- Modifying with new=True/False to test returning updated/original versions
- Verifying database state after modifications
- Testing upsert functionality for non-existent documents
- Testing document removal

Each scenario includes proper assertions to verify the expected behavior.

</details>

</blockquote></details>

</details>
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEejqANiS5pa9NIlkYGkKRUTx8WfADNIzRXgfWQMAOWxmAUouAE4AJgMAVURoyB8SC3huNEJmAA8jfWNwKDJ6XxwCYjJlGnomVnYuXn5hUXEpGXkmJSpVdS0dIpMoOFRUTErCUnIqOoVGjE5IKgB3SEQI5jQKeTkFXpU1TW1dMENi0wMAjCJ8MiJ4cgB9bgtsRAB6bXxvx2dngBHbCUWTPFKabiyDgGABE8IMAGJEZAAIIASWqs2odA2Wx28gqDFgmFIiCMqMg5HWDicxIoXnwH38JFwsEUkAABrSAQFaEFZJzIKsHOg7LiCJA2SQuaj/gwAIog3YAZVZQoYFgciA0MAQyDYbI5qyo3GQ0sgJDySHEtw2dNgDIwTOQnL5AqFhvZtAANJaMCpMnaefTGcynhJ8AxqJ4sKt1LAeDs0IbKIguJzsGbKLhOX7OT5sBYLM8KNJuF4UnmuWWAlJq5zqQ2dlQwT54BYaO5q5h7BRjlRdpAs7QcZAANYkWSrfAUPtECLsHV6mVejll3DYCgYc2wGVoEXqXFlzad/h+T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eD9Hw94/I7Sl2XnaGRac/w8xjDJSx7hMGARcSsIhEUvLiQyKyGU8D5FLApnxTaNMPE7npvA0QTM4nIFZvJWcHMeCqXUv6cQ4gBaQHchnMQl1bAmTMvKYJfgc5WRsrZBg/NEDFTupyd2Ltfg1K9n3XYvtISCjWoUc4lxShkVkmgPA0wahzFxA0chXA1h4n2kOfYmM+jHEGGcQwIwFjgI9vAWgiB/IITinQcED5FKbOGZaBgPgABsAB2AAzD4KIJABCg3uWgAALCQD5ABWGIMRQZ/Ihvchg9z7lxAEH8v5aA4gxAABwxFoI8r5lyDDbNmeoZ4+zDllmOasU5ZRNlAA=== -->

<!-- internal state end -->
<!-- finishing_touch_checkbox_start -->

<details>
<summary>Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---



<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=cuenca-mx/mongoengine-plus&utm_content=92):

- 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](https://docs.coderabbit.ai/finishing-touches/docstrings) for this 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

@felipao-mx felipao-mx merged commit c75b088 into main Jul 16, 2025
11 checks passed
@felipao-mx felipao-mx deleted the modify branch July 16, 2025 19:28
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.

2 participants