Skip to content

Skip classmethods wrapping non-callable descriptors when wiring - #991

Open
RClaudiuM wants to merge 1 commit into
ets-labs:developfrom
RClaudiuM:fix/950-wire-non-callable-classmethod
Open

RClaudiuM wants to merge 1 commit into
ets-labs:developfrom
RClaudiuM:fix/950-wire-non-callable-classmethod

Conversation

@RClaudiuM

Copy link
Copy Markdown

Fixes #950

wire() crashes when a wired module contains a class with a classmethod (or staticmethod) that wraps another descriptor, e.g. functools.singledispatchmethod:

class Model:
    @classmethod
    @singledispatchmethod
    def validate(cls, param): ...
TypeError: <functools.singledispatchmethod object at 0x...> is not a callable object

_patch_method takes method.__func__ from the classmethod/staticmethod and passes it to inspect.signature, but here __func__ is the (non-callable) singledispatchmethod descriptor. There is nothing to inject into such a member, so _patch_method now returns early when __func__ is not callable, leaving the attribute untouched.

Tests

Added tests/unit/wiring/test_classmethod_wrapping_descriptor_py36.py.

  • Before: pytest tests/unit/wiring/test_classmethod_wrapping_descriptor_py36.py fails with the TypeError above (Python 3.11 and 3.13).
  • After: passes on 3.11 and 3.13; full pytest on 3.13: 1388 passed, 2 skipped. flake8 src/dependency_injector/ is clean.

A classmethod (or staticmethod) wrapping another descriptor such as
functools.singledispatchmethod has a non-callable __func__, which made
wire() crash with "TypeError: ... is not a callable object" while
inspecting its signature. There is nothing to inject into such members,
so leave them untouched.

Fixes ets-labs#950

This branch has not been deployed

No deployments
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.

1 participant