@@ -1232,3 +1232,225 @@ A = List # OK
1232
1232
B = List[A] # E:10: Missing type parameters for generic type "A"
1233
1233
x: A # E:4: Missing type parameters for generic type "A"
1234
1234
[builtins fixtures/list.pyi]
1235
+
1236
+ [case testDisallowAnyExplicitDefSignature]
1237
+ # flags: --disallow-any-explicit
1238
+
1239
+ from typing import Any, List
1240
+
1241
+ def f(x: Any) -> None: # E: Explicit "Any" is not allowed
1242
+ pass
1243
+
1244
+ def g() -> Any: # E: Explicit "Any" is not allowed
1245
+ pass
1246
+
1247
+ def h() -> List[Any]: # E: Explicit "Any" is not allowed
1248
+ pass
1249
+ [builtins fixtures/list.pyi]
1250
+
1251
+ [case testDisallowAnyExplicitVarDeclaration]
1252
+ # flags: --python-version 3.6 --disallow-any-explicit
1253
+ from typing import Any
1254
+ v: Any = '' # E: Explicit "Any" is not allowed
1255
+ w = '' # type: Any # E: Explicit "Any" is not allowed
1256
+ class X:
1257
+ y = '' # type: Any # E: Explicit "Any" is not allowed
1258
+
1259
+ [case testDisallowAnyExplicitGenericVarDeclaration]
1260
+ # flags: --python-version 3.6 --disallow-any-explicit
1261
+ from typing import Any, List
1262
+ v: List[Any] = [] # E: Explicit "Any" is not allowed
1263
+ [builtins fixtures/list.pyi]
1264
+
1265
+ [case testDisallowAnyExplicitInheritance]
1266
+ # flags: --disallow-any-explicit
1267
+ from typing import Any, List
1268
+
1269
+ class C(Any): # E: Explicit "Any" is not allowed
1270
+ pass
1271
+
1272
+ class D(List[Any]): # E: Explicit "Any" is not allowed
1273
+ pass
1274
+ [builtins fixtures/list.pyi]
1275
+
1276
+ [case testDisallowAnyExplicitAlias]
1277
+ # flags: --disallow-any-explicit
1278
+ from typing import Any, List
1279
+
1280
+ X = Any # E: Explicit "Any" is not allowed
1281
+ Y = List[Any] # E: Explicit "Any" is not allowed
1282
+
1283
+ def foo(x: X) -> Y: # no error
1284
+ x.nonexistent() # no error
1285
+ return x
1286
+ [builtins fixtures/list.pyi]
1287
+
1288
+ [case testDisallowAnyExplicitGenericAlias]
1289
+ # flags: --disallow-any-explicit
1290
+ from typing import Any, TypeVar, Tuple
1291
+
1292
+ T = TypeVar('T')
1293
+
1294
+ TupleAny = Tuple[Any, T] # E: Explicit "Any" is not allowed
1295
+
1296
+ def foo(x: TupleAny[str]) -> None: # no error
1297
+ pass
1298
+
1299
+ def goo(x: TupleAny[Any]) -> None: # E: Explicit "Any" is not allowed
1300
+ pass
1301
+
1302
+ [case testDisallowAnyExplicitCast]
1303
+ # flags: --disallow-any-explicit
1304
+ from typing import Any, List, cast
1305
+
1306
+ x = 1
1307
+ y = cast(Any, x) # E: Explicit "Any" is not allowed
1308
+ z = cast(List[Any], x) # E: Explicit "Any" is not allowed
1309
+ [builtins fixtures/list.pyi]
1310
+
1311
+ [case testDisallowAnyExplicitNamedTuple]
1312
+ # flags: --disallow-any-explicit
1313
+ from typing import Any, List, NamedTuple
1314
+
1315
+ Point = NamedTuple('Point', [('x', List[Any]), ('y', Any)]) # E: Explicit "Any" is not allowed
1316
+ [builtins fixtures/list.pyi]
1317
+
1318
+ [case testDisallowAnyExplicitTypeVarConstraint]
1319
+ # flags: --disallow-any-explicit
1320
+ from typing import Any, List, TypeVar
1321
+
1322
+ T = TypeVar('T', Any, List[Any]) # E: Explicit "Any" is not allowed
1323
+ [builtins fixtures/list.pyi]
1324
+
1325
+ [case testDisallowAnyExplicitNewType]
1326
+ # flags: --disallow-any-explicit
1327
+ from typing import Any, List, NewType
1328
+
1329
+ # this error does not come from `--disallow-any-explicit` flag
1330
+ Baz = NewType('Baz', Any) # E: Argument 2 to NewType(...) must be subclassable (got "Any")
1331
+ Bar = NewType('Bar', List[Any]) # E: Explicit "Any" is not allowed
1332
+ [builtins fixtures/list.pyi]
1333
+
1334
+ [case testDisallowAnyExplicitTypedDictSimple]
1335
+ # flags: --disallow-any-explicit
1336
+ from mypy_extensions import TypedDict
1337
+ from typing import Any
1338
+
1339
+ M = TypedDict('M', {'x': str, 'y': Any}) # E: Explicit "Any" is not allowed
1340
+ M(x='x', y=2) # no error
1341
+ def f(m: M) -> None: pass # no error
1342
+ [builtins fixtures/dict.pyi]
1343
+
1344
+ [case testDisallowAnyExplicitTypedDictGeneric]
1345
+ # flags: --disallow-any-explicit
1346
+ from mypy_extensions import TypedDict
1347
+ from typing import Any, List
1348
+
1349
+ M = TypedDict('M', {'x': str, 'y': List[Any]}) # E: Explicit "Any" is not allowed
1350
+ N = TypedDict('N', {'x': str, 'y': List}) # no error
1351
+ [builtins fixtures/dict.pyi]
1352
+
1353
+ [case testDisallowAnyGenericsTupleNoTypeParams]
1354
+ # flags: --python-version 3.6 --disallow-any-generics
1355
+ from typing import Tuple
1356
+
1357
+ def f(s: Tuple) -> None: pass # E: Missing type parameters for generic type "Tuple"
1358
+ def g(s) -> Tuple: # E: Missing type parameters for generic type "Tuple"
1359
+ return 'a', 'b'
1360
+ def h(s) -> Tuple[str, str]: # no error
1361
+ return 'a', 'b'
1362
+ x: Tuple = () # E: Missing type parameters for generic type "Tuple"
1363
+
1364
+ [case testDisallowAnyGenericsTupleWithNoTypeParamsGeneric]
1365
+ # flags: --disallow-any-generics
1366
+ from typing import Tuple, List
1367
+
1368
+ def f(s: List[Tuple]) -> None: pass # E: Missing type parameters for generic type "Tuple"
1369
+ def g(s: List[Tuple[str, str]]) -> None: pass # no error
1370
+ [builtins fixtures/list.pyi]
1371
+
1372
+ [case testDisallowAnyGenericsTypeType]
1373
+ # flags: --disallow-any-generics
1374
+ from typing import Type, Any
1375
+
1376
+ def f(s: Type[Any]) -> None: pass # no error
1377
+ def g(s) -> Type: # E: Missing type parameters for generic type "Type"
1378
+ return s
1379
+ def h(s) -> Type[str]: # no error
1380
+ return s
1381
+ x: Type = g(0) # E: Missing type parameters for generic type "Type"
1382
+
1383
+ [case testDisallowAnyGenericsAliasGenericType]
1384
+ # flags: --disallow-any-generics
1385
+ from typing import List
1386
+
1387
+ L = List # no error
1388
+
1389
+ def f(l: L) -> None: pass # E: Missing type parameters for generic type "L"
1390
+ def g(l: L[str]) -> None: pass # no error
1391
+ [builtins fixtures/list.pyi]
1392
+
1393
+ [case testDisallowAnyGenericsGenericAlias]
1394
+ # flags: --python-version 3.6 --disallow-any-generics
1395
+ from typing import TypeVar, Tuple
1396
+
1397
+ T = TypeVar('T')
1398
+ A = Tuple[T, str, T]
1399
+
1400
+ def f(s: A) -> None: pass # E: Missing type parameters for generic type "A"
1401
+ def g(s) -> A: # E: Missing type parameters for generic type "A"
1402
+ return 'a', 'b', 1
1403
+ def h(s) -> A[str]: # no error
1404
+ return 'a', 'b', 'c'
1405
+ x: A = ('a', 'b', 1) # E: Missing type parameters for generic type "A"
1406
+
1407
+ [case testDisallowAnyGenericsPlainList]
1408
+ # flags: --python-version 3.6 --disallow-any-generics
1409
+ from typing import List
1410
+
1411
+ def f(l: List) -> None: pass # E: Missing type parameters for generic type "List"
1412
+ def g(l: List[str]) -> None: pass # no error
1413
+ def h(l: List[List]) -> None: pass # E: Missing type parameters for generic type "List"
1414
+ def i(l: List[List[List[List]]]) -> None: pass # E: Missing type parameters for generic type "List"
1415
+
1416
+ x = [] # E: Need type annotation for 'x' (hint: "x: List[<type>] = ...")
1417
+ y: List = [] # E: Missing type parameters for generic type "List"
1418
+ [builtins fixtures/list.pyi]
1419
+
1420
+ [case testDisallowAnyGenericsCustomGenericClass]
1421
+ # flags: --python-version 3.6 --disallow-any-generics
1422
+ from typing import Generic, TypeVar, Any
1423
+
1424
+ T = TypeVar('T')
1425
+ class G(Generic[T]): pass
1426
+
1427
+ def f() -> G: # E: Missing type parameters for generic type "G"
1428
+ return G()
1429
+
1430
+ x: G[Any] = G() # no error
1431
+ y: G = x # E: Missing type parameters for generic type "G"
1432
+
1433
+ [case testDisallowSubclassingAny]
1434
+ # flags: --config-file tmp/mypy.ini
1435
+ import m
1436
+ import y
1437
+
1438
+ [file m.py]
1439
+ from typing import Any
1440
+
1441
+ x = None # type: Any
1442
+
1443
+ class ShouldBeFine(x): ...
1444
+
1445
+ [file y.py]
1446
+ from typing import Any
1447
+
1448
+ x = None # type: Any
1449
+
1450
+ class ShouldNotBeFine(x): ... # E: Class cannot subclass 'x' (has type 'Any')
1451
+
1452
+ [file mypy.ini]
1453
+ \[mypy]
1454
+ disallow_subclassing_any = True
1455
+ \[mypy-m]
1456
+ disallow_subclassing_any = False
0 commit comments