hynek / characteristic

Please use attrs instead!
https://attrs.readthedocs.io/
MIT License
84 stars 19 forks source link

with_init support for positional arguments #7

Closed wallrj closed 10 years ago

wallrj commented 10 years ago

with_init doesn't currently seem to support positional arguments. Maybe that's deliberate, but it's not explained in the documentation and the error message isn't particularly helpful.

I personally prefer keyword arguments, because it makes the code more readable, but sometimes the name of the variable is clue enough eg

In [13]: from characteristic import attributes

In [14]: class Foo(object):
    pass
   ....: 

In [15]: Foo = attributes(['reactor','url'])(Foo)

In [16]: dummyReactor = object()

In [17]: url = b'http://www.example.com'

In [18]: Foo(dummyReactor, url)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-181d7803e7c2> in <module>()
----> 1 Foo(dummyReactor, url)

/home/richard/.virtualenvs/flocker-30/lib/python2.7/site-packages/characteristic.pyc in init(self, *args, **kw)
    124                     v = defaults[a]
    125                 except KeyError:
--> 126                     raise ValueError("Missing value for '{0}'.".format(a))
    127             setattr(self, a, v)
    128         self.__original_init__(*args, **kw)

ValueError: Missing value for 'reactor'.

So I propose adding support for positional arguments, but if not, add some better error handling and some documentation.

hynek commented 10 years ago

I will fix this in documentation.

Anything else would be backward-incompatible and lead to equivocal situations.