smarie/python-autoclass

@autoprops argument name in setter is not correct

smarie opened this issue · 1 comments

The setter function generated by @autoprops has always the same argument name : 'val'.
This was primarily done so that there is no dynamic compilation, but it leads to confusing error messages when used in combination with PEP484 type checkers:

from autoclass import autoclass
from numbers import Integral

# we use enforce runtime checker for this example
from enforce import runtime_validation, config
config(dict(mode='covariant'))  # to accept subclasses in validation

@runtime_validation
@autoclass
class House:
    def __init__(self, name: str, nb_floors: Integral = 1):
        pass

obj = House('my_house', 'red')

Leads to the following error message:

enforce.exceptions.RuntimeTypeError: 
  The following runtime type errors were encountered:
       Argument 'val' was not of type <class 'numbers.Integral'>. Actual type was str.

The argument name is val in the error message, not nb_floors.

The only way to fix this is to dynamically compile a setter function, but in order to preserve debug-ability we'll make that setter function simply wrap an inline, not dynamically compiled, function.