Repository navigation
Define __slots__ for object as Iterable[str] / Iterable[Union[str, unicode]] - #780
Conversation
| class object: | ||
| __doc__ = ... # type: Optional[str] | ||
| __class__ = ... # type: type | ||
| __slots__ = ... # type: Optional[Iterable[Union[str, unicode]]] |
There was a problem hiding this comment.
Tested with different types of iterables, including generators, this works. However, there is one surprise in the API of __slots__. Consider:
>>> class C(object):
... __slots__ = 'one'
...
>>> c = C()
>>> c.one = 'valid'
>>> c.o = 'invalid'
Traceback (most recent call last):
...
AttributeError: 'C' object has no attribute 'o'
Your type hint supports this use case implicitly because a string is also an iterable of strings. However, this suggests to the reader that providing 'one' should be equivalent to providing ('o', 'n', 'e') and it's not. I think it would be clearer to list the "str" and "unicode" types explicitly for that very reason.
There was a problem hiding this comment.
I did not expect that it would work the way it does :D Python 3 works in same manner, so I changed them both into Unions. Tnx!
| class object: | ||
| __doc__ = ... # type: Optional[str] | ||
| __class__ = ... # type: type | ||
| __slots__ = ... # type: Optional[Union[str, unicode, Iterable[Union[str, unicode]]]] |
There was a problem hiding this comment.
nit: since you're already doing a Union, it would be better to say:
Union[None, str, unicode, Iterable[str], Iterable[unicode]]
Much shorter :) Well, at least much flatter :)
|
Thanks! 🍰 |
|
Hm, apparently this went too far -- while |
No description provided.