Skip to content

Default implementation for Interface properties #39

Open
@aaronsnoswell

Description

@aaronsnoswell

Hello! Thanks for a great Python module.

I'm using this to define an interface with class properties (member variables). I would like to provide a default value for these variables (e.g. None), but I can't see a way to do this.

My attempt;

import interface


class IMyInterface(interface.Interface):
    @property
    def member_variable(self):
        pass

    @property
    @interface.default
    def optional_member_variable(self):
        return None


class Concrete(interface.implements(IMyInterface)):
    @property
    def member_variable(self):
        return 1.0


if __name__ == "__main__":
    c = Concrete()

Gives the error traceback;

Traceback (most recent call last):
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\site-packages\interface\interface.py", line 114, in __new__
    signature = TypedSignature(v)
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\site-packages\interface\typed_signature.py", line 28, in __init__
    self._signature = signature(extract_func(obj))
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\inspect.py", line 3065, in signature
    return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\inspect.py", line 2815, in from_callable
    follow_wrapper_chains=follow_wrapped)
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\inspect.py", line 2193, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: default(<function IMyInterface.optional_member_variable at 0x00000168036D5BF8>) is not a callable object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:/Development/unimodal-irl/unimodal_irl/envs/explicit_env.py", line 3, in <module>
    class IMyInterface(interface.Interface):
  File "C:\Users\uqasnosw\AppData\Local\Continuum\Miniconda3\envs\python36\lib\site-packages\interface\interface.py", line 122, in __new__
    raise_from(TypeError(errmsg), e)
  File "<string>", line 1, in raise_from
TypeError: Couldn't parse signature for field IMyInterface.optional_member_variable of type property.

If you can provide a little guidance on what form a fix might take, I'm happy to attempt a fix and submit a pull request!

Thank you,

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