Repository navigation
fix(observe): detect methods by first parameter only - #2055
zichen0116 wants to merge 1 commit into
Conversation
|
|
Fatal777
left a comment
There was a problem hiding this comment.
Tested this locally on 94ca700, which merges cleanly into current main.
uv run --frozen ruff check .,ruff format --check .,mypy langfuse --no-error-summary: cleanpytest tests/unit/test_observe.py: 20 passed. Withmain'sobserve.py, the 3 new free-function tests fail, so they cover the bug.pytest -n auto --dist worksteal tests/unit: 959 passed, 2 skipped (plus the 18 pre-existingtest_prompt.pyenv-var errors, #1976)
I also compared the old and new _is_method across a set of signatures. Only cases that were wrong before change:
- now treated as functions (first arg kept):
def f(value, *, cls="news")(the issue),def f(value, self=None),def f(*args, cls=None)(old code droppedargs[0]), and@staticmethod def f(x, *, cls=None) - still treated as methods:
def m(self, x),def m(cls, x)under@classmethod,functools.wraps-decorated methods,def m(self, /, x) - unchanged as functions: bound methods and
functools.partial(C.m, obj)
Two limits of the name-based check remain. Neither is a regression:
- a free function whose first parameter is literally named
selfstill loses its first arg - a method whose receiver isn't named
self/clsbut which has a keyword-onlycls(e.g.def m(this, x, *, cls=None)) used to havethisstripped by accident; it will now be logged. Seems acceptable, just noting the behavior change.
Overlap: #2016 targeted the same issue and was closed. #2069 (named_inputs) also edits input capture in observe.py, so whichever lands second may need a small rebase.
Verdict: looks good to me.
|
Now we just need to catch the maintainers’ attention 😄 |
Problem
@observe(capture_input=True)silently drops the first positional argument of a free function whenever any parameter is namedselforcls— including keyword-only ones:The function itself still receives the argument and returns the correct value; only the traced input is wrong. Both sync and async functions are affected.
Root cause
LangfuseDecorator._is_methodinlangfuse/_client/observe.pytreated a function as a method if"self"or"cls"appeared anywhere in the signature:_get_input_from_func_argsthen stripsfunc_args[0]as the implicit receiver — which doesn't exist for free functions.Fix
Only the first parameter indicates an unbound instance/class method, so
_is_methodnow checks just that:Genuine instance methods (
def m(self, ...)) and unbound classmethods (def m(cls, ...)) keep their existing behavior; bound methods never reach this code withself/clsvisible (inspect.signaturealready drops the bound argument), so there is no behavior change there.Verification
main(argscaptured as()) and passes with this fix for both sync and async paths.tests/unit/test_observe.py:clskeeps its first positional arg (sync + async),self,cls,selfis not treated as a method.tests/unit/test_observe.py: 20/20 passed.ruff check,ruff format --check, andmypyare clean on the changed files.Closes #18859


Confidence Score: 5/5
The PR appears safe to merge.Summary
Fixes
@observedropping the first argument of free functions that have a later parameter namedselforcls.No actionable issues found. Tests were not run during this review.
Reviews (1) · Last reviewed commit: "fix(observe): detect methods by first pa..." · Reviewed by Greptile