[issue44547] fraction.Fraction does not implement __int__.

2021-10-21 Thread Łukasz Langa
Łukasz Langa added the comment: Thanks for the patch, Mark! ✨ 🍰 ✨ -- resolution: -> fixed stage: patch review -> resolved status: open -> closed ___ Python tracker ___ ___

[issue44547] fraction.Fraction does not implement __int__.

2021-10-21 Thread Łukasz Langa
Łukasz Langa added the comment: New changeset d1b24775b462f4f28aa4929fd031899170793388 by Mark Dickinson in branch 'main': bpo-44547: Make Fractions objects instances of typing.SupportsInt (GH-27851) https://github.com/python/cpython/commit/d1b24775b462f4f28aa4929fd031899170793388 --

[issue44547] fraction.Fraction does not implement __int__.

2021-08-22 Thread Mark Dickinson
Mark Dickinson added the comment: I think there's no reason not to keep __trunc__ and math.trunc - they're natural counterparts to floor and ceil, and there's probably at least some code out there already using math.trunc. It's the involvement of __trunc__ in the int() builtin that I'd quite

[issue44547] fraction.Fraction does not implement __int__.

2021-08-20 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: Fraction.__int__ = Fraction.__trunc__ may not work because __trunc__() can return any object with __index__, while __int__ should return an exact int (not even an int subclass). -- ___ Python tracker

[issue44547] fraction.Fraction does not implement __int__.

2021-08-20 Thread Mark Dickinson
Change by Mark Dickinson : -- keywords: +patch pull_requests: +26311 stage: -> patch review pull_request: https://github.com/python/cpython/pull/27851 ___ Python tracker ___ _

[issue44547] fraction.Fraction does not implement __int__.

2021-07-06 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: On other hand, there are classes which define __int__ but not __trunc__: UUID and IP4Address. So it makes sense to keep separate __int__ and __trunc__. -- ___ Python tracker

[issue44547] fraction.Fraction does not implement __int__.

2021-07-05 Thread Raymond Hettinger
Raymond Hettinger added the comment: > I am wondering whether one of __int__ or __trunc__ should be deprecated. I would not miss __trunc__. -- nosy: +rhettinger ___ Python tracker __

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: In ideal world we would use __int__ for (1), and __trunc__ for (2). But for some historical reasons __index__ was introduced for (1) and __int__ is only used in the int constructor, although it falls back to __trunc__. I am wondering whether one of __int__

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Mark Dickinson
Mark Dickinson added the comment: Thanks, that's helpful. I guess what you _really_ want there is a duck-typed "tell me whether this value is integral and if so give me the corresponding Python int", but that's not currently easily available, so I suppose x == int(x) is the next-best thing.

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Michael Amrhein
Michael Amrhein added the comment: The background is an implementation of __pow__ for a fixed-point decimal number: SupportsIntOrFloat = Union[SupportsInt, SupportsFloat] def __pow__(self, other: SupportsIntOrFloat, mod: Any = None) -> Complex: if isinstance(other, SupportsInt): e

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Mark Dickinson
Mark Dickinson added the comment: > As another data point, complex supporting __int__ is a little bit of an > oddity, since all that __int__ method does is raise a TypeError. This was fixed in 3.10: #41974 -- ___ Python tracker

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Mark Dickinson
Mark Dickinson added the comment: > Maybe typing.SupportsIndex Apologies: that already exists, of course. It was introduced in #36972. -- ___ Python tracker ___ _

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Mark Dickinson
Mark Dickinson added the comment: I'm actually struggling to think of situations where typing.SupportsInt would be useful in its current form: if I'm writing a function that wants to do a duck-typed acceptance of integer-like things (for example because I want my function to work with NumPy

[issue44547] fraction.Fraction does not implement __int__.

2021-07-02 Thread Mark Dickinson
Mark Dickinson added the comment: FWIW, there's some history here: there's a good reason that fractions.Fraction didn't originally implement __int__. Back in the Bad Old Days, many Python functions that expected an integer would accept anything whose type implemented __int__ instead, and cal

[issue44547] fraction.Fraction does not implement __int__.

2021-07-01 Thread Josh Rosenberg
Josh Rosenberg added the comment: Seems like an equally reasonable solution would be to make class's with __trunc__ but not __int__ automatically generate a __int__ in terms of __trunc__ (similar to __str__ using __repr__ when the latter is defined but not the former). The inconsistency is i

[issue44547] fraction.Fraction does not implement __int__.

2021-07-01 Thread Michael Amrhein
New submission from Michael Amrhein : While int, float, complex and Decimal implement __int__, Fraction does not. Thus, checking for typing.SupportsInt for fractions fails, although int() succeeds, because Fraction implements __trunc__. This looks inconsistent. Easiest fix seems to be: Fractio