@@ -1219,7 +1219,7 @@ static_assert(is_subtype_of(TypeOf[C], Callable[[], str]))
1219
1219
#### Classes with ` __new__ `
1220
1220
1221
1221
``` py
1222
- from typing import Callable
1222
+ from typing import Callable, overload
1223
1223
from ty_extensions import TypeOf, static_assert, is_subtype_of
1224
1224
1225
1225
class A :
@@ -1244,6 +1244,20 @@ static_assert(is_subtype_of(TypeOf[E], Callable[[], C]))
1244
1244
static_assert(is_subtype_of(TypeOf[E], Callable[[], B]))
1245
1245
static_assert(not is_subtype_of(TypeOf[D], Callable[[], C]))
1246
1246
static_assert(is_subtype_of(TypeOf[D], Callable[[], B]))
1247
+
1248
+ class F :
1249
+ @overload
1250
+ def __new__ (cls ) -> int : ...
1251
+ @overload
1252
+ def __new__ (cls , x : int ) -> " F" : ...
1253
+ def __new__ (cls , x : int | None = None ) -> " int | F" :
1254
+ return 1 if x is None else object .__new__ (cls )
1255
+
1256
+ def __init__ (self , y : str ) -> None : ...
1257
+
1258
+ static_assert(is_subtype_of(TypeOf[F], Callable[[int ], F]))
1259
+ static_assert(is_subtype_of(TypeOf[F], Callable[[], int ]))
1260
+ static_assert(not is_subtype_of(TypeOf[F], Callable[[str ], F]))
1247
1261
```
1248
1262
1249
1263
#### Classes with ` __call__ ` and ` __new__ `
@@ -1266,6 +1280,123 @@ static_assert(is_subtype_of(TypeOf[F], Callable[[], int]))
1266
1280
static_assert(not is_subtype_of(TypeOf[F], Callable[[], str ]))
1267
1281
```
1268
1282
1283
+ #### Classes with ` __init__ `
1284
+
1285
+ ``` py
1286
+ from typing import Callable, overload
1287
+ from ty_extensions import TypeOf, static_assert, is_subtype_of
1288
+
1289
+ class A :
1290
+ def __init__ (self , a : int ) -> None : ...
1291
+
1292
+ static_assert(is_subtype_of(TypeOf[A], Callable[[int ], A]))
1293
+ static_assert(not is_subtype_of(TypeOf[A], Callable[[], A]))
1294
+
1295
+ class B :
1296
+ @overload
1297
+ def __init__ (self , a : int ) -> None : ...
1298
+ @overload
1299
+ def __init__ (self ) -> None : ...
1300
+ def __init__ (self , a : int | None = None ) -> None : ...
1301
+
1302
+ static_assert(is_subtype_of(TypeOf[B], Callable[[int ], B]))
1303
+ static_assert(is_subtype_of(TypeOf[B], Callable[[], B]))
1304
+
1305
+ class C : ...
1306
+
1307
+ # TODO : This assertion should be true once we understand `Self`
1308
+ # error: [static-assert-error] "Static assertion error: argument evaluates to `False`"
1309
+ static_assert(is_subtype_of(TypeOf[C], Callable[[], C]))
1310
+
1311
+ class D[T]:
1312
+ def __init__ (self , x : T) -> None : ...
1313
+
1314
+ static_assert(is_subtype_of(TypeOf[D[int ]], Callable[[int ], D[int ]]))
1315
+ static_assert(not is_subtype_of(TypeOf[D[int ]], Callable[[str ], D[int ]]))
1316
+ ```
1317
+
1318
+ #### Classes with ` __init__ ` and ` __new__ `
1319
+
1320
+ ``` py
1321
+ from typing import Callable, overload, Self
1322
+ from ty_extensions import TypeOf, static_assert, is_subtype_of
1323
+
1324
+ class A :
1325
+ def __new__ (cls , a : int ) -> Self:
1326
+ return super ().__new__ (cls )
1327
+
1328
+ def __init__ (self , a : int ) -> None : ...
1329
+
1330
+ static_assert(is_subtype_of(TypeOf[A], Callable[[int ], A]))
1331
+ static_assert(not is_subtype_of(TypeOf[A], Callable[[], A]))
1332
+
1333
+ class B :
1334
+ def __new__ (cls , a : int ) -> int :
1335
+ return super ().__new__ (cls )
1336
+
1337
+ def __init__ (self , a : str ) -> None : ...
1338
+
1339
+ static_assert(is_subtype_of(TypeOf[B], Callable[[int ], int ]))
1340
+ static_assert(not is_subtype_of(TypeOf[B], Callable[[str ], B]))
1341
+
1342
+ class C :
1343
+ def __new__ (cls , * args , ** kwargs ) -> " C" :
1344
+ return super ().__new__ (cls )
1345
+
1346
+ def __init__ (self , x : int ) -> None : ...
1347
+
1348
+ # Not subtype because __new__ signature is not fully static
1349
+ static_assert(not is_subtype_of(TypeOf[C], Callable[[int ], C]))
1350
+ static_assert(not is_subtype_of(TypeOf[C], Callable[[], C]))
1351
+
1352
+ class D : ...
1353
+
1354
+ class E :
1355
+ @overload
1356
+ def __new__ (cls ) -> int : ...
1357
+ @overload
1358
+ def __new__ (cls , x : int ) -> D: ...
1359
+ def __new__ (cls , x : int | None = None ) -> int | D:
1360
+ return D()
1361
+
1362
+ def __init__ (self , y : str ) -> None : ...
1363
+
1364
+ static_assert(is_subtype_of(TypeOf[E], Callable[[int ], D]))
1365
+ static_assert(is_subtype_of(TypeOf[E], Callable[[], int ]))
1366
+
1367
+ class F[T]:
1368
+ def __new__ (cls , x : T) -> " F[T]" :
1369
+ return super ().__new__ (cls )
1370
+
1371
+ def __init__ (self , x : T) -> None : ...
1372
+
1373
+ static_assert(is_subtype_of(TypeOf[F[int ]], Callable[[int ], F[int ]]))
1374
+ static_assert(not is_subtype_of(TypeOf[F[int ]], Callable[[str ], F[int ]]))
1375
+ ```
1376
+
1377
+ #### Classes with ` __call__ ` , ` __new__ ` and ` __init__ `
1378
+
1379
+ If ` __call__ ` , ` __new__ ` and ` __init__ ` are all present, ` __call__ ` takes precedence.
1380
+
1381
+ ``` py
1382
+ from typing import Callable
1383
+ from ty_extensions import TypeOf, static_assert, is_subtype_of
1384
+
1385
+ class MetaWithIntReturn (type ):
1386
+ def __call__ (cls ) -> int :
1387
+ return super ().__call__ ()
1388
+
1389
+ class F (metaclass = MetaWithIntReturn ):
1390
+ def __new__ (cls ) -> str :
1391
+ return super ().__new__ (cls )
1392
+
1393
+ def __init__ (self , x : int ) -> None : ...
1394
+
1395
+ static_assert(is_subtype_of(TypeOf[F], Callable[[], int ]))
1396
+ static_assert(not is_subtype_of(TypeOf[F], Callable[[], str ]))
1397
+ static_assert(not is_subtype_of(TypeOf[F], Callable[[int ], F]))
1398
+ ```
1399
+
1269
1400
### Bound methods
1270
1401
1271
1402
``` py
0 commit comments