21
21
(1 , 0 + 0j ),
22
22
)
23
23
24
+ class ComplexSubclass (complex ):
25
+ pass
26
+
27
+ class MockComplex :
28
+ def __init__ (self , value ):
29
+ self .value = value
30
+ def __complex__ (self ):
31
+ return self .value
32
+
24
33
class ComplexTest (unittest .TestCase ):
25
34
26
35
def assertAlmostEqual (self , a , b ):
@@ -340,16 +349,13 @@ def test_conjugate(self):
340
349
self .assertClose (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
341
350
342
351
def test_constructor (self ):
343
- class NS :
344
- def __init__ (self , value ): self .value = value
345
- def __complex__ (self ): return self .value
346
- self .assertEqual (complex (NS (1 + 10j )), 1 + 10j )
347
- self .assertRaises (TypeError , complex , NS (None ))
352
+ self .assertEqual (complex (MockComplex (1 + 10j )), 1 + 10j )
353
+ self .assertRaises (TypeError , complex , MockComplex (None ))
348
354
self .assertRaises (TypeError , complex , {})
349
- self .assertRaises (TypeError , complex , NS (1.5 ))
350
- self .assertRaises (TypeError , complex , NS (1 ))
355
+ self .assertRaises (TypeError , complex , MockComplex (1.5 ))
356
+ self .assertRaises (TypeError , complex , MockComplex (1 ))
351
357
self .assertRaises (TypeError , complex , object ())
352
- self .assertRaises (TypeError , complex , NS (4.25 + 0.5j ), object ())
358
+ self .assertRaises (TypeError , complex , MockComplex (4.25 + 0.5j ), object ())
353
359
354
360
self .assertAlmostEqual (complex ("1+10j" ), 1 + 10j )
355
361
self .assertAlmostEqual (complex (10 ), 10 + 0j )
@@ -369,13 +375,33 @@ def __complex__(self): return self.value
369
375
self .assertAlmostEqual (complex (3.14 ), 3.14 + 0j )
370
376
self .assertAlmostEqual (complex (314 ), 314.0 + 0j )
371
377
self .assertAlmostEqual (complex (314 ), 314.0 + 0j )
372
- self .assertAlmostEqual (complex (3.14 + 0j , 0j ), 3.14 + 0j )
378
+ with self .assertWarnsRegex (DeprecationWarning ,
379
+ "argument 'imag' must be a real number, not complex" ):
380
+ self .assertAlmostEqual (complex (3.14 + 0j , 0j ), 3.14 + 0j )
373
381
self .assertAlmostEqual (complex (3.14 , 0.0 ), 3.14 + 0j )
374
382
self .assertAlmostEqual (complex (314 , 0 ), 314.0 + 0j )
375
383
self .assertAlmostEqual (complex (314 , 0 ), 314.0 + 0j )
376
- self .assertAlmostEqual (complex (0j , 3.14j ), - 3.14 + 0j )
377
- self .assertAlmostEqual (complex (0.0 , 3.14j ), - 3.14 + 0j )
378
- self .assertAlmostEqual (complex (0j , 3.14 ), 3.14j )
384
+ with self .assertWarnsRegex (DeprecationWarning ,
385
+ "argument 'real' must be a real number, not complex" ):
386
+ self .assertAlmostEqual (complex (0j , 3.14j ), - 3.14 + 0j )
387
+ with self .assertWarnsRegex (DeprecationWarning ,
388
+ "argument 'imag' must be a real number, not complex" ):
389
+ self .assertAlmostEqual (complex (0.0 , 3.14j ), - 3.14 + 0j )
390
+ with self .assertWarnsRegex (DeprecationWarning ,
391
+ "argument 'real' must be a real number, not complex" ):
392
+ self .assertAlmostEqual (complex (0j , 3.14 ), 3.14j )
393
+ with self .assertWarnsRegex (DeprecationWarning ,
394
+ "argument 'real' must be a real number, not complex" ):
395
+ self .assertAlmostEqual (complex (3.14 + 0j , 0 ), 3.14 + 0j )
396
+ with self .assertWarnsRegex (DeprecationWarning ,
397
+ "argument 'real' must be a real number, not .*MockComplex" ):
398
+ self .assertAlmostEqual (complex (MockComplex (3.14 + 0j ), 0 ), 3.14 + 0j )
399
+ with self .assertWarnsRegex (DeprecationWarning ,
400
+ "argument 'imag' must be a real number, not .*complex" ):
401
+ self .assertAlmostEqual (complex (0 , 3.14 + 0j ), 3.14j )
402
+ with self .assertRaisesRegex (TypeError ,
403
+ "argument 'imag' must be a real number, not .*MockComplex" ):
404
+ complex (0 , MockComplex (3.14 + 0j ))
379
405
self .assertAlmostEqual (complex (0.0 , 3.14 ), 3.14j )
380
406
self .assertAlmostEqual (complex ("1" ), 1 + 0j )
381
407
self .assertAlmostEqual (complex ("1j" ), 1j )
@@ -398,12 +424,32 @@ def __complex__(self): return self.value
398
424
self .assertEqual (complex ('1-1j' ), 1.0 - 1j )
399
425
self .assertEqual (complex ('1J' ), 1j )
400
426
401
- class complex2 (complex ): pass
402
- self .assertAlmostEqual (complex (complex2 (1 + 1j )), 1 + 1j )
427
+ self .assertAlmostEqual (complex (ComplexSubclass (1 + 1j )), 1 + 1j )
403
428
self .assertAlmostEqual (complex (real = 17 , imag = 23 ), 17 + 23j )
404
- self .assertAlmostEqual (complex (real = 17 + 23j ), 17 + 23j )
405
- self .assertAlmostEqual (complex (real = 17 + 23j , imag = 23 ), 17 + 46j )
406
- self .assertAlmostEqual (complex (real = 1 + 2j , imag = 3 + 4j ), - 3 + 5j )
429
+ with self .assertWarnsRegex (DeprecationWarning ,
430
+ "argument 'real' must be a real number, not complex" ):
431
+ self .assertAlmostEqual (complex (real = 17 + 23j ), 17 + 23j )
432
+ with self .assertWarnsRegex (DeprecationWarning ,
433
+ "argument 'real' must be a real number, not complex" ):
434
+ self .assertAlmostEqual (complex (real = 17 + 23j , imag = 23 ), 17 + 46j )
435
+ with self .assertWarnsRegex (DeprecationWarning ,
436
+ "argument 'real' must be a real number, not complex" ):
437
+ self .assertAlmostEqual (complex (real = 1 + 2j , imag = 3 + 4j ), - 3 + 5j )
438
+ with self .assertWarnsRegex (DeprecationWarning ,
439
+ "argument 'real' must be a real number, not complex" ):
440
+ self .assertAlmostEqual (complex (real = 3.14 + 0j ), 3.14 + 0j )
441
+ with self .assertWarnsRegex (DeprecationWarning ,
442
+ "argument 'real' must be a real number, not .*MockComplex" ):
443
+ self .assertAlmostEqual (complex (real = MockComplex (3.14 + 0j )), 3.14 + 0j )
444
+ with self .assertRaisesRegex (TypeError ,
445
+ "argument 'real' must be a real number, not str" ):
446
+ complex (real = '1' )
447
+ with self .assertRaisesRegex (TypeError ,
448
+ "argument 'real' must be a real number, not str" ):
449
+ complex ('1' , 0 )
450
+ with self .assertRaisesRegex (TypeError ,
451
+ "argument 'imag' must be a real number, not str" ):
452
+ complex (0 , '1' )
407
453
408
454
# check that the sign of a zero in the real or imaginary part
409
455
# is preserved when constructing from two floats. (These checks
@@ -432,8 +478,9 @@ def split_zeros(x):
432
478
self .assertRaises (TypeError , int , 5 + 3j )
433
479
self .assertRaises (TypeError , float , 5 + 3j )
434
480
self .assertRaises (ValueError , complex , "" )
435
- self .assertRaises (TypeError , complex , None )
436
- self .assertRaisesRegex (TypeError , "not 'NoneType'" , complex , None )
481
+ self .assertRaisesRegex (TypeError ,
482
+ "argument must be a string or a number, not NoneType" ,
483
+ complex , None )
437
484
self .assertRaises (ValueError , complex , "\0 " )
438
485
self .assertRaises (ValueError , complex , "3\09 " )
439
486
self .assertRaises (TypeError , complex , "1" , "2" )
@@ -453,11 +500,11 @@ def split_zeros(x):
453
500
self .assertRaises (ValueError , complex , ")1+2j(" )
454
501
self .assertRaisesRegex (
455
502
TypeError ,
456
- "first argument must be a string or a number, not ' dict' " ,
503
+ "argument 'real' must be a real number, not dict" ,
457
504
complex , {1 :2 }, 1 )
458
505
self .assertRaisesRegex (
459
506
TypeError ,
460
- "second argument must be a number, not ' dict' " ,
507
+ "argument 'imag' must be a real number, not dict" ,
461
508
complex , 1 , {1 :2 })
462
509
# the following three are accepted by Python 2.6
463
510
self .assertRaises (ValueError , complex , "1..1j" )
@@ -537,33 +584,28 @@ def test___complex__(self):
537
584
self .assertEqual (z .__complex__ (), z )
538
585
self .assertEqual (type (z .__complex__ ()), complex )
539
586
540
- class complex_subclass (complex ):
541
- pass
542
-
543
- z = complex_subclass (3 + 4j )
587
+ z = ComplexSubclass (3 + 4j )
544
588
self .assertEqual (z .__complex__ (), 3 + 4j )
545
589
self .assertEqual (type (z .__complex__ ()), complex )
546
590
547
591
@support .requires_IEEE_754
548
592
def test_constructor_special_numbers (self ):
549
- class complex2 (complex ):
550
- pass
551
593
for x in 0.0 , - 0.0 , INF , - INF , NAN :
552
594
for y in 0.0 , - 0.0 , INF , - INF , NAN :
553
595
with self .subTest (x = x , y = y ):
554
596
z = complex (x , y )
555
597
self .assertFloatsAreIdentical (z .real , x )
556
598
self .assertFloatsAreIdentical (z .imag , y )
557
- z = complex2 (x , y )
558
- self .assertIs (type (z ), complex2 )
599
+ z = ComplexSubclass (x , y )
600
+ self .assertIs (type (z ), ComplexSubclass )
559
601
self .assertFloatsAreIdentical (z .real , x )
560
602
self .assertFloatsAreIdentical (z .imag , y )
561
- z = complex (complex2 (x , y ))
603
+ z = complex (ComplexSubclass (x , y ))
562
604
self .assertIs (type (z ), complex )
563
605
self .assertFloatsAreIdentical (z .real , x )
564
606
self .assertFloatsAreIdentical (z .imag , y )
565
- z = complex2 (complex (x , y ))
566
- self .assertIs (type (z ), complex2 )
607
+ z = ComplexSubclass (complex (x , y ))
608
+ self .assertIs (type (z ), ComplexSubclass )
567
609
self .assertFloatsAreIdentical (z .real , x )
568
610
self .assertFloatsAreIdentical (z .imag , y )
569
611
@@ -645,9 +687,6 @@ def test(v, expected, test_fn=self.assertEqual):
645
687
test (complex (- 0. , - 0. ), "(-0-0j)" )
646
688
647
689
def test_pos (self ):
648
- class ComplexSubclass (complex ):
649
- pass
650
-
651
690
self .assertEqual (+ (1 + 6j ), 1 + 6j )
652
691
self .assertEqual (+ ComplexSubclass (1 , 6 ), 1 + 6j )
653
692
self .assertIs (type (+ ComplexSubclass (1 , 6 )), complex )