Stewori/pytypes

error with @autoclass concerning default arguments' type

smarie opened this issue · 6 comments

I would like to provide an example showing how pytypes can be used with autoclass.
However the following code fails with a strange error:

from autoclass import autoclass, Boolean
from pytypes import typechecked
from numbers import Real, Integral
from typing import Optional

@typechecked
@autoclass
class HouseConfiguration(object):
    def __init__(self,
                 name: str,
                 surface: Real,
                 nb_floors: Optional[Integral] = 1,
                 with_windows: Boolean = False):
        pass

    # -- overriden setter for surface for custom validation
    @setter_override
    def surface(self, surface):
        assert surface > 0
        self._surface = surface

t = HouseConfiguration('test', 12, 2)  # error

The error received is :

Expected: Tuple[str, Real, Union[Integral, NoneType], Boolean]
Received: Tuple[str, int, int, int]

While this works:

t = HouseConfiguration('test', 12, nb_floors=2)

So it seems that this does not have anything to do with autoclass: somehow positional arguments / default values in the constructor signature are not handled properly.

Thanks for spotting this!
Interestingly

print (pytypes.is_of_type(('test', 12, 2, False), Tuple[str, Real, Optional[Integral], Boolean]))

works as expected.
So it must really be an issue with retrieving the right default value. I'll look into it...

Thanks @Stewori . Nice demonstration of is_of_type by the way :)

Just fixed it in e8864e6 I believe. It was due to a little flaw in index calculation for default values.
It would be nice if you could confirm this for your use case.

I confirm that this is fixed with the latest version from master.
Let me know when you release it on PyPi so that I update autoclass documentation accordingly

Note that pytypes 1.0b3 was released for PyPI yesterday. It contains this bugfix.

Thanks @Stewori ! I confirm that this fixes the issue.