Repository navigation
SupportsComplex and SupportsBytes? #68
Description
Activity
I'm lukewarm. I haven't really followed the use cases for SupportsInt. Also, is there a deep difference between these and collection.abc's Hashable, Iterable, Iterator, Sized, Container, which represent similar ideas but with one-off implementations? (And am I the only one confused by the fact that most of the SupportsXXX classes are named after a built-in type?)
There is no deep difference -- they are similar to
Sized.SupportsXxxmeans that the type can be passed toxxx(). For example,SupportsIntmeans that you can pass the value toint()(i.e., it defines__int__).If we didn't have those types, how would we specify that
Decimalis a valid argument toint()? We'd have to use something liked.__int__()(ifdhas typeDecimal) or a cast, both of which would be pretty ugly. On the other hand, there aren't that many types that can be passed toint(), so maybe that wouldn't be a big problem. Alternatively, we could allow any argument types toint, even those that would cause the call to fail at runtime.If we supported structural subtyping, we could easily just define internal, anonymous structural types for these functions and not expose them at all in typing. We can actually still kind of do that even without structural subtyping -- we could define types such as
_SupportsIntinternally and use them within our stubs (for things likeDecimal), but we wouldn't allow user-defined classes to add support forint().I'm not attached to the naming of these types -- if I remember correctly, it was you who proposed this convention :-) I can't think of a good name similar to
Sizedfor something that can be passed toint(). We could have something likeRoundablefor something that can be passed toround().OK, I think it's fine to add a few more, I'll add bytes and complex. If we find we need a lot more of these we should rethink this though.
Closed by revision a8cd08d. However, I noticed that
complex.__complex__andbytes.__bytes__don't exist. (Unlikeint.__int__,float.__float__, which do exist.)I filed http://bugs.python.org/issue24234 for the issue about eponymous methods.
Similar to SupportsInt and SupportsFloat, should typing define SupportsComplex and SupportsBytes?
For example: