Skip to content

[bugfix] staticmethod and classmethod action decorators fix - #479

Merged
drazvan merged 1 commit into
NVIDIA-NeMo:developfrom
piotrm0:piotrm/class-static-actions
Jul 10, 2024
Merged

[bugfix] staticmethod and classmethod action decorators fix#479
drazvan merged 1 commit into
NVIDIA-NeMo:developfrom
piotrm0:piotrm/class-static-actions

Conversation

@piotrm0

@piotrm0 piotrm0 commented May 1, 2024

Copy link
Copy Markdown
Contributor

action decorator does not preserve the decorator argument options if applied to staticmethods or classmethods. For example:

import os

from nemoguardrails import LLMRails
from nemoguardrails import RailsConfig
from nemoguardrails.actions.actions import action

os.environ["OPENAI_API_KEY"]="sk-..."

config = RailsConfig.from_path(".")
rails = LLMRails(config)

@action(name="func")
def test_func():
    return "hello"

class Test:
    @action(name="method")
    def test_method(self):
        return "method"

    @action(name="staticmethod")
    @staticmethod
    def test_staticmethod():
        return "static"

    @action(name="classmethod")
    @classmethod
    def test_classmethod(cls):
        return "class"

rails.register_action(test_func)
rails.register_action(Test().test_method)
rails.register_action(Test.test_staticmethod)
rails.register_action(Test.test_classmethod)

assert "func" in rails.runtime.registered_actions
assert "method" in rails.runtime.registered_actions
assert "staticmethod" in rails.runtime.registered_actions
assert "classmethod" in rails.runtime.registered_actions

The last two assertions will fail. This PR makes them pass.

Signed-off-by: Piotr Mardziel <piotrm@truera.com>
@piotrm0
piotrm0 force-pushed the piotrm/class-static-actions branch from addd9ec to eee1886 Compare May 1, 2024 00:16
@piotrm0 piotrm0 changed the title staticmethod and classmethod action decorators fix [bugfix] staticmethod and classmethod action decorators fix May 18, 2024
@drazvan
drazvan self-requested a review July 10, 2024 10:21

@drazvan drazvan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good. Thanks @piotrm0!

@drazvan
drazvan merged commit e8ce7ee into NVIDIA-NeMo:develop Jul 10, 2024
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