characteristic icon indicating copy to clipboard operation
characteristic copied to clipboard

inscrutable error when passing too many parameters

Open glyph opened this issue 11 years ago • 8 comments

If I do this:

from characteristic import attributes

@attributes(["x"])
class X(object):
    pass

X(x=1, y=2)

I get this error:

Traceback (most recent call last):
  File "c.py", line 8, in <module>
    X(x=1, y=2)
  File ".../characteristic.py", line 128, in init
    self.__original_init__(*args, **kw)
TypeError: object.__init__() takes no parameters

This isn't a great way of saying parameter "y" not expected.

glyph avatar Jan 27 '15 05:01 glyph

Yeah, I’m not happy about that either. The problem is that the docs explicitly state that extra parameters are passed to the original __init__.

The original intent was to allow to write own constructors that cooperate with the generated initializer. In retrospect I regret it but I can’t remove it. :-/

hynek avatar Jan 27 '15 06:01 hynek

Would it be terrible to catch that particular error, and explain it some more? Like add to the message and say that the extra parameters kwargs (display) were passed to the original __init__? Then re-raise?

cyli avatar Jan 27 '15 07:01 cyli

Hm, how do you mean? Catch TypeErrors and then look at the error message? I mean there are several other error scenarios related to that. :-/

hynek avatar Jan 27 '15 07:01 hynek

Glyph points the flashlight up under his chin and uses a spooky ghost voice…

inspect.getargspec! Ooooooo!

glyph avatar Jan 27 '15 07:01 glyph

Inspecting the original __init__'s signature is one way to do it. Another way would be to special-case self.__original_init__[.im_func] is object.__init__, which at least rules out one pretty important case.

glyph avatar Jan 27 '15 07:01 glyph

OK, feel free to submit a PR. :)

hynek avatar Jan 27 '15 08:01 hynek

@hynek - any comment on which approach you'd prefer to see? or is it all the same to you?

glyph avatar Jan 27 '15 08:01 glyph

I think getargspec if you can make it work. That would enable you to raise a meaningful error message.

hynek avatar Jan 27 '15 13:01 hynek