{"id":9884,"library":"lazr-delegates","title":"lazr.delegates","description":"lazr.delegates provides a simple, declarative way to create Python objects that delegate behavior to other objects. It simplifies the implementation of common design patterns like composition, allowing classes to expose methods from 'delegatee' objects without boilerplate code. The current version is 2.1.1, and the library maintains a stable, albeit infrequent, release cadence, primarily focusing on Python compatibility and bug fixes.","status":"active","version":"2.1.1","language":"en","source_language":"en","source_url":"https://github.com/lazr/lazr.delegates","tags":["delegation","object-oriented","design-patterns","composition","decorator"],"install":[{"cmd":"pip install lazr-delegates","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary decorator `delegates` (plural) is meant for direct import.","wrong":"import lazr.delegates; lazr.delegates.delegates","symbol":"delegates","correct":"from lazr.delegates import delegates"},{"note":"The base class for advanced delegation scenarios.","symbol":"Delegate","correct":"from lazr.delegates import Delegate"}],"quickstart":{"code":"from lazr.delegates import delegates\n\nclass SoundMaker:\n    def make_sound(self):\n        return \"Woof!\"\n\n    def get_animal_type(self):\n        return \"Dog\"\n\n@delegates(SoundMaker)\nclass MyPet:\n    # MyPet now delegates make_sound and get_animal_type to SoundMaker\n    pass\n\npet = MyPet()\nprint(f\"Pet sound: {pet.make_sound()}\")\nprint(f\"Pet type: {pet.get_animal_type()}\")","lang":"python","description":"This example demonstrates how to use the `@delegates` decorator to easily expose methods from another class. `MyPet` delegates `make_sound` and `get_animal_type` to an instance of `SoundMaker` implicitly."},"warnings":[{"fix":"Ensure your project's Python environment is 3.8 or higher. If you need older Python support, use `lazr-delegates<2.0.0` (e.g., 1.0.3 for Python 3.7, 1.0.2 for Python 2.7).","message":"Version 2.0.0 dropped support for Python 2.x and Python 3.7. The library now requires Python 3.8 or newer.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Set `_DelegateType.delegated_attributes = True` on a custom delegate type, or pass `delegated_attributes=True` when defining the delegate type for `delegates`.","message":"By default, `lazr.delegates` primarily delegates methods. If you intend to delegate attributes (properties, instance variables), you need to explicitly configure the delegate type.","severity":"gotcha","affected_versions":"All"},{"fix":"Carefully design your class hierarchy. For complex cases, explicitly define the MRO if possible, or consider using custom `DelegateType` classes to control how methods are looked up and invoked. Test thoroughly with `inspect.getmro()`.","message":"When combining `lazr.delegates` with complex inheritance hierarchies, particularly multiple inheritance or explicit `super()` calls, method resolution order (MRO) can lead to unexpected behavior.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Verify that your class is decorated with `@delegates(DelegateeClass)` and that `DelegateeClass` correctly defines the method. Also, check for typos in the method name.","cause":"The `@delegates` decorator was either not applied to the class, or the class specified as the delegatee does not actually contain the method you are trying to call.","error":"AttributeError: 'MyPet' object has no attribute 'delegated_method'"},{"fix":"Use a direct import statement for the `delegates` decorator: `from lazr.delegates import delegates`. Then, apply it directly: `@delegates(MyDelegatee)`.","cause":"You likely imported the `lazr.delegates` module directly using `import lazr.delegates` and then attempted to call `lazr.delegates.delegates` as if it were a function or class.","error":"TypeError: 'module' object is not callable (when calling delegates as lazr.delegates.delegates)"},{"fix":"Inspect the `DelegateeClass` (e.g., `SoundMaker` in the quickstart) to ensure that `non_existent_method` actually exists and is spelled correctly. Adjust your code to only delegate methods that are present in the delegatee.","cause":"This warning occurs when the class you're delegating *to* (the delegatee) does not have a method that `lazr.delegates` expects to find, often due to a typo or misunderstanding of the delegatee's API.","error":"SyntaxWarning: lazr.delegates.delegates: 'MyClass' does not implement the delegated method 'non_existent_method'"}]}