bpo-44547: Make Fractions objects instances of typing.SupportsInt (GH… · python/cpython@d1b2477 (original) (raw)

`@@ -8,6 +8,7 @@

`

8

8

`import fractions

`

9

9

`import functools

`

10

10

`import sys

`

``

11

`+

import typing

`

11

12

`import unittest

`

12

13

`from copy import copy, deepcopy

`

13

14

`import pickle

`

`@@ -385,6 +386,47 @@ def testConversions(self):

`

385

386

``

386

387

`self.assertTypedEquals(0.1+0j, complex(F(1,10)))

`

387

388

``

``

389

`+

def testSupportsInt(self):

`

``

390

`+

See bpo-44547.

`

``

391

`+

f = F(3, 2)

`

``

392

`+

self.assertIsInstance(f, typing.SupportsInt)

`

``

393

`+

self.assertEqual(int(f), 1)

`

``

394

`+

self.assertEqual(type(int(f)), int)

`

``

395

+

``

396

`+

def testIntGuaranteesIntReturn(self):

`

``

397

`` +

Check that int(some_fraction) gives a result of exact type int

``

``

398

`+

even if the fraction is using some other Integral type for its

`

``

399

`+

numerator and denominator.

`

``

400

+

``

401

`+

class CustomInt(int):

`

``

402

`+

"""

`

``

403

`+

Subclass of int with just enough machinery to convince the Fraction

`

``

404

`+

constructor to produce something with CustomInt numerator and

`

``

405

`+

denominator.

`

``

406

`+

"""

`

``

407

+

``

408

`+

@property

`

``

409

`+

def numerator(self):

`

``

410

`+

return self

`

``

411

+

``

412

`+

@property

`

``

413

`+

def denominator(self):

`

``

414

`+

return CustomInt(1)

`

``

415

+

``

416

`+

def mul(self, other):

`

``

417

`+

return CustomInt(int(self) * int(other))

`

``

418

+

``

419

`+

def floordiv(self, other):

`

``

420

`+

return CustomInt(int(self) // int(other))

`

``

421

+

``

422

`+

f = F(CustomInt(13), CustomInt(5))

`

``

423

+

``

424

`+

self.assertIsInstance(f.numerator, CustomInt)

`

``

425

`+

self.assertIsInstance(f.denominator, CustomInt)

`

``

426

`+

self.assertIsInstance(f, typing.SupportsInt)

`

``

427

`+

self.assertEqual(int(f), 2)

`

``

428

`+

self.assertEqual(type(int(f)), int)

`

``

429

+

388

430

`def testBoolGuarateesBoolReturn(self):

`

389

431

`# Ensure that bool is used on numerator which guarantees a bool

`

390

432

`# return. See also bpo-39274.

`