@@ -375,99 +375,8 @@ main, line 9: Incompatible types in assignment (expression has type A[A[B]], var
375
375
-- -----------------
376
376
377
377
378
- [case testTrivialTypeApplication]
379
- from typing import TypeVar
380
- T = TypeVar('T')
381
- def f(x: T) -> None: pass
382
- f[object](None)
383
- [out]
384
-
385
- [case testSimpleGenericFunction]
386
- from typing import Undefined, TypeVar
387
- T = TypeVar('T')
388
- class A: pass
389
- class B: pass
390
- a = Undefined # type: A
391
- b = Undefined # type: B
392
-
393
- def id(a: T) -> T:
394
- return a
395
-
396
- b = id[B](a) # E: Argument 1 to "id" has incompatible type "A"; expected "B"
397
- a = id[B](b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
398
- a = id[A](a)
399
-
400
- [case testGenericFunctionWithTwoTypeArguments]
401
- from typing import Undefined, TypeVar, Generic
402
- S = TypeVar('S')
403
- T = TypeVar('T')
404
-
405
- class t(Generic[S, T]): pass
406
- class B: pass
407
- class C: pass
408
-
409
- tbc = Undefined # type: t[B, C]
410
- tbb = Undefined # type: t[B, B]
411
- b = Undefined # type: B
412
- c = Undefined # type: C
413
-
414
- def f(a: S, b: t[S, T]) -> t[S, T]: pass
415
-
416
- tbc = f[B, C](c, tbc) # E: Argument 1 to "f" has incompatible type "C"; expected "B"
417
- tbc = f[B, C](b, tbb) # E: Argument 2 to "f" has incompatible type t[B, B]; expected t[B, C]
418
- tbb = f[B, C](b, tbc) # E: Incompatible types in assignment (expression has type t[B, C], variable has type t[B, B])
419
- tbc = f[B, C](b, tbc)
420
-
421
- [case testInvalidNumberOfTypeArgumentsForGenericFunction]
422
- from typing import Undefined, TypeVar, Generic
423
- s = TypeVar('s')
424
- t = TypeVar('t')
425
- class p(Generic[s, t]): pass
426
- def f(a: p[s, t]) -> None: pass
427
- def g() -> None: pass
428
- a = Undefined # type: p[object, object]
429
-
430
- a = f[object](a) # E: Type application has too few types (2 expected)
431
- a = f[object, object, object](a) # E: Type application has too many types (2 expected)
432
- g[object]() # E: Type application targets a non-generic function
433
- f[object, object](a)
434
-
435
- -- NOTE: Commented out test cases that do not make sense currently.
436
-
437
- --[case testGenericMethodInNonGenericType]
438
- --from typing import Undefined, TypeVar
439
- --T = TypeVar('T')
440
- --a, b = Undefined, Undefined # type: (A, B)
441
- --class A:
442
- -- def f(self, a: T) -> T: pass
443
- --class B: pass
444
- --
445
- --a = a.f[B](b) # E: Incompatible types in assignment
446
- --a = a.f[A](b) # E: Argument 1 to "f" of "A" has incompatible type "B"
447
- --a = a.f[A](a)
448
-
449
- --[case testGenericMethodInGenericType]
450
- --from typing import Undefined, TypeVar, Generic
451
- --T = TypeVar('T')
452
- --S = TypeVar('S')
453
- --class A(Generic[T]):
454
- -- def f(self, a: S) -> 'A[t[S, T]]':
455
- -- pass
456
- --class t(Generic[T, S]): pass
457
- --class B: pass
458
- --class C: pass
459
- --ab = Undefined # type: A[B]
460
- --acb = Undefined # type: A[t[C, B]]
461
- --abc = Undefined # type: A[t[B, C]]
462
- --b = Undefined # type: B
463
- --c = Undefined # type: C
464
- --
465
- --abc = ab.f[C](c) # E: Incompatible types in assignment
466
- --acb = ab.f[C](b) # E: Argument 1 to "f" of "A" has incompatible type "B"
467
- --acb = ab.f[C](c)
468
-
469
378
[case testTypeCheckingGenericFunctionBody]
470
- from typing import TypeVar, Generic
379
+ from typing import TypeVar, Generic, Undefined
471
380
S = TypeVar('S')
472
381
T = TypeVar('T')
473
382
class A: pass
@@ -476,118 +385,69 @@ class p(Generic[T, S]):
476
385
def f(s: S, t: T) -> p[T, A]:
477
386
a = t # type: S # E: Incompatible types in assignment (expression has type "T", variable has type "S")
478
387
s = t # E: Incompatible types in assignment (expression has type "T", variable has type "S")
479
- return p[S, A](s, A()) # E: Incompatible return value type: expected __main__.p[T`-2, __main__.A], got __main__.p[S`-1, __main__.A*]
388
+ p_s_a = Undefined(p[S, A])
389
+ if s:
390
+ return p_s_a # E: Incompatible return value type: expected __main__.p[T`-2, __main__.A], got __main__.p[S`-1, __main__.A]
480
391
b = t # type: T
481
392
c = s # type: S
482
- return p[T, A](t, A())
393
+ p_t_a = Undefined(p[T, A])
394
+ return p_t_a
483
395
[out]
484
396
main: In function "f":
485
397
486
398
[case testTypeCheckingGenericMethodBody]
487
- from typing import TypeVar, Generic
399
+ from typing import TypeVar, Generic, Undefined
488
400
T = TypeVar('T')
489
401
S = TypeVar('S')
490
402
class p(Generic[T, S]):
491
403
def __init__(self, t: T, a: S) -> None: pass
492
404
class A(Generic[T]):
493
405
def f(self, s: S, t: T) -> p[S, T]:
494
406
s = t # E: Incompatible types in assignment (expression has type "T", variable has type "S")
495
- return p[S, S](s, s) # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[S`-1, S`-1]
496
- return p[T, T](t, t) # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[T`1, T`1]
407
+ p_s_s = Undefined(p[S, S])
408
+ if s:
409
+ return p_s_s # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[S`-1, S`-1]
410
+ p_t_t = Undefined(p[T, T])
411
+ if s:
412
+ return p_t_t # E: Incompatible return value type: expected __main__.p[S`-1, T`1], got __main__.p[T`1, T`1]
497
413
t = t
498
414
s = s
499
- return p[S, T](s, t)
415
+ p_s_t = Undefined(p[S, T])
416
+ return p_s_t
500
417
[out]
501
418
main: In member "f" of class "A":
502
419
503
420
504
- -- Constructing instances + generics
505
- -- ---------------------------------
421
+ -- Generic types in expressions
422
+ -- ----------------------------
506
423
507
424
508
- [case testConstructingGenericInstanceWithEmptyConstructor ]
425
+ [case testInvalidTypeApplicationTarget ]
509
426
from typing import TypeVar, Generic
510
427
T = TypeVar('T')
511
428
class A(Generic[T]): pass
512
- class B: pass
513
- class C: pass
514
- a = A[B]() # type: A[C] # Fail
515
- a = A[B]() # Fail
516
- e = A[B]() # type: A[B]
517
- e = A[B]()
429
+ A[A]() # E: Generic type not valid as an expression any more (use '# type:' comment instead)
430
+ A[int, str]() # E: Generic type not valid as an expression any more (use '# type:' comment instead)
518
431
[out]
519
- main, line 6: Incompatible types in assignment (expression has type A[B], variable has type A[C])
520
- main, line 7: Incompatible types in assignment (expression has type A[B], variable has type A[C])
521
432
522
- [case testNonEmptyGenericTypeConstructor ]
523
- from typing import Undefined, TypeVar, Generic
433
+ [case testInvalidTypeApplicationTarget2 ]
434
+ from typing import TypeVar, Generic
524
435
T = TypeVar('T')
525
- class B: pass
526
- class C: pass
527
- class A(Generic[T]):
528
- def __init__(self, a: T, b: B) -> None: pass
529
- ac = Undefined # type: A[C]
530
- ab = Undefined # type: A[B]
531
- b = Undefined # type: B
532
- c = Undefined # type: C
533
-
534
- ac = A[C](b, b) # E: Argument 1 to "A" has incompatible type "B"; expected "C"
535
- ab = A[C](c, b) # E: Incompatible types in assignment (expression has type A[C], variable has type A[B])
536
- ac = A[C](c, b)
537
-
538
- [case testConstructorForGenericTypeWithMultipleArguments]
539
- from typing import Undefined, TypeVar, Generic
540
436
S = TypeVar('S')
541
- T = TypeVar('T')
542
- class A(Generic[S, T]):
543
- def __init__(self, t: T, s: S) -> None: pass
544
- class B: pass
545
- class C: pass
546
-
547
- abc = Undefined # type: A[B, C]
548
- acb = Undefined # type: A[C, B]
549
- b, c = Undefined, Undefined # type: (B, C)
550
-
551
- abc = A[B, C](b, b) # E: Argument 1 to "A" has incompatible type "B"; expected "C"
552
- abc = A[B, C](c, c) # E: Argument 2 to "A" has incompatible type "C"; expected "B"
553
- acb = A[B, C](c, b) # E: Incompatible types in assignment (expression has type A[B, C], variable has type A[C, B])
554
- abc = A[B, C](c, b)
555
- acb = A[C, B](b, c)
556
-
557
- [case testGenericConstructorOfNonGenericType]
558
- from typing import Undefined, TypeVar
559
- T = TypeVar('T')
560
- class A:
561
- def __init__(self, a: T, b: T) -> None: pass
562
- class B: pass
563
- class C: pass
564
- a, b, c = Undefined, Undefined, Undefined # type: (A, B, C)
565
-
566
- a = A[B](b, c) # E: Argument 2 to "A" has incompatible type "C"; expected "B"
567
- a = A[B](c, b) # E: Argument 1 to "A" has incompatible type "C"; expected "B"
568
- b = A[B](b, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
569
-
570
- a = A[B](b, b)
571
- a = A[C](c, c)
437
+ class A(Generic[T, S]): pass
438
+ A[int, str]() # E: Generic type not valid as an expression any more (use '# type:' comment instead)
439
+ A[int]() # E: Generic type not valid as an expression any more (use '# type:' comment instead)
440
+ [out]
572
441
573
- [case testInvalidTypeApplicationTarget ]
442
+ [case testInvalidTypeApplicationTarget3 ]
574
443
from typing import Undefined
575
444
a = Undefined # type: A
576
445
class A: pass
577
446
a[A]() # Fail
578
447
A[A]() # Fail
579
448
[out]
580
449
main, line 4: Value of type "A" is not indexable
581
- main, line 5: Type application targets a non-generic function
582
-
583
- [case testInvalidNumberOfGenericArgsInTypeApplication]
584
- from typing import TypeVar, Generic
585
- T = TypeVar('T')
586
- S = TypeVar('S')
587
- class A(Generic[T, S]): pass
588
- x = A[int, str, int]() # E: Type application has too many types (2 expected)
589
- x = A[int]() # E: Type application has too few types (2 expected)
590
- [out]
450
+ main, line 5: Generic type not valid as an expression any more (use '# type:' comment instead)
591
451
592
452
593
453
-- Multiple assignment with lists
@@ -603,20 +463,26 @@ a = Undefined # type: A
603
463
b = Undefined # type: B
604
464
b2 = Undefined # type: B2
605
465
606
- a, b = List[A]() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
607
- b, a = List[A]() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
608
- b2, b2 = List[B]() # E: Incompatible types in assignment (expression has type "B", variable has type "B2")
466
+ list_a = [a]
467
+ list_b = [b]
468
+ list_b2 = [b2]
609
469
610
- a, a = List[A]()
611
- b, b2, b = List[B2]()
470
+ a, b = list_a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
471
+ b, a = list_a # E: Incompatible types in assignment (expression has type "A", variable has type "B")
472
+ b2, b2 = list_b # E: Incompatible types in assignment (expression has type "B", variable has type "B2")
473
+
474
+ a, a = list_a
475
+ b, b2, b = list_b2
612
476
[builtins fixtures/for.py]
613
477
614
478
[case testMultipleAssignmentWithListsInInitialization]
615
479
from typing import List
616
480
class A: pass
617
- a, b = List[object]() # type: (A, object) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
618
- c, d = List[object]() # type: (object, A) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
619
- e, f = List[A]() # type: (A, object)
481
+ list_object = [object()]
482
+ list_a = [A()]
483
+ a, b = list_object # type: (A, object) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
484
+ c, d = list_object # type: (object, A) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
485
+ e, f = list_a # type: (A, object)
620
486
[builtins fixtures/for.py]
621
487
622
488
[case testMultipleAssignmentWithListAndIndexing]
@@ -743,22 +609,6 @@ b = f([b])
743
609
b = f(b)
744
610
[builtins fixtures/list.py]
745
611
746
- [case testOverloadedGenericFunction]
747
- from typing import overload, TypeVar, Undefined
748
- t = TypeVar('t')
749
- class A: pass
750
- class B: pass
751
-
752
- @overload
753
- def f(x: int, a: A) -> A: pass
754
- @overload
755
- def f(x: str, a: t) -> t: pass
756
-
757
- a, b = Undefined, Undefined # type: (A, B)
758
- b = f[B]('', b)
759
- a = f[B]('', b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
760
- f[B](1, a) # E: No overload variant of "f" matches argument types
761
-
762
612
763
613
-- Type variable scoping
764
614
-- ---------------------
0 commit comments