Skip to content

Exception with static method #4

@marvin8

Description

@marvin8

Started using constable and loving it so far.

I have however noticed that constable seems to have issues with static methods. The following sample code produces an exception:

import constable


class Test:

    @staticmethod
    @constable.trace("a", "b", "c")
    def add(a: int, b: int) -> int:
        c = a + b
        return c


d = Test.add(a=1, b=2)
print(f"{d=}")

Following is the traceback when running the above code:

/home/mark/wip/feed2fedi/.venv/bin/python /home/mark/.config/JetBrains/PyCharmCE2024.1/scratches/constable_scratch.py 
Traceback (most recent call last):
  File "/home/mark/.config/JetBrains/PyCharmCE2024.1/scratches/constable_scratch.py", line 11, in <module>
    d = Test.add(a=1, b=2)
        ^^^^^^^^^^^^^^^^^^
  File "/home/mark/wip/feed2fedi/.venv/lib/python3.12/site-packages/constable/__init__.py", line 228, in wrapper
    ret = executor.execute()
          ^^^^^^^^^^^^^^^^^^
  File "/home/mark/wip/feed2fedi/.venv/lib/python3.12/site-packages/constable/__init__.py", line 190, in execute
    result = fn(*self.fn_wrapper.args, **self.fn_wrapper.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mark/wip/feed2fedi/.venv/lib/python3.12/site-packages/constable/__init__.py", line 225, in wrapper
    fn_wrapper = FunctionWrapper(func, args, kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mark/wip/feed2fedi/.venv/lib/python3.12/site-packages/constable/__init__.py", line 43, in __init__
    self.source_code_lines = inspect.getsource(func).splitlines()
                             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mark/.rye/py/cpython@3.12.2/install/lib/python3.12/inspect.py", line 1282, in getsource
    lines, lnum = getsourcelines(object)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mark/.rye/py/cpython@3.12.2/install/lib/python3.12/inspect.py", line 1264, in getsourcelines
    lines, lnum = findsource(object)
                  ^^^^^^^^^^^^^^^^^^
  File "/home/mark/.rye/py/cpython@3.12.2/install/lib/python3.12/inspect.py", line 1093, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions