| 1 |
-- Test cases for the type checker related to subtyping and inheritance with |
| 2 |
-- generics. |
| 3 |
|
| 4 |
|
| 5 |
-- Subtyping + inheritance |
| 6 |
-- ----------------------- |
| 7 |
|
| 8 |
|
| 9 |
[case testSubtypingAndInheritingNonGenericTypeFromGenericType] |
| 10 |
from typing import TypeVar, Generic |
| 11 |
T = TypeVar('T') |
| 12 |
ac: A[C] |
| 13 |
ad: A[D] |
| 14 |
b: B |
| 15 |
|
| 16 |
if int(): |
| 17 |
b = ad # E: Incompatible types in assignment (expression has type "A[D]", variable has type "B") |
| 18 |
ad = b # E: Incompatible types in assignment (expression has type "B", variable has type "A[D]") |
| 19 |
if int(): |
| 20 |
b = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B") |
| 21 |
|
| 22 |
if int(): |
| 23 |
b = b |
| 24 |
ac = b |
| 25 |
|
| 26 |
class C: pass |
| 27 |
class A(Generic[T]): pass |
| 28 |
class B(A[C]): pass |
| 29 |
class D: pass |
| 30 |
|
| 31 |
[case testSubtypingAndInheritingGenericTypeFromNonGenericType] |
| 32 |
from typing import TypeVar, Generic |
| 33 |
T = TypeVar('T') |
| 34 |
a: A |
| 35 |
bc: B[C] |
| 36 |
bd: B[D] |
| 37 |
|
| 38 |
if int(): |
| 39 |
bc = bd # E: Incompatible types in assignment (expression has type "B[D]", variable has type "B[C]") |
| 40 |
bd = bc # E: Incompatible types in assignment (expression has type "B[C]", variable has type "B[D]") |
| 41 |
if int(): |
| 42 |
bc = a # E: Incompatible types in assignment (expression has type "A", variable has type "B[C]") |
| 43 |
bd = a # E: Incompatible types in assignment (expression has type "A", variable has type "B[D]") |
| 44 |
|
| 45 |
if int(): |
| 46 |
a = bc |
| 47 |
if int(): |
| 48 |
a = bd |
| 49 |
|
| 50 |
class A: pass |
| 51 |
class B(A, Generic[T]): pass |
| 52 |
class C: pass |
| 53 |
class D: pass |
| 54 |
|
| 55 |
[case testSubtypingAndInheritingGenericTypeFromGenericType] |
| 56 |
from typing import TypeVar, Generic |
| 57 |
T = TypeVar('T') |
| 58 |
S = TypeVar('S') |
| 59 |
ac: A[C] |
| 60 |
ad: A[D] |
| 61 |
bcc: B[C, C] |
| 62 |
bdc: B[D, C] |
| 63 |
|
| 64 |
if int(): |
| 65 |
ad = bcc # E: Incompatible types in assignment (expression has type "B[C, C]", variable has type "A[D]") |
| 66 |
if int(): |
| 67 |
ad = bdc # E: Incompatible types in assignment (expression has type "B[D, C]", variable has type "A[D]") |
| 68 |
bcc = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B[C, C]") |
| 69 |
bdc = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B[D, C]") |
| 70 |
|
| 71 |
if int(): |
| 72 |
bcc = bcc |
| 73 |
bdc = bdc |
| 74 |
ac = bcc |
| 75 |
if int(): |
| 76 |
ac = bdc |
| 77 |
|
| 78 |
class A(Generic[T]): pass |
| 79 |
class B(A[S], Generic[T, S]): pass |
| 80 |
class C: pass |
| 81 |
class D: pass |
| 82 |
|
| 83 |
[case testSubtypingAndInheritingGenericTypeFromGenericTypeAcrossHierarchy] |
| 84 |
from typing import TypeVar, Generic |
| 85 |
T = TypeVar('T') |
| 86 |
S = TypeVar('S') |
| 87 |
X = TypeVar('X') |
| 88 |
Y = TypeVar('Y') |
| 89 |
ae: A[A[E]] |
| 90 |
af: A[A[F]] |
| 91 |
|
| 92 |
cef: C[E, F] |
| 93 |
cff: C[F, F] |
| 94 |
cfe: C[F, E] |
| 95 |
|
| 96 |
if int(): |
| 97 |
ae = cef # E: Incompatible types in assignment (expression has type "C[E, F]", variable has type "A[A[E]]") |
| 98 |
af = cfe # E: Incompatible types in assignment (expression has type "C[F, E]", variable has type "A[A[F]]") |
| 99 |
|
| 100 |
if int(): |
| 101 |
ae = cfe |
| 102 |
af = cef |
| 103 |
if int(): |
| 104 |
af = cff |
| 105 |
|
| 106 |
class A(Generic[T]): pass |
| 107 |
class B(A[S], Generic[T, S]): pass |
| 108 |
class C(B[A[X], A[Y]], Generic[X, Y]): pass |
| 109 |
class E: pass |
| 110 |
class F: pass |
| 111 |
|
| 112 |
[case testIncludingBaseClassTwice] |
| 113 |
from typing import TypeVar, Generic |
| 114 |
t = TypeVar('t') |
| 115 |
class I(Generic[t]): pass |
| 116 |
class A(I[C], I[object]): pass # E: Duplicate base class "I" |
| 117 |
class C: pass |
| 118 |
|
| 119 |
|
| 120 |
-- Accessing inherited generic members |
| 121 |
-- ----------------------------------- |
| 122 |
|
| 123 |
|
| 124 |
[case testAccessingMethodInheritedFromGenericType] |
| 125 |
from typing import TypeVar, Generic |
| 126 |
T = TypeVar('T') |
| 127 |
S = TypeVar('S') |
| 128 |
b: B[C, D] |
| 129 |
c: C |
| 130 |
d: D |
| 131 |
|
| 132 |
b.f(c) # E: Argument 1 to "f" of "A" has incompatible type "C"; expected "D" |
| 133 |
b.f(d) |
| 134 |
|
| 135 |
class A(Generic[T]): |
| 136 |
def f(self, a: T) -> None: |
| 137 |
pass |
| 138 |
class B(A[S], Generic[T, S]): pass |
| 139 |
class C: pass |
| 140 |
class D: pass |
| 141 |
[builtins fixtures/tuple.pyi] |
| 142 |
|
| 143 |
[case testAccessingMethodInheritedFromGenericTypeInNonGenericType] |
| 144 |
from typing import TypeVar, Generic |
| 145 |
T = TypeVar('T') |
| 146 |
b: B |
| 147 |
c: C |
| 148 |
d: D |
| 149 |
|
| 150 |
b.f(c) # E: Argument 1 to "f" of "A" has incompatible type "C"; expected "D" |
| 151 |
b.f(d) |
| 152 |
|
| 153 |
class C: pass |
| 154 |
class D: pass |
| 155 |
class A(Generic[T]): |
| 156 |
def f(self, a: T) -> None: |
| 157 |
pass |
| 158 |
class B(A[D]): pass |
| 159 |
[builtins fixtures/tuple.pyi] |
| 160 |
|
| 161 |
[case testAccessingMemberVarInheritedFromGenericType] |
| 162 |
from typing import TypeVar, Generic |
| 163 |
T = TypeVar('T') |
| 164 |
S = TypeVar('S') |
| 165 |
class A(Generic[T]): |
| 166 |
def __init__(self, a: T) -> None: |
| 167 |
self.a = a |
| 168 |
|
| 169 |
b: B[C, D] |
| 170 |
c: C |
| 171 |
d: D |
| 172 |
|
| 173 |
b.a = c # E: Incompatible types in assignment (expression has type "C", variable has type "D") |
| 174 |
b.a = d |
| 175 |
|
| 176 |
class B(A[S], Generic[T, S]): pass |
| 177 |
class C: pass |
| 178 |
class D: pass |
| 179 |
[builtins fixtures/tuple.pyi] |
| 180 |
|
| 181 |
|
| 182 |
-- Overriding with generic types |
| 183 |
-- ----------------------------- |
| 184 |
|
| 185 |
|
| 186 |
[case testOverridingMethodInSimpleTypeInheritingGenericType] |
| 187 |
from typing import TypeVar, Generic |
| 188 |
T = TypeVar('T') |
| 189 |
class B(Generic[T]): |
| 190 |
def f(self, a: T) -> None: pass |
| 191 |
def g(self, a: T) -> None: pass |
| 192 |
class C: pass |
| 193 |
class D: pass |
| 194 |
class A(B[C]): |
| 195 |
def f(self, a: D) -> None: pass \ |
| 196 |
# E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "C" \ |
| 197 |
# N: This violates the Liskov substitution principle \ |
| 198 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 199 |
def g(self, a: C) -> None: pass |
| 200 |
[out] |
| 201 |
|
| 202 |
[case testOverridingMethodInGenericTypeInheritingSimpleType] |
| 203 |
from typing import TypeVar, Generic |
| 204 |
T = TypeVar('T') |
| 205 |
class C: pass |
| 206 |
class B: |
| 207 |
def f(self, a: C) -> None: pass |
| 208 |
def g(self, a: C) -> None: pass |
| 209 |
class A(B, Generic[T]): |
| 210 |
def f(self, a: T) -> None: pass \ |
| 211 |
# E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "C" \ |
| 212 |
# N: This violates the Liskov substitution principle \ |
| 213 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 214 |
def g(self, a: 'C') -> None: pass |
| 215 |
[out] |
| 216 |
|
| 217 |
[case testOverridingMethodInGenericTypeInheritingGenericType] |
| 218 |
from typing import TypeVar, Generic |
| 219 |
T = TypeVar('T') |
| 220 |
S = TypeVar('S') |
| 221 |
class B(Generic[T]): |
| 222 |
def f(self, a: T) -> None: pass |
| 223 |
def g(self, a: T) -> None: pass |
| 224 |
class A(B[S], Generic[T, S]): |
| 225 |
def f(self, a: T) -> None: pass \ |
| 226 |
# E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "S" \ |
| 227 |
# N: This violates the Liskov substitution principle \ |
| 228 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 229 |
def g(self, a: S) -> None: pass |
| 230 |
[out] |
| 231 |
|
| 232 |
[case testOverridingMethodInMultilevelHierarchyOfGenericTypes] |
| 233 |
from typing import TypeVar, Generic |
| 234 |
T = TypeVar('T') |
| 235 |
S = TypeVar('S') |
| 236 |
U = TypeVar('U') |
| 237 |
V = TypeVar('V') |
| 238 |
|
| 239 |
class D: pass |
| 240 |
class C(Generic[T, U, V]): |
| 241 |
def f(self, a: V) -> None: pass |
| 242 |
def g(self, a: V) -> None: pass |
| 243 |
class B(C[D, D, T], Generic[T]): pass |
| 244 |
class A(B[S], Generic[T, S]): |
| 245 |
def f(self, a: T) -> None: pass \ |
| 246 |
# E: Argument 1 of "f" is incompatible with supertype "C"; supertype defines the argument type as "S" \ |
| 247 |
# N: This violates the Liskov substitution principle \ |
| 248 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 249 |
def g(self, a: S) -> None: pass |
| 250 |
[out] |
| 251 |
|
| 252 |
[case testOverrideGenericMethodInNonGenericClass] |
| 253 |
from typing import TypeVar |
| 254 |
|
| 255 |
T = TypeVar('T') |
| 256 |
S = TypeVar('S') |
| 257 |
|
| 258 |
class A: |
| 259 |
def f(self, x: T, y: S) -> None: pass |
| 260 |
class B(A): |
| 261 |
def f(self, x: S, y: T) -> None: pass |
| 262 |
class C(A): |
| 263 |
# Okay, because T = object allows any type for the arguments. |
| 264 |
def f(self, x: T, y: T) -> None: pass |
| 265 |
|
| 266 |
[case testOverrideGenericMethodInNonGenericClassLists] |
| 267 |
from typing import TypeVar, List |
| 268 |
|
| 269 |
T = TypeVar('T') |
| 270 |
S = TypeVar('S') |
| 271 |
|
| 272 |
class A: |
| 273 |
def f(self, x: List[T], y: List[S]) -> None: pass |
| 274 |
class B(A): |
| 275 |
def f(self, x: List[S], y: List[T]) -> None: pass |
| 276 |
class C(A): |
| 277 |
def f(self, x: List[T], y: List[T]) -> None: pass # Fail |
| 278 |
[builtins fixtures/list.pyi] |
| 279 |
[out] |
| 280 |
main:11: error: Signature of "f" incompatible with supertype "A" |
| 281 |
main:11: note: Superclass: |
| 282 |
main:11: note: def [T, S] f(self, x: list[T], y: list[S]) -> None |
| 283 |
main:11: note: Subclass: |
| 284 |
main:11: note: def [T] f(self, x: list[T], y: list[T]) -> None |
| 285 |
|
| 286 |
[case testOverrideGenericMethodInNonGenericClassGeneralize] |
| 287 |
from typing import TypeVar |
| 288 |
|
| 289 |
T = TypeVar('T') |
| 290 |
T1 = TypeVar('T1', bound=str) |
| 291 |
S = TypeVar('S') |
| 292 |
|
| 293 |
class A: |
| 294 |
def f(self, x: int, y: S) -> None: pass |
| 295 |
class B(A): |
| 296 |
def f(self, x: T, y: S) -> None: pass |
| 297 |
class C(A): |
| 298 |
def f(self, x: T, y: str) -> None: pass |
| 299 |
class D(A): |
| 300 |
def f(self, x: T1, y: S) -> None: pass # TODO: This error could be more specific. |
| 301 |
[out] |
| 302 |
main:12: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "S" |
| 303 |
main:12: note: This violates the Liskov substitution principle |
| 304 |
main:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 305 |
main:14: error: Signature of "f" incompatible with supertype "A" |
| 306 |
main:14: note: Superclass: |
| 307 |
main:14: note: def [S] f(self, x: int, y: S) -> None |
| 308 |
main:14: note: Subclass: |
| 309 |
main:14: note: def [T1: str, S] f(self, x: T1, y: S) -> None |
| 310 |
|
| 311 |
-- Inheritance from generic types with implicit dynamic supertype |
| 312 |
-- -------------------------------------------------------------- |
| 313 |
|
| 314 |
|
| 315 |
[case testInheritanceFromGenericWithImplicitDynamicAndSubtyping] |
| 316 |
from typing import TypeVar, Generic |
| 317 |
T = TypeVar('T') |
| 318 |
a: A |
| 319 |
bc: B[C] |
| 320 |
bd: B[D] |
| 321 |
|
| 322 |
if int(): |
| 323 |
a = bc # E: Incompatible types in assignment (expression has type "B[C]", variable has type "A") |
| 324 |
bc = a |
| 325 |
bd = a |
| 326 |
|
| 327 |
class B(Generic[T]): pass |
| 328 |
class A(B): pass |
| 329 |
class C: pass |
| 330 |
class D: pass |
| 331 |
[out] |
| 332 |
|
| 333 |
[case testInheritanceFromGenericWithImplicitDynamicAndExternalAccess] |
| 334 |
from typing import TypeVar, Generic |
| 335 |
T = TypeVar('T') |
| 336 |
|
| 337 |
class B(Generic[T]): |
| 338 |
def f(self, a: 'B[T]') -> None: pass |
| 339 |
def __init__(self, x: 'B[T]') -> None: |
| 340 |
self.x = x |
| 341 |
class A(B): pass |
| 342 |
class C: pass |
| 343 |
|
| 344 |
a: A |
| 345 |
c: C |
| 346 |
bc: B[C] |
| 347 |
|
| 348 |
a.x = c # E: Incompatible types in assignment (expression has type "C", variable has type "B[Any]") |
| 349 |
a.f(c) # E: Argument 1 to "f" of "B" has incompatible type "C"; expected "B[Any]" |
| 350 |
a.x = bc |
| 351 |
a.f(bc) |
| 352 |
[out] |
| 353 |
|
| 354 |
[case testInheritanceFromGenericWithImplicitDynamic] |
| 355 |
from typing import TypeVar, Generic |
| 356 |
T = TypeVar('T') |
| 357 |
a: A |
| 358 |
c: C |
| 359 |
bc: B[C] |
| 360 |
|
| 361 |
class B(Generic[T]): |
| 362 |
def f(self, a: 'B[T]') -> None: pass |
| 363 |
def __init__(self, x: 'B[T]') -> None: |
| 364 |
self.x = x |
| 365 |
|
| 366 |
class A(B): |
| 367 |
def g(self) -> None: |
| 368 |
self.x = c # E: Incompatible types in assignment (expression has type "C", variable has type "B[Any]") |
| 369 |
self.f(c) # E: Argument 1 to "f" of "B" has incompatible type "C"; expected "B[Any]" |
| 370 |
self.x = bc |
| 371 |
self.f(bc) |
| 372 |
|
| 373 |
class C: pass |
| 374 |
[out] |
| 375 |
|
| 376 |
[case testInheritanceFromGenericWithImplicitDynamicAndOverriding] |
| 377 |
from typing import TypeVar, Generic, Tuple |
| 378 |
T = TypeVar('T') |
| 379 |
class B(Generic[T]): |
| 380 |
def f(self, a: T, b: 'Tuple[T, B[T]]') -> None: |
| 381 |
pass |
| 382 |
class A(B): |
| 383 |
def f(self, a, b): pass |
| 384 |
[builtins fixtures/tuple.pyi] |
| 385 |
[out] |
| 386 |
|
| 387 |
|
| 388 |
-- Inheritance from generic types and super expressions |
| 389 |
-- ---------------------------------------------------- |
| 390 |
|
| 391 |
|
| 392 |
[case testSuperExpressionsWhenInheritingFromGenericType] |
| 393 |
from typing import TypeVar, Generic |
| 394 |
T = TypeVar('T') |
| 395 |
S = TypeVar('S') |
| 396 |
class B(Generic[T]): |
| 397 |
def f(self, a: T) -> None: pass |
| 398 |
class A(B[S], Generic[T, S]): |
| 399 |
def g(self, t: T, s: S) -> None: |
| 400 |
super().f(t) # E: Argument 1 to "f" of "B" has incompatible type "T"; expected "S" |
| 401 |
super().f(s) |
| 402 |
[out] |
| 403 |
|
| 404 |
[case testSuperExpressionsWhenInheritingFromGenericTypeAndDeepHierarchy] |
| 405 |
from typing import TypeVar, Generic |
| 406 |
T = TypeVar('T') |
| 407 |
S = TypeVar('S') |
| 408 |
U = TypeVar('U') |
| 409 |
V = TypeVar('V') |
| 410 |
class C(Generic[T, U, V]): |
| 411 |
def f(self, a: V) -> None: pass |
| 412 |
class D: pass |
| 413 |
class B(C[D, D, T], Generic[T]): pass |
| 414 |
class A(B[S], Generic[T, S]): |
| 415 |
def g(self, t: T, s: S) -> None: |
| 416 |
super().f(t) # E: Argument 1 to "f" of "C" has incompatible type "T"; expected "S" |
| 417 |
super().f(s) |
| 418 |
[out] |
| 419 |
|
| 420 |
|
| 421 |
-- Type of inherited constructor |
| 422 |
-- ----------------------------- |
| 423 |
|
| 424 |
|
| 425 |
[case testInheritedConstructor] |
| 426 |
from typing import TypeVar, Generic |
| 427 |
T = TypeVar('T') |
| 428 |
class A(Generic[T]): |
| 429 |
def __init__(self, x: T) -> None: pass |
| 430 |
class B(A[T], Generic[T]): pass |
| 431 |
class C(A[int]): pass |
| 432 |
class D(A[A[T]], Generic[T]): pass |
| 433 |
B(1) |
| 434 |
C(1) |
| 435 |
C('a') # E: Argument 1 to "C" has incompatible type "str"; expected "int" |
| 436 |
D(A(1)) |
| 437 |
D(1) # E: Argument 1 to "D" has incompatible type "int"; expected "A[Never]" |
| 438 |
|
| 439 |
|
| 440 |
[case testInheritedConstructor2] |
| 441 |
from typing import TypeVar, Generic |
| 442 |
T = TypeVar('T') |
| 443 |
U = TypeVar('U') |
| 444 |
Z = TypeVar('Z') |
| 445 |
class A(Generic[T, U]): |
| 446 |
def __init__(self, x: T, y: U, z: Z) -> None: pass |
| 447 |
class B(A[int, T], Generic[T]): pass |
| 448 |
class C(B[A[T, str]], Generic[T, U]): pass |
| 449 |
# C[T, U] <: B[A[T, str]] <: A[int, A[T, str]] |
| 450 |
C(1, A(1, 'a', 0), 'z') |
| 451 |
C(1, A('1', 'a', 0), 'z') |
| 452 |
C('1', A(1, 'a', 0), 'z') # E: Argument 1 to "C" has incompatible type "str"; expected "int" |
| 453 |
C(1, A(1, 1, 0), 'z') # E: Argument 2 to "A" has incompatible type "int"; expected "str" |
| 454 |
|
| 455 |
|
| 456 |
-- Subtyping with a generic abstract base class |
| 457 |
-- -------------------------------------------- |
| 458 |
|
| 459 |
|
| 460 |
[case testSubtypingWithGenericTypeSubclassingGenericAbstractClass] |
| 461 |
from typing import TypeVar, Generic |
| 462 |
from abc import abstractmethod |
| 463 |
T = TypeVar('T') |
| 464 |
S = TypeVar('S') |
| 465 |
acd: A[C, D] |
| 466 |
adc: A[D, C] |
| 467 |
ic: I[C] |
| 468 |
id: I[D] |
| 469 |
|
| 470 |
if int(): |
| 471 |
ic = acd # E: Incompatible types in assignment (expression has type "A[C, D]", variable has type "I[C]") |
| 472 |
id = adc # E: Incompatible types in assignment (expression has type "A[D, C]", variable has type "I[D]") |
| 473 |
adc = ic # E: Incompatible types in assignment (expression has type "I[C]", variable has type "A[D, C]") |
| 474 |
|
| 475 |
if int(): |
| 476 |
ic = adc |
| 477 |
id = acd |
| 478 |
|
| 479 |
class I(Generic[T]): |
| 480 |
@abstractmethod |
| 481 |
def f(self): pass |
| 482 |
class A(I[S], Generic[T, S]): pass |
| 483 |
class C: pass |
| 484 |
class D: pass |
| 485 |
|
| 486 |
[case testSubtypingWithTypeImplementingGenericABCViaInheritance] |
| 487 |
from typing import TypeVar, Generic |
| 488 |
S = TypeVar('S') |
| 489 |
a: A |
| 490 |
b: B |
| 491 |
ic: I[C] |
| 492 |
id: I[D] |
| 493 |
ie: I[E] |
| 494 |
|
| 495 |
class I(Generic[S]): pass |
| 496 |
class B(I[C]): pass |
| 497 |
class A(B): pass |
| 498 |
|
| 499 |
if int(): |
| 500 |
ie = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[E]") |
| 501 |
a = ic # E: Incompatible types in assignment (expression has type "I[C]", variable has type "A") |
| 502 |
if int(): |
| 503 |
a = id # E: Incompatible types in assignment (expression has type "I[D]", variable has type "A") |
| 504 |
if int(): |
| 505 |
a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 506 |
id = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[D]") |
| 507 |
|
| 508 |
ic = a |
| 509 |
b = a |
| 510 |
|
| 511 |
class C: pass |
| 512 |
class D: pass |
| 513 |
class E: pass |
| 514 |
[builtins fixtures/tuple.pyi] |
| 515 |
[out] |
| 516 |
|
| 517 |
[case testSubtypingWithTypeImplementingGenericABCViaInheritance2-skip] |
| 518 |
from typing import TypeVar, Generic |
| 519 |
T = TypeVar('T') |
| 520 |
class I(Generic[T]): pass |
| 521 |
class A(I[C]): pass |
| 522 |
class B(A, I[D]): pass # Fail |
| 523 |
|
| 524 |
class C: pass |
| 525 |
class D: pass |
| 526 |
[out] |
| 527 |
main:5: error: Class "B" has base "I" duplicated inconsistently |
| 528 |
|
| 529 |
[case testSubtypingAndABCExtension] |
| 530 |
from typing import TypeVar, Generic |
| 531 |
from abc import abstractmethod, ABCMeta |
| 532 |
t = TypeVar('t') |
| 533 |
a: A[object] |
| 534 |
i: I[object] |
| 535 |
j: J[object] |
| 536 |
(ii, jj) = (i, j) |
| 537 |
if int(): |
| 538 |
ii = a |
| 539 |
jj = a |
| 540 |
if int(): |
| 541 |
jj = i |
| 542 |
a = i # E: Incompatible types in assignment (expression has type "I[object]", variable has type "A[object]") |
| 543 |
if int(): |
| 544 |
a = j # E: Incompatible types in assignment (expression has type "J[object]", variable has type "A[object]") |
| 545 |
|
| 546 |
class J(Generic[t]): pass |
| 547 |
class X(metaclass=ABCMeta): pass |
| 548 |
class I(X, J[t], Generic[t]): pass |
| 549 |
class A(I[t], Generic[t]): pass |
| 550 |
[builtins fixtures/tuple.pyi] |
| 551 |
|
| 552 |
|
| 553 |
-- Subclassing a generic ABC |
| 554 |
-- ------------------------- |
| 555 |
|
| 556 |
|
| 557 |
[case testSubclassingGenericABC1] |
| 558 |
from typing import TypeVar, Generic |
| 559 |
from abc import abstractmethod |
| 560 |
T = TypeVar('T') |
| 561 |
class I(Generic[T]): |
| 562 |
@abstractmethod |
| 563 |
def f(self, a: T) -> None: pass |
| 564 |
@abstractmethod |
| 565 |
def g(self, a: T) -> None: pass |
| 566 |
class A(I[C]): |
| 567 |
def f(self, a: 'D') -> None: pass \ |
| 568 |
# E: Argument 1 of "f" is incompatible with supertype "I"; supertype defines the argument type as "C" \ |
| 569 |
# N: This violates the Liskov substitution principle \ |
| 570 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 571 |
def g(self, a: 'C') -> None: pass |
| 572 |
class C: pass |
| 573 |
class D: pass |
| 574 |
[out] |
| 575 |
|
| 576 |
|
| 577 |
-- Extending a generic ABC with deep type hierarchy |
| 578 |
-- ------------------------------------------------ |
| 579 |
|
| 580 |
|
| 581 |
[case testSubclassingGenericABCWithDeepHierarchy] |
| 582 |
from typing import Any, TypeVar, Generic |
| 583 |
from abc import abstractmethod |
| 584 |
T = TypeVar('T') |
| 585 |
a: A |
| 586 |
ic: I[C] |
| 587 |
id: I[D] |
| 588 |
|
| 589 |
if int(): |
| 590 |
id = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[D]") |
| 591 |
ic = a |
| 592 |
|
| 593 |
class I(Generic[T]): |
| 594 |
@abstractmethod |
| 595 |
def f(self, a: T, b: T) -> None: pass |
| 596 |
@abstractmethod |
| 597 |
def g(self, a: T, b: 'D') -> None: pass |
| 598 |
class B(I[C]): |
| 599 |
def f(self, a: 'C', b: 'C') -> None: pass |
| 600 |
def g(self, a: 'C', b: Any) -> None: pass |
| 601 |
class A(B): |
| 602 |
def g(self, a: 'C', b: 'C') -> None: pass \ |
| 603 |
# E: Argument 2 of "g" is incompatible with supertype "I"; supertype defines the argument type as "D" \ |
| 604 |
# N: This violates the Liskov substitution principle \ |
| 605 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 606 |
def f(self, a: 'C', b: 'C') -> None: pass |
| 607 |
class C: pass |
| 608 |
class D: pass |
| 609 |
[builtins fixtures/tuple.pyi] |
| 610 |
|
| 611 |
[case testSubclassingGenericABCWithDeepHierarchy2] |
| 612 |
from typing import Any, TypeVar, Generic |
| 613 |
from abc import abstractmethod |
| 614 |
T = TypeVar('T') |
| 615 |
class I(Generic[T]): |
| 616 |
@abstractmethod |
| 617 |
def f(self, a: T, b: T) -> None: pass |
| 618 |
class B(I[C]): |
| 619 |
def f(self, a: 'C', b: Any) -> None: pass |
| 620 |
class A(B): |
| 621 |
def f(self, a: 'C', b: 'D') -> None: pass \ |
| 622 |
# E: Argument 2 of "f" is incompatible with supertype "I"; supertype defines the argument type as "C" \ |
| 623 |
# N: This violates the Liskov substitution principle \ |
| 624 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 625 |
class C: pass |
| 626 |
class D: pass |
| 627 |
[out] |
| 628 |
|
| 629 |
|
| 630 |
-- Implicit Any types and subclassing generic ABC |
| 631 |
-- ---------------------------------------------- |
| 632 |
|
| 633 |
|
| 634 |
[case testSubclassingGenericABCWithImplicitAny] |
| 635 |
from typing import Any, TypeVar, Generic |
| 636 |
from abc import abstractmethod |
| 637 |
T = TypeVar('T') |
| 638 |
a: Any |
| 639 |
ic: I[C] |
| 640 |
id: I[D] |
| 641 |
|
| 642 |
ic = a |
| 643 |
id = a |
| 644 |
|
| 645 |
class I(Generic[T]): |
| 646 |
@abstractmethod |
| 647 |
def f(self, a: T) -> None: pass |
| 648 |
class A(I): |
| 649 |
def f(self, a): pass |
| 650 |
|
| 651 |
class C: pass |
| 652 |
class D: pass |
| 653 |
|
| 654 |
[case testSubclassingGenericABCWithImplicitAnyAndDeepHierarchy] |
| 655 |
from typing import Any, TypeVar, Generic |
| 656 |
from abc import abstractmethod |
| 657 |
T = TypeVar('T') |
| 658 |
a: Any |
| 659 |
ic: I[C] |
| 660 |
id: I[D] |
| 661 |
|
| 662 |
ic = a |
| 663 |
id = a |
| 664 |
|
| 665 |
class I(Generic[T]): |
| 666 |
@abstractmethod |
| 667 |
def f(self, a: T, b: T) -> None: pass |
| 668 |
class B(I): |
| 669 |
def f(self, a, b): pass |
| 670 |
class A(B): |
| 671 |
def f(self, a: 'C', b: 'D') -> None: pass |
| 672 |
class C: pass |
| 673 |
class D: pass |
| 674 |
|
| 675 |
[case testImplementingGenericABCWithImplicitAnyAndDeepHierarchy2] |
| 676 |
from typing import Any, TypeVar, Generic |
| 677 |
from abc import abstractmethod |
| 678 |
T = TypeVar('T') |
| 679 |
a: Any |
| 680 |
jc: J[C] |
| 681 |
jd: J[D] |
| 682 |
|
| 683 |
jc = a |
| 684 |
jd = a |
| 685 |
|
| 686 |
class J(Generic[T]): |
| 687 |
@abstractmethod |
| 688 |
def f(self, a: T, b: T) -> None: pass |
| 689 |
class I(J): |
| 690 |
@abstractmethod |
| 691 |
def f(self, a, b): pass |
| 692 |
class A(I): |
| 693 |
def f(self, a: 'C', b: 'D') -> None: pass |
| 694 |
|
| 695 |
class C: pass |
| 696 |
class D: pass |
| 697 |
|
| 698 |
|
| 699 |
-- Accessing generic ABC members |
| 700 |
-- ----------------------------- |
| 701 |
|
| 702 |
|
| 703 |
[case testAccessingGenericABCMembers] |
| 704 |
from typing import TypeVar, Generic |
| 705 |
from abc import abstractmethod |
| 706 |
T = TypeVar('T') |
| 707 |
class I(Generic[T]): |
| 708 |
@abstractmethod |
| 709 |
def f(self, a: T) -> None: pass |
| 710 |
class A: pass |
| 711 |
class B: pass |
| 712 |
|
| 713 |
a: A |
| 714 |
b: B |
| 715 |
ia: I[A] |
| 716 |
|
| 717 |
ia.f(b) # E: Argument 1 to "f" of "I" has incompatible type "B"; expected "A" |
| 718 |
ia.f(a) |
| 719 |
[builtins fixtures/tuple.pyi] |
| 720 |
|
| 721 |
[case testAccessingInheritedGenericABCMembers] |
| 722 |
from typing import TypeVar, Generic |
| 723 |
from abc import abstractmethod |
| 724 |
T = TypeVar('T') |
| 725 |
class J(Generic[T]): |
| 726 |
@abstractmethod |
| 727 |
def f(self, a: T) -> None: pass |
| 728 |
class I(J[T], Generic[T]): pass |
| 729 |
class A: pass |
| 730 |
class B: pass |
| 731 |
a: A |
| 732 |
b: B |
| 733 |
ia: I[A] |
| 734 |
|
| 735 |
ia.f(b) # E: Argument 1 to "f" of "J" has incompatible type "B"; expected "A" |
| 736 |
ia.f(a) |
| 737 |
[builtins fixtures/tuple.pyi] |
| 738 |
|
| 739 |
|
| 740 |
-- Misc |
| 741 |
-- ---- |
| 742 |
|
| 743 |
|
| 744 |
[case testMultipleAssignmentAndGenericSubtyping] |
| 745 |
from typing import Iterable |
| 746 |
n: int |
| 747 |
s: str |
| 748 |
class Nums(Iterable[int]): |
| 749 |
def __iter__(self): pass |
| 750 |
def __next__(self): pass |
| 751 |
n, n = Nums() |
| 752 |
s, s = Nums() # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 753 |
[builtins fixtures/for.pyi] |
| 754 |
[out] |
| 755 |
|
| 756 |
[case testUninhabitedCacheChecksAmbiguous] |
| 757 |
# https://github.com/python/mypy/issues/19641 |
| 758 |
from typing import Mapping, Never, TypeVar |
| 759 |
|
| 760 |
M = TypeVar("M", bound=Mapping[str,object]) |
| 761 |
|
| 762 |
def get(arg: M, /) -> M: |
| 763 |
return arg |
| 764 |
|
| 765 |
get({}) |
| 766 |
|
| 767 |
def upcast(d: dict[Never, Never]) -> Mapping[str, object]: |
| 768 |
return d # E: Incompatible return value type (got "dict[Never, Never]", expected "Mapping[str, object]") |
| 769 |
[builtins fixtures/dict.pyi] |
| 770 |
|
| 771 |
-- Variance |
| 772 |
-- -------- |
| 773 |
|
| 774 |
|
| 775 |
[case testCovariant] |
| 776 |
from typing import TypeVar, Generic |
| 777 |
T = TypeVar('T', covariant=True) |
| 778 |
|
| 779 |
class G(Generic[T]): pass |
| 780 |
class A: pass |
| 781 |
class B(A): pass |
| 782 |
class C(B): pass |
| 783 |
|
| 784 |
a: G[A] |
| 785 |
b: G[B] |
| 786 |
c: G[C] |
| 787 |
|
| 788 |
if int(): |
| 789 |
b = a # E: Incompatible types in assignment (expression has type "G[A]", variable has type "G[B]") |
| 790 |
b = c |
| 791 |
[builtins fixtures/bool.pyi] |
| 792 |
[out] |
| 793 |
|
| 794 |
[case testContravariant] |
| 795 |
from typing import TypeVar, Generic |
| 796 |
T = TypeVar('T', contravariant=True) |
| 797 |
|
| 798 |
class G(Generic[T]): pass |
| 799 |
class A: pass |
| 800 |
class B(A): pass |
| 801 |
class C(B): pass |
| 802 |
|
| 803 |
a: G[A] |
| 804 |
b: G[B] |
| 805 |
c: G[C] |
| 806 |
|
| 807 |
if int(): |
| 808 |
b = a |
| 809 |
b = c # E: Incompatible types in assignment (expression has type "G[C]", variable has type "G[B]") |
| 810 |
[builtins fixtures/bool.pyi] |
| 811 |
[out] |
| 812 |
|
| 813 |
[case testInvariant] |
| 814 |
from typing import TypeVar, Generic |
| 815 |
T = TypeVar('T') # invariant (default) |
| 816 |
|
| 817 |
class G(Generic[T]): pass |
| 818 |
class A: pass |
| 819 |
class B(A): pass |
| 820 |
class C(B): pass |
| 821 |
|
| 822 |
a: G[A] |
| 823 |
b: G[B] |
| 824 |
c: G[C] |
| 825 |
|
| 826 |
if int(): |
| 827 |
b = a # E: Incompatible types in assignment (expression has type "G[A]", variable has type "G[B]") |
| 828 |
b = c # E: Incompatible types in assignment (expression has type "G[C]", variable has type "G[B]") |
| 829 |
[builtins fixtures/bool.pyi] |
| 830 |
[out] |
| 831 |
|
| 832 |
|
| 833 |
[case testTypeVarSubtypeUnion] |
| 834 |
from typing import Union, TypeVar, Generic |
| 835 |
|
| 836 |
class U: pass |
| 837 |
class W: pass |
| 838 |
|
| 839 |
T = TypeVar('T', bound=Union[U, W]) |
| 840 |
|
| 841 |
class Y(Generic[T]): |
| 842 |
def __init__(self) -> None: |
| 843 |
pass |
| 844 |
def f(self) -> T: |
| 845 |
return U() # E: Incompatible return value type (got "U", expected "T") |
| 846 |
|
| 847 |
|
| 848 |
[case testTypeVarBoundToOldUnionAttributeAccess] |
| 849 |
from typing import Union, TypeVar |
| 850 |
|
| 851 |
class U: |
| 852 |
a: float |
| 853 |
class V: |
| 854 |
b: float |
| 855 |
class W: |
| 856 |
c: float |
| 857 |
|
| 858 |
T = TypeVar("T", bound=Union[U, V, W]) |
| 859 |
|
| 860 |
def f(x: T) -> None: |
| 861 |
x.a # E |
| 862 |
x.b = 1.0 # E |
| 863 |
del x.c # E |
| 864 |
|
| 865 |
[out] |
| 866 |
main:13: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "a" |
| 867 |
main:13: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "a" |
| 868 |
main:14: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "b" |
| 869 |
main:14: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "b" |
| 870 |
main:15: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "c" |
| 871 |
main:15: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "c" |
| 872 |
|
| 873 |
|
| 874 |
[case testTypeVarBoundToNewUnionAttributeAccess] |
| 875 |
from typing import TypeVar |
| 876 |
|
| 877 |
class U: |
| 878 |
a: int |
| 879 |
class V: |
| 880 |
b: int |
| 881 |
class W: |
| 882 |
c: int |
| 883 |
|
| 884 |
T = TypeVar("T", bound=U | V | W) |
| 885 |
|
| 886 |
def f(x: T) -> None: |
| 887 |
x.a # E |
| 888 |
x.b = 1 # E |
| 889 |
del x.c # E |
| 890 |
|
| 891 |
[builtins fixtures/tuple.pyi] |
| 892 |
[out] |
| 893 |
main:13: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "a" |
| 894 |
main:13: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "a" |
| 895 |
main:14: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "b" |
| 896 |
main:14: error: Item "W" of the upper bound "U | V | W" of type variable "T" has no attribute "b" |
| 897 |
main:15: error: Item "U" of the upper bound "U | V | W" of type variable "T" has no attribute "c" |
| 898 |
main:15: error: Item "V" of the upper bound "U | V | W" of type variable "T" has no attribute "c" |
| 899 |
|
| 900 |
|
| 901 |
[case testSubtypingIterableUnpacking1] |
| 902 |
# https://github.com/python/mypy/issues/11138 |
| 903 |
from typing import Generic, Iterator, TypeVar |
| 904 |
T = TypeVar("T") |
| 905 |
U = TypeVar("U") |
| 906 |
|
| 907 |
class X1(Iterator[U], Generic[T, U]): |
| 908 |
pass |
| 909 |
|
| 910 |
x1: X1[str, int] |
| 911 |
reveal_type(list(x1)) # N: Revealed type is "builtins.list[builtins.int]" |
| 912 |
reveal_type([*x1]) # N: Revealed type is "builtins.list[builtins.int]" |
| 913 |
|
| 914 |
class X2(Iterator[T], Generic[T, U]): |
| 915 |
pass |
| 916 |
|
| 917 |
x2: X2[str, int] |
| 918 |
reveal_type(list(x2)) # N: Revealed type is "builtins.list[builtins.str]" |
| 919 |
reveal_type([*x2]) # N: Revealed type is "builtins.list[builtins.str]" |
| 920 |
|
| 921 |
class X3(Generic[T, U], Iterator[U]): |
| 922 |
pass |
| 923 |
|
| 924 |
x3: X3[str, int] |
| 925 |
reveal_type(list(x3)) # N: Revealed type is "builtins.list[builtins.int]" |
| 926 |
reveal_type([*x3]) # N: Revealed type is "builtins.list[builtins.int]" |
| 927 |
|
| 928 |
class X4(Generic[T, U], Iterator[T]): |
| 929 |
pass |
| 930 |
|
| 931 |
x4: X4[str, int] |
| 932 |
reveal_type(list(x4)) # N: Revealed type is "builtins.list[builtins.str]" |
| 933 |
reveal_type([*x4]) # N: Revealed type is "builtins.list[builtins.str]" |
| 934 |
|
| 935 |
class X5(Iterator[T]): |
| 936 |
pass |
| 937 |
|
| 938 |
x5: X5[str] |
| 939 |
reveal_type(list(x5)) # N: Revealed type is "builtins.list[builtins.str]" |
| 940 |
reveal_type([*x5]) # N: Revealed type is "builtins.list[builtins.str]" |
| 941 |
|
| 942 |
class X6(Generic[T, U], Iterator[bool]): |
| 943 |
pass |
| 944 |
|
| 945 |
x6: X6[str, int] |
| 946 |
reveal_type(list(x6)) # N: Revealed type is "builtins.list[builtins.bool]" |
| 947 |
reveal_type([*x6]) # N: Revealed type is "builtins.list[builtins.bool]" |
| 948 |
[builtins fixtures/list.pyi] |
| 949 |
|
| 950 |
[case testSubtypingIterableUnpacking2] |
| 951 |
from typing import Generic, Iterator, TypeVar, Mapping |
| 952 |
T = TypeVar("T") |
| 953 |
U = TypeVar("U") |
| 954 |
|
| 955 |
class X1(Generic[T, U], Iterator[U], Mapping[U, T]): |
| 956 |
pass |
| 957 |
|
| 958 |
x1: X1[str, int] |
| 959 |
reveal_type(list(x1)) # N: Revealed type is "builtins.list[builtins.int]" |
| 960 |
reveal_type([*x1]) # N: Revealed type is "builtins.list[builtins.int]" |
| 961 |
|
| 962 |
class X2(Generic[T, U], Iterator[U], Mapping[T, U]): |
| 963 |
pass |
| 964 |
|
| 965 |
x2: X2[str, int] |
| 966 |
reveal_type(list(x2)) # N: Revealed type is "builtins.list[builtins.int]" |
| 967 |
reveal_type([*x2]) # N: Revealed type is "builtins.list[builtins.int]" |
| 968 |
[builtins fixtures/list.pyi] |
| 969 |
|
| 970 |
[case testSubtypingMappingUnpacking1] |
| 971 |
# https://github.com/python/mypy/issues/11138 |
| 972 |
from typing import Generic, TypeVar, Mapping |
| 973 |
T = TypeVar("T") |
| 974 |
U = TypeVar("U") |
| 975 |
|
| 976 |
class X1(Generic[T, U], Mapping[U, T]): |
| 977 |
pass |
| 978 |
|
| 979 |
x1: X1[str, int] |
| 980 |
reveal_type(iter(x1)) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 981 |
reveal_type({**x1}) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 982 |
|
| 983 |
class X2(Generic[T, U], Mapping[T, U]): |
| 984 |
pass |
| 985 |
|
| 986 |
x2: X2[str, int] |
| 987 |
reveal_type(iter(x2)) # N: Revealed type is "typing.Iterator[builtins.str]" |
| 988 |
reveal_type({**x2}) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]" |
| 989 |
|
| 990 |
class X3(Generic[T, U], Mapping[bool, float]): |
| 991 |
pass |
| 992 |
|
| 993 |
x3: X3[str, int] |
| 994 |
reveal_type(iter(x3)) # N: Revealed type is "typing.Iterator[builtins.bool]" |
| 995 |
reveal_type({**x3}) # N: Revealed type is "builtins.dict[builtins.bool, builtins.float]" |
| 996 |
[builtins fixtures/dict.pyi] |
| 997 |
|
| 998 |
[case testSubtypingMappingUnpacking2] |
| 999 |
from typing import Generic, TypeVar, Mapping |
| 1000 |
T = TypeVar("T") |
| 1001 |
U = TypeVar("U") |
| 1002 |
|
| 1003 |
class X1(Generic[T, U], Mapping[U, T]): |
| 1004 |
pass |
| 1005 |
|
| 1006 |
def func_with_kwargs(**kwargs: int): |
| 1007 |
pass |
| 1008 |
|
| 1009 |
x1: X1[str, int] |
| 1010 |
reveal_type(iter(x1)) |
| 1011 |
reveal_type({**x1}) |
| 1012 |
func_with_kwargs(**x1) |
| 1013 |
[out] |
| 1014 |
main:12: note: Revealed type is "typing.Iterator[builtins.int]" |
| 1015 |
main:13: note: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 1016 |
main:14: error: Argument after ** must have string keys |
| 1017 |
main:14: error: Argument 1 to "func_with_kwargs" has incompatible type "**X1[str, int]"; expected "int" |
| 1018 |
[builtins fixtures/dict.pyi] |
| 1019 |
[typing fixtures/typing-medium.pyi] |
| 1020 |
|
| 1021 |
[case testSubtypingMappingUnpacking3] |
| 1022 |
from typing import Generic, TypeVar, Mapping, Iterable |
| 1023 |
T = TypeVar("T") |
| 1024 |
U = TypeVar("U") |
| 1025 |
|
| 1026 |
class X1(Generic[T, U], Mapping[U, T], Iterable[U]): |
| 1027 |
pass |
| 1028 |
|
| 1029 |
x1: X1[str, int] |
| 1030 |
reveal_type(iter(x1)) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 1031 |
reveal_type({**x1}) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 1032 |
|
| 1033 |
# Some people would expect this to raise an error, but this currently does not: |
| 1034 |
# `Mapping` has `Iterable[U]` base class, `X2` has direct `Iterable[T]` base class. |
| 1035 |
# It would be impossible to define correct `__iter__` method for incompatible `T` and `U`. |
| 1036 |
class X2(Generic[T, U], Mapping[U, T], Iterable[T]): |
| 1037 |
pass |
| 1038 |
|
| 1039 |
x2: X2[str, int] |
| 1040 |
reveal_type(iter(x2)) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 1041 |
reveal_type({**x2}) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 1042 |
[builtins fixtures/dict.pyi] |
| 1043 |
|
| 1044 |
[case testNotDirectIterableAndMappingSubtyping] |
| 1045 |
from typing import Generic, TypeVar, Dict, Iterable, Iterator, List |
| 1046 |
T = TypeVar("T") |
| 1047 |
U = TypeVar("U") |
| 1048 |
|
| 1049 |
class X1(Generic[T, U], Dict[U, T], Iterable[U]): |
| 1050 |
def __iter__(self) -> Iterator[U]: pass |
| 1051 |
|
| 1052 |
x1: X1[str, int] |
| 1053 |
reveal_type(iter(x1)) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 1054 |
reveal_type({**x1}) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" |
| 1055 |
|
| 1056 |
class X2(Generic[T, U], List[U]): |
| 1057 |
def __iter__(self) -> Iterator[U]: pass |
| 1058 |
|
| 1059 |
x2: X2[str, int] |
| 1060 |
reveal_type(iter(x2)) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 1061 |
reveal_type([*x2]) # N: Revealed type is "builtins.list[builtins.int]" |
| 1062 |
[builtins fixtures/dict.pyi] |
| 1063 |
|
| 1064 |
[case testIncompatibleVariance] |
| 1065 |
from typing import TypeVar, Generic |
| 1066 |
T = TypeVar('T') |
| 1067 |
T_co = TypeVar('T_co', covariant=True) |
| 1068 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 1069 |
|
| 1070 |
class A(Generic[T_co]): ... |
| 1071 |
class B(A[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type |
| 1072 |
|
| 1073 |
class C(Generic[T_contra]): ... |
| 1074 |
class D(C[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatible with variance in parent type |
| 1075 |
|
| 1076 |
class E(Generic[T]): ... |
| 1077 |
class F(E[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatible with variance in parent type |
| 1078 |
|
| 1079 |
class G(Generic[T]): ... |
| 1080 |
class H(G[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type |
| 1081 |
|
| 1082 |
[case testParameterizedGenericOverrideWithProperty] |
| 1083 |
from typing import TypeVar, Generic |
| 1084 |
|
| 1085 |
T = TypeVar("T") |
| 1086 |
|
| 1087 |
class A(Generic[T]): |
| 1088 |
def __init__(self, val: T): |
| 1089 |
self.member: T = val |
| 1090 |
|
| 1091 |
class B(A[str]): |
| 1092 |
member: str |
| 1093 |
|
| 1094 |
class GoodPropertyOverride(A[str]): |
| 1095 |
@property |
| 1096 |
def member(self) -> str: ... |
| 1097 |
@member.setter |
| 1098 |
def member(self, val: str): ... |
| 1099 |
|
| 1100 |
class BadPropertyOverride(A[str]): |
| 1101 |
@property # E: Signature of "member" incompatible with supertype "A" \ |
| 1102 |
# N: Superclass: \ |
| 1103 |
# N: str \ |
| 1104 |
# N: Subclass: \ |
| 1105 |
# N: int |
| 1106 |
def member(self) -> int: ... |
| 1107 |
@member.setter |
| 1108 |
def member(self, val: int): ... |
| 1109 |
|
| 1110 |
class BadGenericPropertyOverride(A[str], Generic[T]): |
| 1111 |
@property # E: Signature of "member" incompatible with supertype "A" \ |
| 1112 |
# N: Superclass: \ |
| 1113 |
# N: str \ |
| 1114 |
# N: Subclass: \ |
| 1115 |
# N: T |
| 1116 |
def member(self) -> T: ... |
| 1117 |
@member.setter |
| 1118 |
def member(self, val: T): ... |
| 1119 |
[builtins fixtures/property.pyi] |
| 1120 |
|
| 1121 |
[case testParameterizedGenericPropertyOverrideWithProperty] |
| 1122 |
from typing import TypeVar, Generic |
| 1123 |
|
| 1124 |
T = TypeVar("T") |
| 1125 |
|
| 1126 |
class A(Generic[T]): |
| 1127 |
@property |
| 1128 |
def member(self) -> T: ... |
| 1129 |
@member.setter |
| 1130 |
def member(self, val: T): ... |
| 1131 |
|
| 1132 |
class B(A[str]): |
| 1133 |
member: str |
| 1134 |
|
| 1135 |
class GoodPropertyOverride(A[str]): |
| 1136 |
@property |
| 1137 |
def member(self) -> str: ... |
| 1138 |
@member.setter |
| 1139 |
def member(self, val: str): ... |
| 1140 |
|
| 1141 |
class BadPropertyOverride(A[str]): |
| 1142 |
@property # E: Signature of "member" incompatible with supertype "A" \ |
| 1143 |
# N: Superclass: \ |
| 1144 |
# N: str \ |
| 1145 |
# N: Subclass: \ |
| 1146 |
# N: int |
| 1147 |
def member(self) -> int: ... |
| 1148 |
@member.setter |
| 1149 |
def member(self, val: int): ... |
| 1150 |
|
| 1151 |
class BadGenericPropertyOverride(A[str], Generic[T]): |
| 1152 |
@property # E: Signature of "member" incompatible with supertype "A" \ |
| 1153 |
# N: Superclass: \ |
| 1154 |
# N: str \ |
| 1155 |
# N: Subclass: \ |
| 1156 |
# N: T |
| 1157 |
def member(self) -> T: ... |
| 1158 |
@member.setter |
| 1159 |
def member(self, val: T): ... |
| 1160 |
[builtins fixtures/property.pyi] |
| 1161 |
|
| 1162 |
[case testParameterizedGenericOverrideSelfWithProperty] |
| 1163 |
from typing_extensions import Self |
| 1164 |
|
| 1165 |
class A: |
| 1166 |
def __init__(self, val: Self): |
| 1167 |
self.member: Self = val |
| 1168 |
|
| 1169 |
class GoodPropertyOverride(A): |
| 1170 |
@property |
| 1171 |
def member(self) -> "GoodPropertyOverride": ... |
| 1172 |
@member.setter |
| 1173 |
def member(self, val: "GoodPropertyOverride"): ... |
| 1174 |
|
| 1175 |
class GoodPropertyOverrideSelf(A): |
| 1176 |
@property |
| 1177 |
def member(self) -> Self: ... |
| 1178 |
@member.setter |
| 1179 |
def member(self, val: Self): ... |
| 1180 |
[builtins fixtures/property.pyi] |
| 1181 |
|
| 1182 |
[case testParameterizedGenericOverrideWithSelfProperty] |
| 1183 |
from typing import TypeVar, Generic |
| 1184 |
from typing_extensions import Self |
| 1185 |
|
| 1186 |
T = TypeVar("T") |
| 1187 |
|
| 1188 |
class A(Generic[T]): |
| 1189 |
def __init__(self, val: T): |
| 1190 |
self.member: T = val |
| 1191 |
|
| 1192 |
class B(A["B"]): |
| 1193 |
member: Self |
| 1194 |
|
| 1195 |
class GoodPropertyOverride(A["GoodPropertyOverride"]): |
| 1196 |
@property |
| 1197 |
def member(self) -> Self: ... |
| 1198 |
@member.setter |
| 1199 |
def member(self, val: Self): ... |
| 1200 |
[builtins fixtures/property.pyi] |
| 1201 |
|
| 1202 |
[case testMultipleInheritanceCompatibleTypeVar] |
| 1203 |
from typing import Generic, TypeVar |
| 1204 |
|
| 1205 |
T = TypeVar("T") |
| 1206 |
U = TypeVar("U") |
| 1207 |
|
| 1208 |
class A(Generic[T]): |
| 1209 |
x: T |
| 1210 |
def fn(self, t: T) -> None: ... |
| 1211 |
|
| 1212 |
class A2(A[T]): |
| 1213 |
y: str |
| 1214 |
z: str |
| 1215 |
|
| 1216 |
class B(Generic[T]): |
| 1217 |
x: T |
| 1218 |
def fn(self, t: T) -> None: ... |
| 1219 |
|
| 1220 |
class C1(A2[str], B[str]): pass |
| 1221 |
class C2(A2[str], B[int]): pass # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \ |
| 1222 |
# E: Definition of "x" in base class "A" is incompatible with definition in base class "B" |
| 1223 |
class C3(A2[T], B[T]): pass |
| 1224 |
class C4(A2[U], B[U]): pass |
| 1225 |
class C5(A2[U], B[T]): pass # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \ |
| 1226 |
# E: Definition of "x" in base class "A" is incompatible with definition in base class "B" |
| 1227 |
|
| 1228 |
class D1(A[str], B[str]): pass |
| 1229 |
class D2(A[str], B[int]): pass # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \ |
| 1230 |
# E: Definition of "x" in base class "A" is incompatible with definition in base class "B" |
| 1231 |
class D3(A[T], B[T]): pass |
| 1232 |
class D4(A[U], B[U]): pass |
| 1233 |
class D5(A[U], B[T]): pass # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \ |
| 1234 |
# E: Definition of "x" in base class "A" is incompatible with definition in base class "B" |
| 1235 |
[builtins fixtures/tuple.pyi] |