Skip to content

NoCache class requires parentheses for decorator, unlike Cache#20

Merged
leshchenko1979 merged 3 commits intomasterfrom
leshchenko1979/issue19
Nov 12, 2024
Merged

NoCache class requires parentheses for decorator, unlike Cache#20
leshchenko1979 merged 3 commits intomasterfrom
leshchenko1979/issue19

Conversation

@leshchenko1979
Copy link
Copy Markdown
Owner

@leshchenko1979 leshchenko1979 commented Nov 12, 2024

Fixes #19

Summary by Sourcery

Fix the NoCache class to support usage as a decorator with or without parentheses and add corresponding tests to ensure correct functionality.

Bug Fixes:

  • Fix the NoCache class to allow usage as a decorator both with and without parentheses.

Tests:

  • Add tests for NoCache to verify its functionality with and without parentheses when used as a decorator.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Nov 12, 2024

Reviewer's Guide by Sourcery

This PR modifies the NoCache class to support both decorator syntaxes - with and without parentheses (@cache and @cache()). The implementation changes the call method from a staticmethod to an instance method and introduces a new _decorator method to handle the function wrapping logic.

Sequence diagram for NoCache decorator usage

sequenceDiagram
    actor User
    participant NoCache
    participant Function
    User->>NoCache: Apply @cache or @cache() decorator
    NoCache->>NoCache: __call__(*decorator_args, **decorator_kwargs)
    alt With parentheses
        NoCache->>NoCache: _decorator(fn)
    else Without parentheses
        NoCache->>NoCache: _decorator(fn)
    end
    NoCache->>Function: Wrap function
    User->>Function: Call decorated function
    Function-->>User: Return result
Loading

Updated class diagram for NoCache class

classDiagram
    class NoCache {
        +__call__(*decorator_args, **decorator_kwargs)
        +_decorator(fn)
        +cache
    }
    note for NoCache "The __call__ method is now an instance method and can handle both @cache and @cache() syntaxes."
Loading

File-Level Changes

Change Details Files
Refactor NoCache class to support both decorator syntaxes
  • Convert call from staticmethod to instance method
  • Add logic to handle both @cache and @cache() decorator patterns
  • Extract function wrapping logic into new _decorator method
  • Maintain existing wrapper behavior for both async and non-async functions
perscache/cache.py
Add tests to verify NoCache decorator functionality
  • Add test for NoCache decorator with parentheses
  • Add test for NoCache decorator without parentheses
tests/test_nocache.py

Assessment against linked issues

Issue Objective Addressed Explanation
#19 Allow NoCache decorator to be used without parentheses (like @cache instead of @cache())

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

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

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @leshchenko1979 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

"""
if decorator_args and callable(decorator_args[0]):
return self._decorator(decorator_args[0])
return self._decorator
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Decorator kwargs are not forwarded to _decorator when used as a parameterized decorator

When used as a parameterized decorator, decorator_kwargs are lost. Consider passing them through: return lambda fn: self._decorator(fn, **decorator_kwargs)

Copy link
Copy Markdown
Owner Author

@leshchenko1979 leshchenko1979 left a comment

Choose a reason for hiding this comment

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

@sourcery-ai review

@leshchenko1979 leshchenko1979 merged commit 7fcd34b into master Nov 12, 2024
@leshchenko1979 leshchenko1979 deleted the leshchenko1979/issue19 branch November 12, 2024 12:43
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.

NoCache class requires parentheses for decorator, unlike Cache

1 participant