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

Add Python 3.13 Support #203

Merged
merged 2 commits into from
Dec 19, 2024
Merged

Add Python 3.13 Support #203

merged 2 commits into from
Dec 19, 2024

Conversation

bonk1t
Copy link
Collaborator

@bonk1t bonk1t commented Dec 16, 2024

Overview

This PR adds support for Python 3.13 by introducing a custom classproperty decorator in the BaseTool class. The change addresses the incompatibility of using @classmethod in conjunction with @property in Python 3.13 and ensures that class-level properties function correctly in newer Python versions.

Changes Made

  1. Introduced classproperty Decorator:

    • Defined a custom classproperty decorator to allow class methods to be accessed as properties at the class level.
    • The decorator is compatible with Python 3.13 and provides a clean way to define class-level properties without relying on the now-unsupported combination of @classmethod and @property.
  2. Updated BaseTool.openai_schema Method:

    • Replaced the @classmethod decorator with the new @classproperty decorator on the openai_schema method.
    • This change allows openai_schema to be accessed as a property directly from the class without instantiation.

How It Works

The classproperty Decorator

The custom classproperty decorator enables defining properties that are accessible at the class level. Here's the implementation:

class classproperty:
    def __init__(self, fget):
        self.fget = fget

    def __get__(self, instance, owner):
        return self.fget(owner)
  • Initialization: The decorator takes a getter function (fget) as its argument.
  • Descriptor Protocol:
    • The __get__ method is part of Python's descriptor protocol.
    • It accepts instance (which will be None since it's accessed at the class level) and owner (the class itself).
    • Returns the result of calling the getter function with the class (owner) as the argument.

Applying classproperty to openai_schema

In BaseTool.py, the openai_schema method is updated to use the new decorator:

class BaseTool(BaseModel, ABC):
    # ... existing code ...

    @classproperty
    def openai_schema(cls) -> dict[str, Any]:
        # Implementation details...
        # Generates and returns the schema dictionary.
        return schema

    # ... remaining code ...
  • Access Without Instantiation: With the @classproperty decorator, we can access openai_schema directly from the class:

    schema = BaseTool.openai_schema
  • Compatibility with Python 3.13: This approach avoids the use of @classmethod and @property together, which is not supported in Python 3.13 and later.

Testing and Validation

  • Python Version Testing: Verified that the updated code works as expected in both Python 3.12 and Python 3.13 environments.
  • Functionality Check: Ensured that BaseTool.openai_schema returns the correct dictionary and that other functionalities of the BaseTool class remain unaffected.
  • No Breaking Changes: The change is backward-compatible and does not introduce breaking changes to the existing codebase.

Fix #198

@bonk1t bonk1t merged commit 1e28824 into VRSEN:main Dec 19, 2024
1 check failed
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.

TypeError: Object of type method is not JSON serializable when using Python v3.13.0
1 participant