Skip to content

Support @logfire.instrument above @classmethod and @staticmethod - #2807

Closed
astroknot-sheep wants to merge 1 commit into
pydantic:mainfrom
astroknot-sheep:fix/instrument-over-classmethod
Closed

astroknot-sheep wants to merge 1 commit into
pydantic:mainfrom
astroknot-sheep:fix/instrument-over-classmethod

Conversation

@astroknot-sheep

@astroknot-sheep astroknot-sheep commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

@logfire.instrument breaks when it's put above @classmethod or @staticmethod instead of below:

import logfire

logfire.configure(send_to_logfire=False)


class Service:
    @logfire.instrument('load {a}')
    @classmethod
    def load(cls, a: int): ...
    # TypeError: <classmethod(<function Service.load ...>)> is not a callable object  (at class definition)

    @logfire.instrument
    @classmethod
    def bare(cls, a: int): ...
    # defines fine, but the classmethod is taken as msg_template, so `bare` is now the decorator:
    # Service.bare(1) -> TypeError: 1 is not a callable object

    @logfire.instrument('check {a}')
    @staticmethod
    def check(a: int): ...
    # Service.check(1) works, but Service().check(1) -> TypeError: too many positional arguments

The wrapper is a plain function, so the classmethod/staticmethod binding gets lost. With this change, instrument instruments the underlying __func__ and wraps the result in the same descriptor type again. Logfire.instrument also treats a classmethod object as the decorated function for the bare @logfire.instrument form (a staticmethod is already callable). I made the same small change in the logfire-api shim, because the bare form above @classmethod breaks there too.

Tests: added test_instrument_above_classmethod_and_staticmethod, and a shim case in test_runtime. Both fail on main and pass now. tests/test_logfire.py, tests/test_logfire_api.py and tests/test_auto_trace.py pass locally. The one exception is test_runtime[with_logfire], which fails the same way on main for me because of a local google-genai import error. ruff and pyright are clean.

I used an AI assistant while working on this; I reproduced the issue, reviewed the change and ran the tests myself.

View guided diff

@veria-ai

veria-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 5 files

Confidence score: 5/5

  • tests/test_logfire.py only covers the configured form above @staticmethod, so regressions in the bare-descriptor path could go undetected. Add a bare @logfire.instrument case and assert the call and span.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/test_logfire.py">

<violation number="1" location="tests/test_logfire.py:1467">
P2: This regression test does not cover bare `@logfire.instrument` above `@staticmethod`; it only covers the configured form. Add a bare staticmethod case and assert its call and span so the separate bare-descriptor path is protected.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

Comment thread tests/test_logfire.py
def template_classmethod(cls, a: int) -> str:
return f'{cls.__name__} {a}'

@logfire.instrument('staticmethod {a}')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: This regression test does not cover bare @logfire.instrument above @staticmethod; it only covers the configured form. Add a bare staticmethod case and assert its call and span so the separate bare-descriptor path is protected.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/test_logfire.py, line 1467:

<comment>This regression test does not cover bare `@logfire.instrument` above `@staticmethod`; it only covers the configured form. Add a bare staticmethod case and assert its call and span so the separate bare-descriptor path is protected.</comment>

<file context>
@@ -1452,6 +1452,86 @@ def __repr__(self):
+        def template_classmethod(cls, a: int) -> str:
+            return f'{cls.__name__} {a}'
+
+        @logfire.instrument('staticmethod {a}')
+        @staticmethod
+        def template_staticmethod(a: int) -> int:
</file context>

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-api/logfire_api
  __init__.py
  logfire-sdk/logfire/_internal
  instrument.py
  main.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki alexmojaki closed this Nov 16, 2026
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