Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-03-15 Thread Rémi Lapeyre
Le 15 mars 2019 à 03:49:19, Steven D'Aprano (st...@pearwood.info(mailto:st...@pearwood.info)) a écrit: > On Wed, Mar 13, 2019 at 03:21:31AM -0700, Rémi Lapeyre wrote: > > > When __index__ is defined it means that there is a lossless conversion > > to int possible. In this case, this means a

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-03-14 Thread Steven D'Aprano
On Wed, Mar 13, 2019 at 03:21:31AM -0700, Rémi Lapeyre wrote: > When __index__ is defined it means that there is a lossless conversion > to int possible. In this case, this means a lossless conversion to > float and complex is also possible That's not correct: py> n = 2**64 + 1 py> n ==

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-03-13 Thread Rémi Lapeyre
Le 22 février 2019 à 18:14:01, Nick Coghlan (ncogh...@gmail.com(mailto:ncogh...@gmail.com)) a écrit: > On Fri, 22 Feb 2019 at 18:29, Serhiy Storchaka wrote: > > Should we add default implementations of __float__ and __complex__ when > > either __index__ or __int__ is defined? Currently: > > > >

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-02-22 Thread Nick Coghlan
On Fri, 22 Feb 2019 at 18:29, Serhiy Storchaka wrote: > Should we add default implementations of __float__ and __complex__ when > either __index__ or __int__ is defined? Currently: > > >>> class A: > ... def __int__(self): return 42 > ... > >>> int(A()) > 42 > >>> float(A()) > Traceback

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-02-22 Thread Serhiy Storchaka
18.02.19 18:16, Rémi Lapeyre пише: The documentation mentions at https://docs.python.org/3/reference/datamodel.html#object.__index__ the need to always define both __index__ and __int__:     Note: In order to have a coherent integer type class, when __index__() is defined __int__() should

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-02-19 Thread Rémi Lapeyre
Another point in favor of the change I just noticed is that int() accept objects defining __index__ as its `base` argument:     Python 3.7.2 (default, Jan 13 2019, 12:50:01)     [Clang 10.0.0 (clang-1000.11.45.5)] on darwin     Type "help", "copyright", "credits" or "license" for more

Re: [Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-02-19 Thread Nick Coghlan
On Tue, 19 Feb 2019 at 03:31, Rémi Lapeyre wrote: > Nick Coghlan proposes to make __int__ defaults to __index__ when only the > second > is defined and asked to open a discussion on python-dev before making any > change > "as the closest equivalent we have to this right now is the "negative" >

[Python-Dev] int() and math.trunc don't accept objects that only define __index__

2019-02-18 Thread Rémi Lapeyre
Hi, I open this thread to discuss the proposal by Nick Coghlan in https://bugs.python.org/issue33039 to add __int__ and __trunc__ to a type when __index__ is defined. Currently __int__ does not default to __index__ during class initialisation so both must be defined to get a coherant behavior: