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

Merge locals() with globals() for context in Python 3.13 #14

Merged
merged 4 commits into from
Oct 9, 2024

Conversation

CNSeniorious000
Copy link
Member

@CNSeniorious000 CNSeniorious000 commented Oct 9, 2024

Summary by Sourcery

Fix context generation by merging locals() with globals() for Python 3.13 and above, ensuring correct context forwarding.

Bug Fixes:

  • Fix context generation to correctly merge locals() with globals() when Python version is 3.13 or higher.

Copy link

sourcery-ai bot commented Oct 9, 2024

Reviewer's Guide by Sourcery

This pull request modifies the context generation in the _make_context method of the Template class to merge globals() with locals() for Python versions 3.13 and above. This change addresses issue #13 and improves the context forwarding mechanism.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Update context generation to include global variables for Python 3.13+
  • Add version check for Python 3.13 and above
  • Merge globals() with locals() for newer Python versions
  • Maintain backward compatibility for older Python versions
python/promplate/prompt/template.py

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

@CNSeniorious000
Copy link
Member Author

============================== warnings summary ===============================
tests/test_utils.py::test_accumulate
  C:\hostedtoolcache\windows\Python\3.13.0\x64\Lib\asyncio\base_events.py:746: RuntimeWarning: coroutine method 'aclose' of 'accumulate_any.<locals>._' was never awaited
    self._ready.clear()
  Enable tracemalloc to get traceback where the object was allocated.
  See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================== short test summary info ===========================

Copy link

netlify bot commented Oct 9, 2024

Deploy Preview for promplate-core ready!

Name Link
🔨 Latest commit 2ff4fa1
🔍 Latest deploy log https://app.netlify.com/sites/promplate-core/deploys/670696eadfa0130008cbc445
😎 Deploy Preview https://deploy-preview-14--promplate-core.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

vercel bot commented Oct 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
core ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 2:45pm

Copy link

@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 @CNSeniorious000 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider breaking down the one-liner into multiple lines for improved readability.
  • It would be helpful to add a brief comment explaining why the change is necessary for Python 3.13 and above.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Review instructions: all looks good
  • 🟢 Testing: all looks good
  • 🟢 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.

@@ -87,7 +88,8 @@ def _on_special_token(self, token, sync: bool):
@staticmethod
def _make_context(text: str):
"""generate context parameter if specified otherwise use locals() by default"""

if version_info >= (3, 13):
return f"globals() | locals() | dict({text[text.index(' ') + 1:]})" if " " in text else "globals() | locals()"
return f"locals() | dict({text[text.index(' ') + 1:]})" if " " in text else "locals()"
Copy link

@sourcery-ai sourcery-ai bot Oct 9, 2024

Choose a reason for hiding this comment

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

suggestion: Consider refactoring the common condition if " " in text to improve readability.

The condition is repeated in both the new and existing code. Extracting this logic could make the code more maintainable and reduce duplication.

@staticmethod
def _make_context(text: str):
    """generate context parameter if specified otherwise use locals() by default"""
    has_extra_context = " " in text
    if version_info >= (3, 13):
        base_context = "globals() | locals()"
        extra = f" | dict({text[text.index(' ') + 1:]})" if has_extra_context else ""
    else:
        base_context = "locals()"
        extra = f" | dict({text[text.index(' ') + 1:]})" if has_extra_context else ""
    return f"{base_context}{extra}"

@CNSeniorious000 CNSeniorious000 temporarily deployed to fix-3.13-locals - promplate python coverage PR #14 October 9, 2024 14:44 — with Render Destroyed
@CNSeniorious000 CNSeniorious000 changed the title Merge locals() with globals() for context forwarding Merge locals() with globals() for context in Python 3.13 Oct 9, 2024
@CNSeniorious000 CNSeniorious000 merged commit 4fed982 into dev Oct 9, 2024
42 checks passed
@CNSeniorious000 CNSeniorious000 deleted the fix-3.13-locals branch October 9, 2024 14:52
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.

locals() forwarding breaks in Python 3.13
1 participant