1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
|
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
"""
This module contains the classes for "scoped" node, i.e. which are opening a
new local scope in the language definition : Module, ClassDef, FunctionDef (and
Lambda, GeneratorExp, DictComp and SetComp to some extent).
"""
from __future__ import annotations
import io
import itertools
import os
import sys
import warnings
from collections.abc import Generator, Iterator
from functools import lru_cache
from typing import TYPE_CHECKING, ClassVar, NoReturn, TypeVar, overload
from astroid import bases, util
from astroid import decorators as decorators_mod
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS
from astroid.context import (
CallContext,
InferenceContext,
bind_context_to_node,
copy_context,
)
from astroid.exceptions import (
AstroidBuildingError,
AstroidTypeError,
AttributeInferenceError,
DuplicateBasesError,
InconsistentMroError,
InferenceError,
MroError,
StatementMissing,
TooManyLevelsError,
)
from astroid.interpreter.dunder_lookup import lookup
from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel
from astroid.manager import AstroidManager
from astroid.nodes import Arguments, Const, NodeNG, _base_nodes, node_classes
from astroid.nodes.scoped_nodes.mixin import ComprehensionScope, LocalsDictNodeNG
from astroid.nodes.scoped_nodes.utils import builtin_lookup
from astroid.nodes.utils import Position
from astroid.typing import InferBinaryOp, InferenceResult, SuccessfulInferenceResult
if sys.version_info >= (3, 8):
from functools import cached_property
from typing import Literal
else:
from typing_extensions import Literal
from astroid.decorators import cachedproperty as cached_property
if TYPE_CHECKING:
from astroid import nodes
ITER_METHODS = ("__iter__", "__getitem__")
EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"})
objects = util.lazy_import("objects")
BUILTIN_DESCRIPTORS = frozenset(
{"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"}
)
_T = TypeVar("_T")
def _c3_merge(sequences, cls, context):
"""Merges MROs in *sequences* to a single MRO using the C3 algorithm.
Adapted from http://www.python.org/download/releases/2.3/mro/.
"""
result = []
while True:
sequences = [s for s in sequences if s] # purge empty sequences
if not sequences:
return result
for s1 in sequences: # find merge candidates among seq heads
candidate = s1[0]
for s2 in sequences:
if candidate in s2[1:]:
candidate = None
break # reject the current head, it appears later
else:
break
if not candidate:
# Show all the remaining bases, which were considered as
# candidates for the next mro sequence.
raise InconsistentMroError(
message="Cannot create a consistent method resolution order "
"for MROs {mros} of class {cls!r}.",
mros=sequences,
cls=cls,
context=context,
)
result.append(candidate)
# remove the chosen candidate
for seq in sequences:
if seq[0] == candidate:
del seq[0]
return None
def clean_typing_generic_mro(sequences: list[list[ClassDef]]) -> None:
"""A class can inherit from typing.Generic directly, as base,
and as base of bases. The merged MRO must however only contain the last entry.
To prepare for _c3_merge, remove some typing.Generic entries from
sequences if multiple are present.
This method will check if Generic is in inferred_bases and also
part of bases_mro. If true, remove it from inferred_bases
as well as its entry the bases_mro.
Format sequences: [[self]] + bases_mro + [inferred_bases]
"""
bases_mro = sequences[1:-1]
inferred_bases = sequences[-1]
# Check if Generic is part of inferred_bases
for i, base in enumerate(inferred_bases):
if base.qname() == "typing.Generic":
position_in_inferred_bases = i
break
else:
return
# Check if also part of bases_mro
# Ignore entry for typing.Generic
for i, seq in enumerate(bases_mro):
if i == position_in_inferred_bases:
continue
if any(base.qname() == "typing.Generic" for base in seq):
break
else:
return
# Found multiple Generics in mro, remove entry from inferred_bases
# and the corresponding one from bases_mro
inferred_bases.pop(position_in_inferred_bases)
bases_mro.pop(position_in_inferred_bases)
def clean_duplicates_mro(sequences, cls, context):
for sequence in sequences:
names = [
(node.lineno, node.qname()) if node.name else None for node in sequence
]
last_index = dict(map(reversed, enumerate(names)))
if names and names[0] is not None and last_index[names[0]] != 0:
raise DuplicateBasesError(
message="Duplicates found in MROs {mros} for {cls!r}.",
mros=sequences,
cls=cls,
context=context,
)
yield [
node
for i, (node, name) in enumerate(zip(sequence, names))
if name is None or last_index[name] == i
]
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == "classmethod":
return bases.BoundMethod(n, klass)
if n.type == "property":
return n
if n.type != "staticmethod":
return bases.UnboundMethod(n)
return n
class Module(LocalsDictNodeNG):
"""Class representing an :class:`ast.Module` node.
>>> import astroid
>>> node = astroid.extract_node('import astroid')
>>> node
<Import l.1 at 0x7f23b2e4e5c0>
>>> node.parent
<Module l.0 at 0x7f23b2e4eda0>
"""
_astroid_fields = ("doc_node", "body")
fromlineno: Literal[0] = 0
"""The first line that this node appears on in the source code."""
lineno: Literal[0] = 0
"""The line that this node appears on in the source code."""
# attributes below are set by the builder module or by raw factories
file_bytes: str | bytes | None = None
"""The string/bytes that this ast was built from."""
file_encoding: str | None = None
"""The encoding of the source file.
This is used to get unicode out of a source file.
Python 2 only.
"""
special_attributes = ModuleModel()
"""The names of special attributes that this module has."""
# names of module attributes available through the global scope
scope_attrs = {"__name__", "__doc__", "__file__", "__path__", "__package__"}
"""The names of module attributes available through the global scope."""
_other_fields = (
"name",
"doc",
"file",
"path",
"package",
"pure_python",
"future_imports",
)
_other_other_fields = ("locals", "globals")
col_offset: None
end_lineno: None
end_col_offset: None
parent: None
@decorators_mod.deprecate_arguments(doc="Use the postinit arg 'doc_node' instead")
def __init__(
self,
name: str,
doc: str | None = None,
file: str | None = None,
path: list[str] | None = None,
package: bool | None = None,
parent: None = None,
pure_python: bool | None = True,
) -> None:
"""
:param name: The name of the module.
:param doc: The module docstring.
:param file: The path to the file that this ast has been extracted from.
:param path:
:param package: Whether the node represents a package or a module.
:param parent: The parent node in the syntax tree.
:param pure_python: Whether the ast was built from source.
"""
self.name = name
"""The name of the module."""
self._doc = doc
"""The module docstring."""
self.file = file
"""The path to the file that this ast has been extracted from.
This will be ``None`` when the representation has been built from a
built-in module.
"""
self.path = path
self.package = package
"""Whether the node represents a package or a module."""
self.pure_python = pure_python
"""Whether the ast was built from source."""
self.globals: dict[str, list[node_classes.NodeNG]]
"""A map of the name of a global variable to the node defining the global."""
self.locals = self.globals = {}
"""A map of the name of a local variable to the node defining the local."""
self.body: list[node_classes.NodeNG] | None = []
"""The contents of the module."""
self.doc_node: Const | None = None
"""The doc node associated with this node."""
self.future_imports: set[str] = set()
"""The imports from ``__future__``."""
super().__init__(lineno=0, parent=parent)
# pylint: enable=redefined-builtin
def postinit(self, body=None, *, doc_node: Const | None = None):
"""Do some setup after initialisation.
:param body: The contents of the module.
:type body: list(NodeNG) or None
:param doc_node: The doc node associated with this node.
"""
self.body = body
self.doc_node = doc_node
if doc_node:
self._doc = doc_node.value
@property
def doc(self) -> str | None:
"""The module docstring."""
warnings.warn(
"The 'Module.doc' attribute is deprecated, "
"use 'Module.doc_node' instead.",
DeprecationWarning,
stacklevel=2,
)
return self._doc
@doc.setter
def doc(self, value: str | None) -> None:
warnings.warn(
"Setting the 'Module.doc' attribute is deprecated, "
"use 'Module.doc_node' instead.",
DeprecationWarning,
stacklevel=2,
)
self._doc = value
def _get_stream(self):
if self.file_bytes is not None:
return io.BytesIO(self.file_bytes)
if self.file is not None:
# pylint: disable=consider-using-with
stream = open(self.file, "rb")
return stream
return None
def stream(self):
"""Get a stream to the underlying file or bytes.
:type: file or io.BytesIO or None
"""
return self._get_stream()
def block_range(self, lineno):
"""Get a range from where this node starts to where this node ends.
:param lineno: Unused.
:type lineno: int
:returns: The range of line numbers that this node belongs to.
:rtype: tuple(int, int)
"""
return self.fromlineno, self.tolineno
def scope_lookup(self, node, name, offset=0):
"""Lookup where the given variable is assigned.
:param node: The node to look for assignments up to.
Any assignments after the given node are ignored.
:type node: NodeNG
:param name: The name of the variable to find assignments for.
:type name: str
:param offset: The line offset to filter statements up to.
:type offset: int
:returns: This scope node and the list of assignments associated to the
given name according to the scope where it has been found (locals,
globals or builtin).
:rtype: tuple(str, list(NodeNG))
"""
if name in self.scope_attrs and name not in self.locals:
try:
return self, self.getattr(name)
except AttributeInferenceError:
return self, ()
return self._scope_lookup(node, name, offset)
def pytype(self) -> Literal["builtins.module"]:
"""Get the name of the type that this node represents.
:returns: The name of the type.
"""
return "builtins.module"
def display_type(self) -> str:
"""A human readable type of this node.
:returns: The type of this node.
:rtype: str
"""
return "Module"
def getattr(
self, name, context: InferenceContext | None = None, ignore_locals=False
):
if not name:
raise AttributeInferenceError(target=self, attribute=name, context=context)
result = []
name_in_locals = name in self.locals
if name in self.special_attributes and not ignore_locals and not name_in_locals:
result = [self.special_attributes.lookup(name)]
elif not ignore_locals and name_in_locals:
result = self.locals[name]
elif self.package:
try:
result = [self.import_module(name, relative_only=True)]
except (AstroidBuildingError, SyntaxError) as exc:
raise AttributeInferenceError(
target=self, attribute=name, context=context
) from exc
result = [n for n in result if not isinstance(n, node_classes.DelName)]
if result:
return result
raise AttributeInferenceError(target=self, attribute=name, context=context)
def igetattr(self, name, context: InferenceContext | None = None):
"""Infer the possible values of the given variable.
:param name: The name of the variable to infer.
:type name: str
:returns: The inferred possible values.
:rtype: iterable(NodeNG) or None
"""
# set lookup name since this is necessary to infer on import nodes for
# instance
context = copy_context(context)
context.lookupname = name
try:
return bases._infer_stmts(self.getattr(name, context), context, frame=self)
except AttributeInferenceError as error:
raise InferenceError(
str(error), target=self, attribute=name, context=context
) from error
def fully_defined(self) -> bool:
"""Check if this module has been build from a .py file.
If so, the module contains a complete representation,
including the code.
:returns: Whether the module has been built from a .py file.
"""
return self.file is not None and self.file.endswith(".py")
@overload
def statement(self, *, future: None = ...) -> Module:
...
@overload
def statement(self, *, future: Literal[True]) -> NoReturn:
...
def statement(self, *, future: Literal[None, True] = None) -> Module | NoReturn:
"""The first parent node, including self, marked as statement node.
When called on a :class:`Module` with the future parameter this raises an error.
TODO: Deprecate the future parameter and only raise StatementMissing
:raises StatementMissing: If no self has no parent attribute and future is True
"""
if future:
raise StatementMissing(target=self)
warnings.warn(
"In astroid 3.0.0 NodeNG.statement() will return either a nodes.Statement "
"or raise a StatementMissing exception. nodes.Module will no longer be "
"considered a statement. This behaviour can already be triggered "
"by passing 'future=True' to a statement() call.",
DeprecationWarning,
stacklevel=2,
)
return self
def previous_sibling(self):
"""The previous sibling statement.
:returns: The previous sibling statement node.
:rtype: NodeNG or None
"""
def next_sibling(self):
"""The next sibling statement node.
:returns: The next sibling statement node.
:rtype: NodeNG or None
"""
_absolute_import_activated = True
def absolute_import_activated(self) -> bool:
"""Whether :pep:`328` absolute import behaviour has been enabled.
:returns: Whether :pep:`328` has been enabled.
"""
return self._absolute_import_activated
def import_module(
self,
modname: str | None,
relative_only: bool = False,
level: int | None = None,
use_cache: bool = True,
) -> Module:
"""Get the ast for a given module as if imported from this module.
:param modname: The name of the module to "import".
:param relative_only: Whether to only consider relative imports.
:param level: The level of relative import.
:param use_cache: Whether to use the astroid_cache of modules.
:returns: The imported module ast.
"""
if relative_only and level is None:
level = 0
absmodname = self.relative_to_absolute_name(modname, level)
try:
return AstroidManager().ast_from_module_name(
absmodname, use_cache=use_cache
)
except AstroidBuildingError:
# we only want to import a sub module or package of this module,
# skip here
if relative_only:
raise
return AstroidManager().ast_from_module_name(modname)
def relative_to_absolute_name(
self, modname: str | None, level: int | None
) -> str | None:
"""Get the absolute module name for a relative import.
The relative import can be implicit or explicit.
:param modname: The module name to convert.
:param level: The level of relative import.
:returns: The absolute module name.
:raises TooManyLevelsError: When the relative import refers to a
module too far above this one.
"""
# XXX this returns non sens when called on an absolute import
# like 'pylint.checkers.astroid.utils'
# XXX doesn't return absolute name if self.name isn't absolute name
if self.absolute_import_activated() and level is None:
return modname
if level:
if self.package:
level = level - 1
package_name = self.name.rsplit(".", level)[0]
elif (
self.path
and not os.path.exists(os.path.dirname(self.path[0]) + "/__init__.py")
and os.path.exists(
os.path.dirname(self.path[0]) + "/" + modname.split(".")[0]
)
):
level = level - 1
package_name = ""
else:
package_name = self.name.rsplit(".", level)[0]
if level and self.name.count(".") < level:
raise TooManyLevelsError(level=level, name=self.name)
elif self.package:
package_name = self.name
else:
package_name = self.name.rsplit(".", 1)[0]
if package_name:
if not modname:
return package_name
return f"{package_name}.{modname}"
return modname
def wildcard_import_names(self):
"""The list of imported names when this module is 'wildcard imported'.
It doesn't include the '__builtins__' name which is added by the
current CPython implementation of wildcard imports.
:returns: The list of imported names.
:rtype: list(str)
"""
# We separate the different steps of lookup in try/excepts
# to avoid catching too many Exceptions
default = [name for name in self.keys() if not name.startswith("_")]
try:
all_values = self["__all__"]
except KeyError:
return default
try:
explicit = next(all_values.assigned_stmts())
except (InferenceError, StopIteration):
return default
except AttributeError:
# not an assignment node
# XXX infer?
return default
# Try our best to detect the exported name.
inferred = []
try:
explicit = next(explicit.infer())
except (InferenceError, StopIteration):
return default
if not isinstance(explicit, (node_classes.Tuple, node_classes.List)):
return default
def str_const(node) -> bool:
return isinstance(node, node_classes.Const) and isinstance(node.value, str)
for node in explicit.elts:
if str_const(node):
inferred.append(node.value)
else:
try:
inferred_node = next(node.infer())
except (InferenceError, StopIteration):
continue
if str_const(inferred_node):
inferred.append(inferred_node.value)
return inferred
def public_names(self):
"""The list of the names that are publicly available in this module.
:returns: The list of public names.
:rtype: list(str)
"""
return [name for name in self.keys() if not name.startswith("_")]
def bool_value(self, context: InferenceContext | None = None) -> bool:
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`Module` this is always ``True``.
"""
return True
def get_children(self):
yield from self.body
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
"""The node's frame node.
A frame node is a :class:`Module`, :class:`FunctionDef`,
:class:`ClassDef` or :class:`Lambda`.
:returns: The node itself.
"""
return self
class GeneratorExp(ComprehensionScope):
"""Class representing an :class:`ast.GeneratorExp` node.
>>> import astroid
>>> node = astroid.extract_node('(thing for thing in things if thing)')
>>> node
<GeneratorExp l.1 at 0x7f23b2e4e400>
"""
_astroid_fields = ("elt", "generators")
_other_other_fields = ("locals",)
elt = None
"""The element that forms the output of the expression.
:type: NodeNG or None
"""
def __init__(
self,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.locals = {}
"""A map of the name of a local variable to the node defining the local."""
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
def postinit(self, elt=None, generators: list[nodes.Comprehension] | None = None):
"""Do some setup after initialisation.
:param elt: The element that forms the output of the expression.
:type elt: NodeNG or None
:param generators: The generators that are looped through.
"""
self.elt = elt
if generators is None:
self.generators = []
else:
self.generators = generators
def bool_value(self, context: InferenceContext | None = None) -> Literal[True]:
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`GeneratorExp` this is always ``True``.
"""
return True
def get_children(self):
yield self.elt
yield from self.generators
class DictComp(ComprehensionScope):
"""Class representing an :class:`ast.DictComp` node.
>>> import astroid
>>> node = astroid.extract_node('{k:v for k, v in things if k > v}')
>>> node
<DictComp l.1 at 0x7f23b2e41d68>
"""
_astroid_fields = ("key", "value", "generators")
_other_other_fields = ("locals",)
key = None
"""What produces the keys.
:type: NodeNG or None
"""
value = None
"""What produces the values.
:type: NodeNG or None
"""
def __init__(
self,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.locals = {}
"""A map of the name of a local variable to the node defining the local."""
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
def postinit(
self,
key=None,
value=None,
generators: list[nodes.Comprehension] | None = None,
):
"""Do some setup after initialisation.
:param key: What produces the keys.
:type key: NodeNG or None
:param value: What produces the values.
:type value: NodeNG or None
:param generators: The generators that are looped through.
"""
self.key = key
self.value = value
if generators is None:
self.generators = []
else:
self.generators = generators
def bool_value(self, context: InferenceContext | None = None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`DictComp` this is always :class:`Uninferable`.
:rtype: Uninferable
"""
return util.Uninferable
def get_children(self):
yield self.key
yield self.value
yield from self.generators
class SetComp(ComprehensionScope):
"""Class representing an :class:`ast.SetComp` node.
>>> import astroid
>>> node = astroid.extract_node('{thing for thing in things if thing}')
>>> node
<SetComp l.1 at 0x7f23b2e41898>
"""
_astroid_fields = ("elt", "generators")
_other_other_fields = ("locals",)
elt = None
"""The element that forms the output of the expression.
:type: NodeNG or None
"""
def __init__(
self,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.locals = {}
"""A map of the name of a local variable to the node defining the local."""
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
def postinit(self, elt=None, generators: list[nodes.Comprehension] | None = None):
"""Do some setup after initialisation.
:param elt: The element that forms the output of the expression.
:type elt: NodeNG or None
:param generators: The generators that are looped through.
"""
self.elt = elt
if generators is None:
self.generators = []
else:
self.generators = generators
def bool_value(self, context: InferenceContext | None = None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`SetComp` this is always :class:`Uninferable`.
:rtype: Uninferable
"""
return util.Uninferable
def get_children(self):
yield self.elt
yield from self.generators
class ListComp(ComprehensionScope):
"""Class representing an :class:`ast.ListComp` node.
>>> import astroid
>>> node = astroid.extract_node('[thing for thing in things if thing]')
>>> node
<ListComp l.1 at 0x7f23b2e418d0>
"""
_astroid_fields = ("elt", "generators")
_other_other_fields = ("locals",)
elt = None
"""The element that forms the output of the expression.
:type: NodeNG or None
"""
def __init__(
self,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
self.locals = {}
"""A map of the name of a local variable to the node defining it."""
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
def postinit(self, elt=None, generators: list[nodes.Comprehension] | None = None):
"""Do some setup after initialisation.
:param elt: The element that forms the output of the expression.
:type elt: NodeNG or None
:param generators: The generators that are looped through.
:type generators: list(Comprehension) or None
"""
self.elt = elt
if generators is None:
self.generators = []
else:
self.generators = generators
def bool_value(self, context: InferenceContext | None = None):
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`ListComp` this is always :class:`Uninferable`.
:rtype: Uninferable
"""
return util.Uninferable
def get_children(self):
yield self.elt
yield from self.generators
def _infer_decorator_callchain(node):
"""Detect decorator call chaining and see if the end result is a
static or a classmethod.
"""
if not isinstance(node, FunctionDef):
return None
if not node.parent:
return None
try:
result = next(node.infer_call_result(node.parent), None)
except InferenceError:
return None
if isinstance(result, bases.Instance):
result = result._proxied
if isinstance(result, ClassDef):
if result.is_subtype_of("builtins.classmethod"):
return "classmethod"
if result.is_subtype_of("builtins.staticmethod"):
return "staticmethod"
if isinstance(result, FunctionDef):
if not result.decorators:
return None
# Determine if this function is decorated with one of the builtin descriptors we want.
for decorator in result.decorators.nodes:
if isinstance(decorator, node_classes.Name):
if decorator.name in BUILTIN_DESCRIPTORS:
return decorator.name
if (
isinstance(decorator, node_classes.Attribute)
and isinstance(decorator.expr, node_classes.Name)
and decorator.expr.name == "builtins"
and decorator.attrname in BUILTIN_DESCRIPTORS
):
return decorator.attrname
return None
class Lambda(_base_nodes.FilterStmtsBaseNode, LocalsDictNodeNG):
"""Class representing an :class:`ast.Lambda` node.
>>> import astroid
>>> node = astroid.extract_node('lambda arg: arg + 1')
>>> node
<Lambda.<lambda> l.1 at 0x7f23b2e41518>
"""
_astroid_fields: ClassVar[tuple[str, ...]] = ("args", "body")
_other_other_fields: ClassVar[tuple[str, ...]] = ("locals",)
name = "<lambda>"
is_lambda = True
special_attributes = FunctionModel()
"""The names of special attributes that this function has."""
def implicit_parameters(self) -> Literal[0]:
return 0
@property
def type(self) -> Literal["method", "function"]:
"""Whether this is a method or function.
:returns: 'method' if this is a method, 'function' otherwise.
"""
if self.args.arguments and self.args.arguments[0].name == "self":
if isinstance(self.parent.scope(), ClassDef):
return "method"
return "function"
def __init__(
self,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.locals = {}
"""A map of the name of a local variable to the node defining it."""
self.args: Arguments
"""The arguments that the function takes."""
self.body = []
"""The contents of the function body.
:type: list(NodeNG)
"""
self.instance_attrs: dict[str, list[NodeNG]] = {}
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
def postinit(self, args: Arguments, body):
"""Do some setup after initialisation.
:param args: The arguments that the function takes.
:param body: The contents of the function body.
:type body: list(NodeNG)
"""
self.args = args
self.body = body
def pytype(self) -> Literal["builtins.instancemethod", "builtins.function"]:
"""Get the name of the type that this node represents.
:returns: The name of the type.
"""
if "method" in self.type:
return "builtins.instancemethod"
return "builtins.function"
def display_type(self) -> str:
"""A human readable type of this node.
:returns: The type of this node.
:rtype: str
"""
if "method" in self.type:
return "Method"
return "Function"
def callable(self) -> Literal[True]:
"""Whether this node defines something that is callable.
:returns: Whether this defines something that is callable
For a :class:`Lambda` this is always ``True``.
"""
return True
def argnames(self) -> list[str]:
"""Get the names of each of the arguments, including that
of the collections of variable-length arguments ("args", "kwargs",
etc.), as well as positional-only and keyword-only arguments.
:returns: The names of the arguments.
:rtype: list(str)
"""
if self.args.arguments: # maybe None with builtin functions
names = _rec_get_names(self.args.arguments)
else:
names = []
if self.args.vararg:
names.append(self.args.vararg)
names += [elt.name for elt in self.args.kwonlyargs]
if self.args.kwarg:
names.append(self.args.kwarg)
return names
def infer_call_result(self, caller, context: InferenceContext | None = None):
"""Infer what the function returns when called.
:param caller: Unused
:type caller: object
"""
# pylint: disable=no-member; github.com/pylint-dev/astroid/issues/291
# args is in fact redefined later on by postinit. Can't be changed
# to None due to a strong interaction between Lambda and FunctionDef.
return self.body.infer(context)
def scope_lookup(self, node, name, offset=0):
"""Lookup where the given names is assigned.
:param node: The node to look for assignments up to.
Any assignments after the given node are ignored.
:type node: NodeNG
:param name: The name to find assignments for.
:type name: str
:param offset: The line offset to filter statements up to.
:type offset: int
:returns: This scope node and the list of assignments associated to the
given name according to the scope where it has been found (locals,
globals or builtin).
:rtype: tuple(str, list(NodeNG))
"""
if node in self.args.defaults or node in self.args.kw_defaults:
frame = self.parent.frame(future=True)
# line offset to avoid that def func(f=func) resolve the default
# value to the defined function
offset = -1
else:
# check this is not used in function decorators
frame = self
return frame._scope_lookup(node, name, offset)
def bool_value(self, context: InferenceContext | None = None) -> Literal[True]:
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`Lambda` this is always ``True``.
"""
return True
def get_children(self):
yield self.args
yield self.body
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
"""The node's frame node.
A frame node is a :class:`Module`, :class:`FunctionDef`,
:class:`ClassDef` or :class:`Lambda`.
:returns: The node itself.
"""
return self
def getattr(
self, name: str, context: InferenceContext | None = None
) -> list[NodeNG]:
if not name:
raise AttributeInferenceError(target=self, attribute=name, context=context)
found_attrs = []
if name in self.instance_attrs:
found_attrs = self.instance_attrs[name]
if name in self.special_attributes:
found_attrs.append(self.special_attributes.lookup(name))
if found_attrs:
return found_attrs
raise AttributeInferenceError(target=self, attribute=name)
class FunctionDef(_base_nodes.MultiLineBlockNode, _base_nodes.Statement, Lambda):
"""Class representing an :class:`ast.FunctionDef`.
>>> import astroid
>>> node = astroid.extract_node('''
... def my_func(arg):
... return arg + 1
... ''')
>>> node
<FunctionDef.my_func l.2 at 0x7f23b2e71e10>
"""
_astroid_fields = ("decorators", "args", "returns", "doc_node", "body")
_multi_line_block_fields = ("body",)
returns = None
decorators: node_classes.Decorators | None = None
"""The decorators that are applied to this method or function."""
is_function = True
"""Whether this node indicates a function.
For a :class:`FunctionDef` this is always ``True``.
:type: bool
"""
type_annotation = None
"""If present, this will contain the type annotation passed by a type comment
:type: NodeNG or None
"""
type_comment_args = None
"""
If present, this will contain the type annotation for arguments
passed by a type comment
"""
type_comment_returns = None
"""If present, this will contain the return type annotation, passed by a type comment"""
# attributes below are set by the builder module or by raw factories
_other_fields = ("name", "doc", "position")
_other_other_fields = (
"locals",
"_type",
"type_comment_returns",
"type_comment_args",
)
_type = None
@decorators_mod.deprecate_arguments(doc="Use the postinit arg 'doc_node' instead")
def __init__(
self,
name=None,
doc: str | None = None,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param name: The name of the function.
:type name: str or None
:param doc: The function docstring.
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.name = name
"""The name of the function.
:type name: str or None
"""
self._doc = doc
"""The function docstring."""
self.doc_node: Const | None = None
"""The doc node associated with this node."""
self.instance_attrs = {}
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
if parent:
frame = parent.frame(future=True)
frame.set_local(name, self)
def postinit(
self,
args: Arguments,
body,
decorators: node_classes.Decorators | None = None,
returns=None,
type_comment_returns=None,
type_comment_args=None,
*,
position: Position | None = None,
doc_node: Const | None = None,
):
"""Do some setup after initialisation.
:param args: The arguments that the function takes.
:param body: The contents of the function body.
:type body: list(NodeNG)
:param decorators: The decorators that are applied to this
method or function.
:type decorators: Decorators or None
:params type_comment_returns:
The return type annotation passed via a type comment.
:params type_comment_args:
The args type annotation passed via a type comment.
:params position:
Position of function keyword(s) and name.
:param doc_node:
The doc node associated with this node.
"""
self.args = args
self.body = body
self.decorators = decorators
self.returns = returns
self.type_comment_returns = type_comment_returns
self.type_comment_args = type_comment_args
self.position = position
self.doc_node = doc_node
if doc_node:
self._doc = doc_node.value
@property
def doc(self) -> str | None:
"""The function docstring."""
warnings.warn(
"The 'FunctionDef.doc' attribute is deprecated, "
"use 'FunctionDef.doc_node' instead.",
DeprecationWarning,
stacklevel=2,
)
return self._doc
@doc.setter
def doc(self, value: str | None) -> None:
warnings.warn(
"Setting the 'FunctionDef.doc' attribute is deprecated, "
"use 'FunctionDef.doc_node' instead.",
DeprecationWarning,
stacklevel=2,
)
self._doc = value
@cached_property
def extra_decorators(self) -> list[node_classes.Call]:
"""The extra decorators that this function can have.
Additional decorators are considered when they are used as
assignments, as in ``method = staticmethod(method)``.
The property will return all the callables that are used for
decoration.
"""
frame = self.parent.frame(future=True)
if not isinstance(frame, ClassDef):
return []
decorators: list[node_classes.Call] = []
for assign in frame._assign_nodes_in_scope:
if isinstance(assign.value, node_classes.Call) and isinstance(
assign.value.func, node_classes.Name
):
for assign_node in assign.targets:
if not isinstance(assign_node, node_classes.AssignName):
# Support only `name = callable(name)`
continue
if assign_node.name != self.name:
# Interested only in the assignment nodes that
# decorates the current method.
continue
try:
meth = frame[self.name]
except KeyError:
continue
else:
# Must be a function and in the same frame as the
# original method.
if (
isinstance(meth, FunctionDef)
and assign_node.frame(future=True) == frame
):
decorators.append(assign.value)
return decorators
@cached_property
def type(self) -> str: # pylint: disable=too-many-return-statements # noqa: C901
"""The function type for this node.
Possible values are: method, function, staticmethod, classmethod.
"""
for decorator in self.extra_decorators:
if decorator.func.name in BUILTIN_DESCRIPTORS:
return decorator.func.name
frame = self.parent.frame(future=True)
type_name = "function"
if isinstance(frame, ClassDef):
if self.name == "__new__":
return "classmethod"
if self.name == "__init_subclass__":
return "classmethod"
if self.name == "__class_getitem__":
return "classmethod"
type_name = "method"
if not self.decorators:
return type_name
for node in self.decorators.nodes:
if isinstance(node, node_classes.Name):
if node.name in BUILTIN_DESCRIPTORS:
return node.name
if (
isinstance(node, node_classes.Attribute)
and isinstance(node.expr, node_classes.Name)
and node.expr.name == "builtins"
and node.attrname in BUILTIN_DESCRIPTORS
):
return node.attrname
if isinstance(node, node_classes.Call):
# Handle the following case:
# @some_decorator(arg1, arg2)
# def func(...)
#
try:
current = next(node.func.infer())
except (InferenceError, StopIteration):
continue
_type = _infer_decorator_callchain(current)
if _type is not None:
return _type
try:
for inferred in node.infer():
# Check to see if this returns a static or a class method.
_type = _infer_decorator_callchain(inferred)
if _type is not None:
return _type
if not isinstance(inferred, ClassDef):
continue
for ancestor in inferred.ancestors():
if not isinstance(ancestor, ClassDef):
continue
if ancestor.is_subtype_of("builtins.classmethod"):
return "classmethod"
if ancestor.is_subtype_of("builtins.staticmethod"):
return "staticmethod"
except InferenceError:
pass
return type_name
@cached_property
def fromlineno(self) -> int | None:
"""The first line that this node appears on in the source code."""
# lineno is the line number of the first decorator, we want the def
# statement lineno. Similar to 'ClassDef.fromlineno'
lineno = self.lineno
if self.decorators is not None:
lineno += sum(
node.tolineno - node.lineno + 1 for node in self.decorators.nodes
)
return lineno
@cached_property
def blockstart_tolineno(self):
"""The line on which the beginning of this block ends.
:type: int
"""
return self.args.tolineno
def implicit_parameters(self) -> Literal[0, 1]:
return 1 if self.is_bound() else 0
def block_range(self, lineno):
"""Get a range from the given line number to where this node ends.
:param lineno: Unused.
:type lineno: int
:returns: The range of line numbers that this node belongs to,
:rtype: tuple(int, int)
"""
return self.fromlineno, self.tolineno
def igetattr(self, name, context: InferenceContext | None = None):
"""Inferred getattr, which returns an iterator of inferred statements."""
try:
return bases._infer_stmts(self.getattr(name, context), context, frame=self)
except AttributeInferenceError as error:
raise InferenceError(
str(error), target=self, attribute=name, context=context
) from error
def is_method(self) -> bool:
"""Check if this function node represents a method.
:returns: Whether this is a method.
"""
# check we are defined in a ClassDef, because this is usually expected
# (e.g. pylint...) when is_method() return True
return self.type != "function" and isinstance(
self.parent.frame(future=True), ClassDef
)
def decoratornames(self, context: InferenceContext | None = None):
"""Get the qualified names of each of the decorators on this function.
:param context:
An inference context that can be passed to inference functions
:returns: The names of the decorators.
:rtype: set(str)
"""
result = set()
decoratornodes = []
if self.decorators is not None:
decoratornodes += self.decorators.nodes
decoratornodes += self.extra_decorators
for decnode in decoratornodes:
try:
for infnode in decnode.infer(context=context):
result.add(infnode.qname())
except InferenceError:
continue
return result
def is_bound(self) -> bool:
"""Check if the function is bound to an instance or class.
:returns: Whether the function is bound to an instance or class.
"""
return self.type in {"method", "classmethod"}
def is_abstract(self, pass_is_abstract=True, any_raise_is_abstract=False) -> bool:
"""Check if the method is abstract.
A method is considered abstract if any of the following is true:
* The only statement is 'raise NotImplementedError'
* The only statement is 'raise <SomeException>' and any_raise_is_abstract is True
* The only statement is 'pass' and pass_is_abstract is True
* The method is annotated with abc.astractproperty/abc.abstractmethod
:returns: Whether the method is abstract.
"""
if self.decorators:
for node in self.decorators.nodes:
try:
inferred = next(node.infer())
except (InferenceError, StopIteration):
continue
if inferred and inferred.qname() in {
"abc.abstractproperty",
"abc.abstractmethod",
}:
return True
for child_node in self.body:
if isinstance(child_node, node_classes.Raise):
if any_raise_is_abstract:
return True
if child_node.raises_not_implemented():
return True
return pass_is_abstract and isinstance(child_node, node_classes.Pass)
# empty function is the same as function with a single "pass" statement
if pass_is_abstract:
return True
return False
def is_generator(self) -> bool:
"""Check if this is a generator function.
:returns: Whether this is a generator function.
"""
return bool(next(self._get_yield_nodes_skip_lambdas(), False))
def infer_yield_result(self, context: InferenceContext | None = None):
"""Infer what the function yields when called
:returns: What the function yields
:rtype: iterable(NodeNG or Uninferable) or None
"""
# pylint: disable=not-an-iterable
# https://github.com/pylint-dev/astroid/issues/1015
for yield_ in self.nodes_of_class(node_classes.Yield):
if yield_.value is None:
const = node_classes.Const(None)
const.parent = yield_
const.lineno = yield_.lineno
yield const
elif yield_.scope() == self:
yield from yield_.value.infer(context=context)
def infer_call_result(self, caller=None, context: InferenceContext | None = None):
"""Infer what the function returns when called.
:returns: What the function returns.
:rtype: iterable(NodeNG or Uninferable) or None
"""
if self.is_generator():
if isinstance(self, AsyncFunctionDef):
generator_cls = bases.AsyncGenerator
else:
generator_cls = bases.Generator
result = generator_cls(self, generator_initial_context=context)
yield result
return
# This is really a gigantic hack to work around metaclass generators
# that return transient class-generating functions. Pylint's AST structure
# cannot handle a base class object that is only used for calling __new__,
# but does not contribute to the inheritance structure itself. We inject
# a fake class into the hierarchy here for several well-known metaclass
# generators, and filter it out later.
if (
self.name == "with_metaclass"
and len(self.args.args) == 1
and self.args.vararg is not None
):
metaclass = next(caller.args[0].infer(context), None)
if isinstance(metaclass, ClassDef):
try:
class_bases = [
# Find the first non-None inferred base value
next(
b
for b in arg.infer(context=context.clone())
if not (isinstance(b, Const) and b.value is None)
)
for arg in caller.args[1:]
]
except StopIteration as e:
raise InferenceError(node=caller.args[1:], context=context) from e
new_class = ClassDef(name="temporary_class")
new_class.hide = True
new_class.parent = self
new_class.postinit(
bases=[
base
for base in class_bases
if not isinstance(base, util.UninferableBase)
],
body=[],
decorators=[],
metaclass=metaclass,
)
yield new_class
return
returns = self._get_return_nodes_skip_functions()
first_return = next(returns, None)
if not first_return:
if self.body:
if self.is_abstract(pass_is_abstract=True, any_raise_is_abstract=True):
yield util.Uninferable
else:
yield node_classes.Const(None)
return
raise InferenceError("The function does not have any return statements")
for returnnode in itertools.chain((first_return,), returns):
if returnnode.value is None:
yield node_classes.Const(None)
else:
try:
yield from returnnode.value.infer(context)
except InferenceError:
yield util.Uninferable
def bool_value(self, context: InferenceContext | None = None) -> bool:
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`FunctionDef` this is always ``True``.
"""
return True
def get_children(self):
if self.decorators is not None:
yield self.decorators
yield self.args
if self.returns is not None:
yield self.returns
yield from self.body
def scope_lookup(self, node, name, offset=0):
"""Lookup where the given name is assigned."""
if name == "__class__":
# __class__ is an implicit closure reference created by the compiler
# if any methods in a class body refer to either __class__ or super.
# In our case, we want to be able to look it up in the current scope
# when `__class__` is being used.
frame = self.parent.frame(future=True)
if isinstance(frame, ClassDef):
return self, [frame]
return super().scope_lookup(node, name, offset)
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
"""The node's frame node.
A frame node is a :class:`Module`, :class:`FunctionDef`,
:class:`ClassDef` or :class:`Lambda`.
:returns: The node itself.
"""
return self
class AsyncFunctionDef(FunctionDef):
"""Class representing an :class:`ast.FunctionDef` node.
A :class:`AsyncFunctionDef` is an asynchronous function
created with the `async` keyword.
>>> import astroid
>>> node = astroid.extract_node('''
async def func(things):
async for thing in things:
print(thing)
''')
>>> node
<AsyncFunctionDef.func l.2 at 0x7f23b2e416d8>
>>> node.body[0]
<AsyncFor l.3 at 0x7f23b2e417b8>
"""
def _rec_get_names(args, names: list[str] | None = None) -> list[str]:
"""return a list of all argument names"""
if names is None:
names = []
for arg in args:
if isinstance(arg, node_classes.Tuple):
_rec_get_names(arg.elts, names)
else:
names.append(arg.name)
return names
def _is_metaclass(klass, seen=None) -> bool:
"""Return if the given class can be
used as a metaclass.
"""
if klass.name == "type":
return True
if seen is None:
seen = set()
for base in klass.bases:
try:
for baseobj in base.infer():
baseobj_name = baseobj.qname()
if baseobj_name in seen:
continue
seen.add(baseobj_name)
if isinstance(baseobj, bases.Instance):
# not abstract
return False
if baseobj is klass:
continue
if not isinstance(baseobj, ClassDef):
continue
if baseobj._type == "metaclass":
return True
if _is_metaclass(baseobj, seen):
return True
except InferenceError:
continue
return False
def _class_type(klass, ancestors=None):
"""return a ClassDef node type to differ metaclass and exception
from 'regular' classes
"""
# XXX we have to store ancestors in case we have an ancestor loop
if klass._type is not None:
return klass._type
if _is_metaclass(klass):
klass._type = "metaclass"
elif klass.name.endswith("Exception"):
klass._type = "exception"
else:
if ancestors is None:
ancestors = set()
klass_name = klass.qname()
if klass_name in ancestors:
# XXX we are in loop ancestors, and have found no type
klass._type = "class"
return "class"
ancestors.add(klass_name)
for base in klass.ancestors(recurs=False):
name = _class_type(base, ancestors)
if name != "class":
if name == "metaclass" and not _is_metaclass(klass):
# don't propagate it if the current class
# can't be a metaclass
continue
klass._type = base.type
break
if klass._type is None:
klass._type = "class"
return klass._type
def get_wrapping_class(node):
"""Get the class that wraps the given node.
We consider that a class wraps a node if the class
is a parent for the said node.
:returns: The class that wraps the given node
:rtype: ClassDef or None
"""
klass = node.frame(future=True)
while klass is not None and not isinstance(klass, ClassDef):
if klass.parent is None:
klass = None
else:
klass = klass.parent.frame(future=True)
return klass
# pylint: disable=too-many-instance-attributes
class ClassDef(
_base_nodes.FilterStmtsBaseNode, LocalsDictNodeNG, _base_nodes.Statement
):
"""Class representing an :class:`ast.ClassDef` node.
>>> import astroid
>>> node = astroid.extract_node('''
class Thing:
def my_meth(self, arg):
return arg + self.offset
''')
>>> node
<ClassDef.Thing l.2 at 0x7f23b2e9e748>
"""
# some of the attributes below are set by the builder module or
# by a raw factories
# a dictionary of class instances attributes
_astroid_fields = ("decorators", "bases", "keywords", "doc_node", "body") # name
decorators = None
"""The decorators that are applied to this class.
:type: Decorators or None
"""
special_attributes = ClassModel()
"""The names of special attributes that this class has.
:type: objectmodel.ClassModel
"""
_type = None
_metaclass: NodeNG | None = None
_metaclass_hack = False
hide = False
type = property(
_class_type,
doc=(
"The class type for this node.\n\n"
"Possible values are: class, metaclass, exception.\n\n"
":type: str"
),
)
_other_fields = ("name", "doc", "is_dataclass", "position")
_other_other_fields = ("locals", "_newstyle")
_newstyle = None
@decorators_mod.deprecate_arguments(doc="Use the postinit arg 'doc_node' instead")
def __init__(
self,
name=None,
doc: str | None = None,
lineno=None,
col_offset=None,
parent=None,
*,
end_lineno=None,
end_col_offset=None,
):
"""
:param name: The name of the class.
:type name: str or None
:param doc: The class docstring.
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
:param end_lineno: The last line this node appears on in the source code.
:type end_lineno: Optional[int]
:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
:type end_col_offset: Optional[int]
"""
self.instance_attrs = {}
self.locals = {}
"""A map of the name of a local variable to the node defining it."""
self.keywords = []
"""The keywords given to the class definition.
This is usually for :pep:`3115` style metaclass declaration.
:type: list(Keyword) or None
"""
self.bases: list[NodeNG] = []
"""What the class inherits from."""
self.body = []
"""The contents of the class body.
:type: list(NodeNG)
"""
self.name = name
"""The name of the class.
:type name: str or None
"""
self._doc = doc
"""The class docstring."""
self.doc_node: Const | None = None
"""The doc node associated with this node."""
self.is_dataclass: bool = False
"""Whether this class is a dataclass."""
super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
if parent is not None:
parent.frame(future=True).set_local(name, self)
for local_name, node in self.implicit_locals():
self.add_local_node(node, local_name)
infer_binary_op: ClassVar[InferBinaryOp[ClassDef]]
@property
def doc(self) -> str | None:
"""The class docstring."""
warnings.warn(
"The 'ClassDef.doc' attribute is deprecated, "
"use 'ClassDef.doc_node' instead.",
DeprecationWarning,
stacklevel=2,
)
return self._doc
@doc.setter
def doc(self, value: str | None) -> None:
warnings.warn(
"Setting the 'ClassDef.doc' attribute is deprecated, "
"use 'ClassDef.doc_node.value' instead.",
DeprecationWarning,
stacklevel=2,
)
self._doc = value
def implicit_parameters(self) -> Literal[1]:
return 1
def implicit_locals(self):
"""Get implicitly defined class definition locals.
:returns: the the name and Const pair for each local
:rtype: tuple(tuple(str, node_classes.Const), ...)
"""
locals_ = (("__module__", self.special_attributes.attr___module__),)
# __qualname__ is defined in PEP3155
locals_ += (("__qualname__", self.special_attributes.attr___qualname__),)
return locals_
# pylint: disable=redefined-outer-name
def postinit(
self,
bases,
body,
decorators,
newstyle=None,
metaclass: NodeNG | None = None,
keywords=None,
*,
position: Position | None = None,
doc_node: Const | None = None,
):
"""Do some setup after initialisation.
:param bases: What the class inherits from.
:type bases: list(NodeNG)
:param body: The contents of the class body.
:type body: list(NodeNG)
:param decorators: The decorators that are applied to this class.
:type decorators: Decorators or None
:param newstyle: Whether this is a new style class or not.
:type newstyle: bool or None
:param metaclass: The metaclass of this class.
:param keywords: The keywords given to the class definition.
:type keywords: list(Keyword) or None
:param position: Position of class keyword and name.
:param doc_node: The doc node associated with this node.
"""
if keywords is not None:
self.keywords = keywords
self.bases = bases
self.body = body
self.decorators = decorators
if newstyle is not None:
self._newstyle = newstyle
if metaclass is not None:
self._metaclass = metaclass
self.position = position
self.doc_node = doc_node
if doc_node:
self._doc = doc_node.value
def _newstyle_impl(self, context: InferenceContext | None = None):
if context is None:
context = InferenceContext()
if self._newstyle is not None:
return self._newstyle
for base in self.ancestors(recurs=False, context=context):
if base._newstyle_impl(context):
self._newstyle = True
break
klass = self.declared_metaclass()
# could be any callable, we'd need to infer the result of klass(name,
# bases, dict). punt if it's not a class node.
if klass is not None and isinstance(klass, ClassDef):
self._newstyle = klass._newstyle_impl(context)
if self._newstyle is None:
self._newstyle = False
return self._newstyle
_newstyle = None
newstyle = property(
_newstyle_impl,
doc=("Whether this is a new style class or not\n\n" ":type: bool or None"),
)
@cached_property
def fromlineno(self) -> int | None:
"""The first line that this node appears on in the source code."""
if not PY38_PLUS or IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
# For Python < 3.8 the lineno is the line number of the first decorator.
# We want the class statement lineno. Similar to 'FunctionDef.fromlineno'
# PyPy (3.8): Fixed with version v7.3.11
lineno = self.lineno
if self.decorators is not None:
lineno += sum(
node.tolineno - node.lineno + 1 for node in self.decorators.nodes
)
return lineno
return super().fromlineno
@cached_property
def blockstart_tolineno(self):
"""The line on which the beginning of this block ends.
:type: int
"""
if self.bases:
return self.bases[-1].tolineno
return self.fromlineno
def block_range(self, lineno):
"""Get a range from the given line number to where this node ends.
:param lineno: Unused.
:type lineno: int
:returns: The range of line numbers that this node belongs to,
:rtype: tuple(int, int)
"""
return self.fromlineno, self.tolineno
def pytype(self) -> Literal["builtins.type", "builtins.classobj"]:
"""Get the name of the type that this node represents.
:returns: The name of the type.
"""
if self.newstyle:
return "builtins.type"
return "builtins.classobj"
def display_type(self) -> str:
"""A human readable type of this node.
:returns: The type of this node.
:rtype: str
"""
return "Class"
def callable(self) -> bool:
"""Whether this node defines something that is callable.
:returns: Whether this defines something that is callable.
For a :class:`ClassDef` this is always ``True``.
"""
return True
def is_subtype_of(self, type_name, context: InferenceContext | None = None) -> bool:
"""Whether this class is a subtype of the given type.
:param type_name: The name of the type of check against.
:type type_name: str
:returns: Whether this class is a subtype of the given type.
"""
if self.qname() == type_name:
return True
return any(anc.qname() == type_name for anc in self.ancestors(context=context))
def _infer_type_call(self, caller, context):
try:
name_node = next(caller.args[0].infer(context))
except StopIteration as e:
raise InferenceError(node=caller.args[0], context=context) from e
if isinstance(name_node, node_classes.Const) and isinstance(
name_node.value, str
):
name = name_node.value
else:
return util.Uninferable
result = ClassDef(name)
# Get the bases of the class.
try:
class_bases = next(caller.args[1].infer(context))
except StopIteration as e:
raise InferenceError(node=caller.args[1], context=context) from e
if isinstance(class_bases, (node_classes.Tuple, node_classes.List)):
bases = []
for base in class_bases.itered():
inferred = next(base.infer(context=context), None)
if inferred:
bases.append(
node_classes.EvaluatedObject(original=base, value=inferred)
)
result.bases = bases
else:
# There is currently no AST node that can represent an 'unknown'
# node (Uninferable is not an AST node), therefore we simply return Uninferable here
# although we know at least the name of the class.
return util.Uninferable
# Get the members of the class
try:
members = next(caller.args[2].infer(context))
except (InferenceError, StopIteration):
members = None
if members and isinstance(members, node_classes.Dict):
for attr, value in members.items:
if isinstance(attr, node_classes.Const) and isinstance(attr.value, str):
result.locals[attr.value] = [value]
result.parent = caller.parent
return result
def infer_call_result(self, caller, context: InferenceContext | None = None):
"""infer what a class is returning when called"""
if self.is_subtype_of("builtins.type", context) and len(caller.args) == 3:
result = self._infer_type_call(caller, context)
yield result
return
dunder_call = None
try:
metaclass = self.metaclass(context=context)
if metaclass is not None:
# Only get __call__ if it's defined locally for the metaclass.
# Otherwise we will find ObjectModel.__call__ which will
# return an instance of the metaclass. Instantiating the class is
# handled later.
if "__call__" in metaclass.locals:
dunder_call = next(metaclass.igetattr("__call__", context))
except (AttributeInferenceError, StopIteration):
pass
if dunder_call and dunder_call.qname() != "builtins.type.__call__":
# Call type.__call__ if not set metaclass
# (since type is the default metaclass)
context = bind_context_to_node(context, self)
context.callcontext.callee = dunder_call
yield from dunder_call.infer_call_result(caller, context)
else:
yield self.instantiate_class()
def scope_lookup(self, node, name, offset=0):
"""Lookup where the given name is assigned.
:param node: The node to look for assignments up to.
Any assignments after the given node are ignored.
:type node: NodeNG
:param name: The name to find assignments for.
:type name: str
:param offset: The line offset to filter statements up to.
:type offset: int
:returns: This scope node and the list of assignments associated to the
given name according to the scope where it has been found (locals,
globals or builtin).
:rtype: tuple(str, list(NodeNG))
"""
# If the name looks like a builtin name, just try to look
# into the upper scope of this class. We might have a
# decorator that it's poorly named after a builtin object
# inside this class.
lookup_upper_frame = (
isinstance(node.parent, node_classes.Decorators)
and name in AstroidManager().builtins_module
)
if (
any(node == base or base.parent_of(node) for base in self.bases)
or lookup_upper_frame
):
# Handle the case where we have either a name
# in the bases of a class, which exists before
# the actual definition or the case where we have
# a Getattr node, with that name.
#
# name = ...
# class A(name):
# def name(self): ...
#
# import name
# class A(name.Name):
# def name(self): ...
frame = self.parent.frame(future=True)
# line offset to avoid that class A(A) resolve the ancestor to
# the defined class
offset = -1
else:
frame = self
return frame._scope_lookup(node, name, offset)
@property
def basenames(self):
"""The names of the parent classes
Names are given in the order they appear in the class definition.
:type: list(str)
"""
return [bnode.as_string() for bnode in self.bases]
def ancestors(
self, recurs: bool = True, context: InferenceContext | None = None
) -> Generator[ClassDef, None, None]:
"""Iterate over the base classes in prefixed depth first order.
:param recurs: Whether to recurse or return direct ancestors only.
:returns: The base classes
"""
# FIXME: should be possible to choose the resolution order
# FIXME: inference make infinite loops possible here
yielded = {self}
if context is None:
context = InferenceContext()
if not self.bases and self.qname() != "builtins.object":
yield builtin_lookup("object")[1][0]
return
for stmt in self.bases:
with context.restore_path():
try:
for baseobj in stmt.infer(context):
if not isinstance(baseobj, ClassDef):
if isinstance(baseobj, bases.Instance):
baseobj = baseobj._proxied
else:
continue
if not baseobj.hide:
if baseobj in yielded:
continue
yielded.add(baseobj)
yield baseobj
if not recurs:
continue
for grandpa in baseobj.ancestors(recurs=True, context=context):
if grandpa is self:
# This class is the ancestor of itself.
break
if grandpa in yielded:
continue
yielded.add(grandpa)
yield grandpa
except InferenceError:
continue
def local_attr_ancestors(self, name, context: InferenceContext | None = None):
"""Iterate over the parents that define the given name.
:param name: The name to find definitions for.
:type name: str
:returns: The parents that define the given name.
:rtype: iterable(NodeNG)
"""
# Look up in the mro if we can. This will result in the
# attribute being looked up just as Python does it.
try:
ancestors = self.mro(context)[1:]
except MroError:
# Fallback to use ancestors, we can't determine
# a sane MRO.
ancestors = self.ancestors(context=context)
for astroid in ancestors:
if name in astroid:
yield astroid
def instance_attr_ancestors(self, name, context: InferenceContext | None = None):
"""Iterate over the parents that define the given name as an attribute.
:param name: The name to find definitions for.
:type name: str
:returns: The parents that define the given name as
an instance attribute.
:rtype: iterable(NodeNG)
"""
for astroid in self.ancestors(context=context):
if name in astroid.instance_attrs:
yield astroid
def has_base(self, node) -> bool:
"""Whether this class directly inherits from the given node.
:param node: The node to check for.
:type node: NodeNG
:returns: Whether this class directly inherits from the given node.
"""
return node in self.bases
def local_attr(self, name, context: InferenceContext | None = None):
"""Get the list of assign nodes associated to the given name.
Assignments are looked for in both this class and in parents.
:returns: The list of assignments to the given name.
:rtype: list(NodeNG)
:raises AttributeInferenceError: If no attribute with this name
can be found in this class or parent classes.
"""
result = []
if name in self.locals:
result = self.locals[name]
else:
class_node = next(self.local_attr_ancestors(name, context), None)
if class_node:
result = class_node.locals[name]
result = [n for n in result if not isinstance(n, node_classes.DelAttr)]
if result:
return result
raise AttributeInferenceError(target=self, attribute=name, context=context)
def instance_attr(self, name, context: InferenceContext | None = None):
"""Get the list of nodes associated to the given attribute name.
Assignments are looked for in both this class and in parents.
:returns: The list of assignments to the given name.
:rtype: list(NodeNG)
:raises AttributeInferenceError: If no attribute with this name
can be found in this class or parent classes.
"""
# Return a copy, so we don't modify self.instance_attrs,
# which could lead to infinite loop.
values = list(self.instance_attrs.get(name, []))
# get all values from parents
for class_node in self.instance_attr_ancestors(name, context):
values += class_node.instance_attrs[name]
values = [n for n in values if not isinstance(n, node_classes.DelAttr)]
if values:
return values
raise AttributeInferenceError(target=self, attribute=name, context=context)
def instantiate_class(self) -> bases.Instance:
"""Get an :class:`Instance` of the :class:`ClassDef` node.
:returns: An :class:`Instance` of the :class:`ClassDef` node
"""
try:
if any(cls.name in EXCEPTION_BASE_CLASSES for cls in self.mro()):
# Subclasses of exceptions can be exception instances
return objects.ExceptionInstance(self)
except MroError:
pass
return bases.Instance(self)
def getattr(
self,
name: str,
context: InferenceContext | None = None,
class_context: bool = True,
) -> list[SuccessfulInferenceResult]:
"""Get an attribute from this class, using Python's attribute semantic.
This method doesn't look in the :attr:`instance_attrs` dictionary
since it is done by an :class:`Instance` proxy at inference time.
It may return an :class:`Uninferable` object if
the attribute has not been
found, but a ``__getattr__`` or ``__getattribute__`` method is defined.
If ``class_context`` is given, then it is considered that the
attribute is accessed from a class context,
e.g. ClassDef.attribute, otherwise it might have been accessed
from an instance as well. If ``class_context`` is used in that
case, then a lookup in the implicit metaclass and the explicit
metaclass will be done.
:param name: The attribute to look for.
:param class_context: Whether the attribute can be accessed statically.
:returns: The attribute.
:raises AttributeInferenceError: If the attribute cannot be inferred.
"""
if not name:
raise AttributeInferenceError(target=self, attribute=name, context=context)
# don't modify the list in self.locals!
values: list[SuccessfulInferenceResult] = list(self.locals.get(name, []))
for classnode in self.ancestors(recurs=True, context=context):
values += classnode.locals.get(name, [])
if name in self.special_attributes and class_context and not values:
result = [self.special_attributes.lookup(name)]
if name == "__bases__":
# Need special treatment, since they are mutable
# and we need to return all the values.
result += values
return result
if class_context:
values += self._metaclass_lookup_attribute(name, context)
# Remove AnnAssigns without value, which are not attributes in the purest sense.
for value in values.copy():
if isinstance(value, node_classes.AssignName):
stmt = value.statement(future=True)
if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None:
values.pop(values.index(value))
if not values:
raise AttributeInferenceError(target=self, attribute=name, context=context)
return values
@lru_cache(maxsize=1024) # noqa
def _metaclass_lookup_attribute(self, name, context):
"""Search the given name in the implicit and the explicit metaclass."""
attrs = set()
implicit_meta = self.implicit_metaclass()
context = copy_context(context)
metaclass = self.metaclass(context=context)
for cls in (implicit_meta, metaclass):
if cls and cls != self and isinstance(cls, ClassDef):
cls_attributes = self._get_attribute_from_metaclass(cls, name, context)
attrs.update(set(cls_attributes))
return attrs
def _get_attribute_from_metaclass(self, cls, name, context):
try:
attrs = cls.getattr(name, context=context, class_context=True)
except AttributeInferenceError:
return
for attr in bases._infer_stmts(attrs, context, frame=cls):
if not isinstance(attr, FunctionDef):
yield attr
continue
if isinstance(attr, objects.Property):
yield attr
continue
if attr.type == "classmethod":
# If the method is a classmethod, then it will
# be bound to the metaclass, not to the class
# from where the attribute is retrieved.
# get_wrapping_class could return None, so just
# default to the current class.
frame = get_wrapping_class(attr) or self
yield bases.BoundMethod(attr, frame)
elif attr.type == "staticmethod":
yield attr
else:
yield bases.BoundMethod(attr, self)
def igetattr(
self,
name: str,
context: InferenceContext | None = None,
class_context: bool = True,
) -> Iterator[InferenceResult]:
"""Infer the possible values of the given variable.
:param name: The name of the variable to infer.
:returns: The inferred possible values.
"""
# set lookup name since this is necessary to infer on import nodes for
# instance
context = copy_context(context)
context.lookupname = name
metaclass = self.metaclass(context=context)
try:
attributes = self.getattr(name, context, class_context=class_context)
# If we have more than one attribute, make sure that those starting from
# the second one are from the same scope. This is to account for modifications
# to the attribute happening *after* the attribute's definition (e.g. AugAssigns on lists)
if len(attributes) > 1:
first_attr, attributes = attributes[0], attributes[1:]
first_scope = first_attr.scope()
attributes = [first_attr] + [
attr
for attr in attributes
if attr.parent and attr.parent.scope() == first_scope
]
for inferred in bases._infer_stmts(attributes, context, frame=self):
# yield Uninferable object instead of descriptors when necessary
if not isinstance(inferred, node_classes.Const) and isinstance(
inferred, bases.Instance
):
try:
inferred._proxied.getattr("__get__", context)
except AttributeInferenceError:
yield inferred
else:
yield util.Uninferable
elif isinstance(inferred, objects.Property):
function = inferred.function
if not class_context:
# Through an instance so we can solve the property
yield from function.infer_call_result(
caller=self, context=context
)
# If we're in a class context, we need to determine if the property
# was defined in the metaclass (a derived class must be a subclass of
# the metaclass of all its bases), in which case we can resolve the
# property. If not, i.e. the property is defined in some base class
# instead, then we return the property object
elif metaclass and function.parent.scope() is metaclass:
# Resolve a property as long as it is not accessed through
# the class itself.
yield from function.infer_call_result(
caller=self, context=context
)
else:
yield inferred
else:
yield function_to_method(inferred, self)
except AttributeInferenceError as error:
if not name.startswith("__") and self.has_dynamic_getattr(context):
# class handle some dynamic attributes, return a Uninferable object
yield util.Uninferable
else:
raise InferenceError(
str(error), target=self, attribute=name, context=context
) from error
def has_dynamic_getattr(self, context: InferenceContext | None = None) -> bool:
"""Check if the class has a custom __getattr__ or __getattribute__.
If any such method is found and it is not from
builtins, nor from an extension module, then the function
will return True.
:returns: Whether the class has a custom __getattr__ or __getattribute__.
"""
def _valid_getattr(node):
root = node.root()
return root.name != "builtins" and getattr(root, "pure_python", None)
try:
return _valid_getattr(self.getattr("__getattr__", context)[0])
except AttributeInferenceError:
# if self.newstyle: XXX cause an infinite recursion error
try:
getattribute = self.getattr("__getattribute__", context)[0]
return _valid_getattr(getattribute)
except AttributeInferenceError:
pass
return False
def getitem(self, index, context: InferenceContext | None = None):
"""Return the inference of a subscript.
This is basically looking up the method in the metaclass and calling it.
:returns: The inferred value of a subscript to this class.
:rtype: NodeNG
:raises AstroidTypeError: If this class does not define a
``__getitem__`` method.
"""
try:
methods = lookup(self, "__getitem__")
except AttributeInferenceError as exc:
if isinstance(self, ClassDef):
# subscripting a class definition may be
# achieved thanks to __class_getitem__ method
# which is a classmethod defined in the class
# that supports subscript and not in the metaclass
try:
methods = self.getattr("__class_getitem__")
# Here it is assumed that the __class_getitem__ node is
# a FunctionDef. One possible improvement would be to deal
# with more generic inference.
except AttributeInferenceError:
raise AstroidTypeError(node=self, context=context) from exc
else:
raise AstroidTypeError(node=self, context=context) from exc
method = methods[0]
# Create a new callcontext for providing index as an argument.
new_context = bind_context_to_node(context, self)
new_context.callcontext = CallContext(args=[index], callee=method)
try:
return next(method.infer_call_result(self, new_context), util.Uninferable)
except AttributeError:
# Starting with python3.9, builtin types list, dict etc...
# are subscriptable thanks to __class_getitem___ classmethod.
# However in such case the method is bound to an EmptyNode and
# EmptyNode doesn't have infer_call_result method yielding to
# AttributeError
if (
isinstance(method, node_classes.EmptyNode)
and self.pytype() == "builtins.type"
and PY39_PLUS
):
return self
raise
except InferenceError:
return util.Uninferable
def methods(self):
"""Iterate over all of the method defined in this class and its parents.
:returns: The methods defined on the class.
:rtype: iterable(FunctionDef)
"""
done = {}
for astroid in itertools.chain(iter((self,)), self.ancestors()):
for meth in astroid.mymethods():
if meth.name in done:
continue
done[meth.name] = None
yield meth
def mymethods(self):
"""Iterate over all of the method defined in this class only.
:returns: The methods defined on the class.
:rtype: iterable(FunctionDef)
"""
for member in self.values():
if isinstance(member, FunctionDef):
yield member
def implicit_metaclass(self):
"""Get the implicit metaclass of the current class.
For newstyle classes, this will return an instance of builtins.type.
For oldstyle classes, it will simply return None, since there's
no implicit metaclass there.
:returns: The metaclass.
:rtype: builtins.type or None
"""
if self.newstyle:
return builtin_lookup("type")[1][0]
return None
def declared_metaclass(
self, context: InferenceContext | None = None
) -> NodeNG | None:
"""Return the explicit declared metaclass for the current class.
An explicit declared metaclass is defined
either by passing the ``metaclass`` keyword argument
in the class definition line (Python 3) or (Python 2) by
having a ``__metaclass__`` class attribute, or if there are
no explicit bases but there is a global ``__metaclass__`` variable.
:returns: The metaclass of this class,
or None if one could not be found.
"""
for base in self.bases:
try:
for baseobj in base.infer(context=context):
if isinstance(baseobj, ClassDef) and baseobj.hide:
self._metaclass = baseobj._metaclass
self._metaclass_hack = True
break
except InferenceError:
pass
if self._metaclass:
# Expects this from Py3k TreeRebuilder
try:
return next(
node
for node in self._metaclass.infer(context=context)
if not isinstance(node, util.UninferableBase)
)
except (InferenceError, StopIteration):
return None
return None
def _find_metaclass(
self, seen: set[ClassDef] | None = None, context: InferenceContext | None = None
) -> NodeNG | None:
if seen is None:
seen = set()
seen.add(self)
klass = self.declared_metaclass(context=context)
if klass is None:
for parent in self.ancestors(context=context):
if parent not in seen:
klass = parent._find_metaclass(seen)
if klass is not None:
break
return klass
def metaclass(self, context: InferenceContext | None = None) -> NodeNG | None:
"""Get the metaclass of this class.
If this class does not define explicitly a metaclass,
then the first defined metaclass in ancestors will be used
instead.
:returns: The metaclass of this class.
"""
return self._find_metaclass(context=context)
def has_metaclass_hack(self):
return self._metaclass_hack
def _islots(self):
"""Return an iterator with the inferred slots."""
if "__slots__" not in self.locals:
return None
for slots in self.igetattr("__slots__"):
# check if __slots__ is a valid type
for meth in ITER_METHODS:
try:
slots.getattr(meth)
break
except AttributeInferenceError:
continue
else:
continue
if isinstance(slots, node_classes.Const):
# a string. Ignore the following checks,
# but yield the node, only if it has a value
if slots.value:
yield slots
continue
if not hasattr(slots, "itered"):
# we can't obtain the values, maybe a .deque?
continue
if isinstance(slots, node_classes.Dict):
values = [item[0] for item in slots.items]
else:
values = slots.itered()
if isinstance(values, util.UninferableBase):
continue
if not values:
# Stop the iteration, because the class
# has an empty list of slots.
return values
for elt in values:
try:
for inferred in elt.infer():
if not isinstance(
inferred, node_classes.Const
) or not isinstance(inferred.value, str):
continue
if not inferred.value:
continue
yield inferred
except InferenceError:
continue
return None
def _slots(self):
if not self.newstyle:
raise NotImplementedError(
"The concept of slots is undefined for old-style classes."
)
slots = self._islots()
try:
first = next(slots)
except StopIteration as exc:
# The class doesn't have a __slots__ definition or empty slots.
if exc.args and exc.args[0] not in ("", None):
return exc.args[0]
return None
return [first, *slots]
# Cached, because inferring them all the time is expensive
@cached_property
def _all_slots(self):
"""Get all the slots for this node.
:returns: The names of slots for this class.
If the class doesn't define any slot, through the ``__slots__``
variable, then this function will return a None.
Also, it will return None in the case the slots were not inferred.
:rtype: list(str) or None
"""
def grouped_slots(
mro: list[ClassDef],
) -> Iterator[node_classes.NodeNG | None]:
for cls in mro:
# Not interested in object, since it can't have slots.
if cls.qname() == "builtins.object":
continue
try:
cls_slots = cls._slots()
except NotImplementedError:
continue
if cls_slots is not None:
yield from cls_slots
else:
yield None
if not self.newstyle:
raise NotImplementedError(
"The concept of slots is undefined for old-style classes."
)
try:
mro = self.mro()
except MroError as e:
raise NotImplementedError(
"Cannot get slots while parsing mro fails."
) from e
slots = list(grouped_slots(mro))
if not all(slot is not None for slot in slots):
return None
return sorted(set(slots), key=lambda item: item.value)
def slots(self):
return self._all_slots
def _inferred_bases(self, context: InferenceContext | None = None):
# Similar with .ancestors, but the difference is when one base is inferred,
# only the first object is wanted. That's because
# we aren't interested in superclasses, as in the following
# example:
#
# class SomeSuperClass(object): pass
# class SomeClass(SomeSuperClass): pass
# class Test(SomeClass): pass
#
# Inferring SomeClass from the Test's bases will give
# us both SomeClass and SomeSuperClass, but we are interested
# only in SomeClass.
if context is None:
context = InferenceContext()
if not self.bases and self.qname() != "builtins.object":
yield builtin_lookup("object")[1][0]
return
for stmt in self.bases:
try:
# Find the first non-None inferred base value
baseobj = next(
b
for b in stmt.infer(context=context.clone())
if not (isinstance(b, Const) and b.value is None)
)
except (InferenceError, StopIteration):
continue
if isinstance(baseobj, bases.Instance):
baseobj = baseobj._proxied
if not isinstance(baseobj, ClassDef):
continue
if not baseobj.hide:
yield baseobj
else:
yield from baseobj.bases
def _compute_mro(self, context: InferenceContext | None = None):
inferred_bases = list(self._inferred_bases(context=context))
bases_mro = []
for base in inferred_bases:
if base is self:
continue
try:
mro = base._compute_mro(context=context)
bases_mro.append(mro)
except NotImplementedError:
# Some classes have in their ancestors both newstyle and
# old style classes. For these we can't retrieve the .mro,
# although in Python it's possible, since the class we are
# currently working is in fact new style.
# So, we fallback to ancestors here.
ancestors = list(base.ancestors(context=context))
bases_mro.append(ancestors)
unmerged_mro = [[self], *bases_mro, inferred_bases]
unmerged_mro = list(clean_duplicates_mro(unmerged_mro, self, context))
clean_typing_generic_mro(unmerged_mro)
return _c3_merge(unmerged_mro, self, context)
def mro(self, context: InferenceContext | None = None) -> list[ClassDef]:
"""Get the method resolution order, using C3 linearization.
:returns: The list of ancestors, sorted by the mro.
:rtype: list(NodeNG)
:raises DuplicateBasesError: Duplicate bases in the same class base
:raises InconsistentMroError: A class' MRO is inconsistent
"""
return self._compute_mro(context=context)
def bool_value(self, context: InferenceContext | None = None) -> Literal[True]:
"""Determine the boolean value of this node.
:returns: The boolean value of this node.
For a :class:`ClassDef` this is always ``True``.
"""
return True
def get_children(self):
if self.decorators is not None:
yield self.decorators
yield from self.bases
if self.keywords is not None:
yield from self.keywords
yield from self.body
@cached_property
def _assign_nodes_in_scope(self):
children_assign_nodes = (
child_node._assign_nodes_in_scope for child_node in self.body
)
return list(itertools.chain.from_iterable(children_assign_nodes))
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
"""The node's frame node.
A frame node is a :class:`Module`, :class:`FunctionDef`,
:class:`ClassDef` or :class:`Lambda`.
:returns: The node itself.
"""
return self
|