iotile / typedargs

API typechecking and command line shell generation package
GNU Lesser General Public License v3.0
1 stars 2 forks source link

Add a support for parsing recursive return value formatters #83

Closed dvksirin closed 4 years ago

dvksirin commented 4 years ago

This PR makes possible to specify complex return value formatters in a docstring Returns stanza. For testing this feature I have also added a formatter method format_one_line() for map type class.

Example:

    @docannotate
    def func() -> Dict[User, int]:
        """
        Returns:
            dict show-as one_line[short_name, hex]: value description
        """
        return {User('john'): 15, User('bob'): 100}

Return value type would be taken from type annotations, value formatter - from a docstring. Dict[t, t] is internally mapped to our map class that has format_one_line formatter.

    def format_one_line(self, value: dict, key_formatter: str = None, val_formatter: str = None, **kwargs) -> str:
        """Get string representation for the passed dict object.

Formatter notation from a docstring would be parsed to base formatter one_line and sub-formatters short_name and hex that would be applied on dict keys and values accordingly.

Also added a test case to make sure this feature works.

Closes #61