File size: 112,183 Bytes
c1f3d83 6faed22 c1f3d83 6faed22 c1f3d83 6faed22 c1f3d83 6faed22 c1f3d83 6faed22 c1f3d83 6faed22 c1f3d83 c3d4547 d0bca75 c3d4547 054a97a b999a8e c3d4547 6d991e1 66c7faa 03128a2 177f1ca 0539253 a8f9476 0539253 03128a2 054a97a a8f9476 054a97a 6d991e1 3e7a0a4 6faed22 223d4f4 7dbbe3a 223d4f4 210b09e 223d4f4 210b09e 6eb635c 77aa4e0 fed0255 210b09e fb9624b 210b09e c298898 666f897 a9e151c 575ff59 ae568da e9d9bcb d43fe3b 847fcae 82013bf | 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 | """The AST checkers, and the fragment gate that decides when to trust them.
Each test names the defect it guards. Every one of them fails against the code
as it stood before the session that wrote them.
"""
import inspect
import pytest
import corpus
import semantics
# ------------------------------------------------- prefer_sql_constraint_applies
UNIQUENESS_IN_PYTHON = """@api.constrains('code')
def _check_unique_code(self):
for rec in self:
if self.search_count([('code', '=', rec.code), ('id', '!=', rec.id)]):
raise ValidationError('Code must be unique')"""
SQL_CONSTRAINT = """_sql_constraints = [
('code_uniq', 'UNIQUE(code)', 'Code must be unique.'),
]"""
LITERAL_BOUND = """@api.constrains('product_uom_qty')
def _check_quantity(self):
for rec in self:
if rec.product_uom_qty < 0:
raise ValidationError('Quantity cannot be negative')"""
CROSS_TABLE = """@api.constrains('product_id', 'product_uom_qty', 'company_id')
def _check_available_qty(self):
for rec in self:
if not rec.product_id or rec.product_uom_qty <= 0:
continue
available = rec.product_id.with_company(rec.company_id).qty_available
if rec.product_uom_qty > available:
raise ValidationError('too much')"""
TWO_COLUMN_COMPARISON = """@api.constrains('date_start', 'date_end')
def _check_dates(self):
for rec in self:
if rec.date_start and rec.date_end and rec.date_end < rec.date_start:
raise ValidationError('End before start')"""
FLOAT_UTILS = """@api.constrains('selling_price')
def _check_price(self):
precision = self.env['decimal.precision'].precision_get('Product Price')
for rec in self:
if float_is_zero(rec.selling_price, precision_digits=precision):
raise ValidationError('Price cannot be zero')"""
def fires(code):
return bool(semantics.prefer_sql_constraint_applies(code))
def test_uniqueness_in_python_is_caught():
"""The rule's own 'wrong' sample. validate fails the build without this."""
assert fires(UNIQUENESS_IN_PYTHON)
def test_sql_constraint_is_silent():
"""The rule's own 'correct' sample. A checker that fires here cries wolf."""
assert not fires(SQL_CONSTRAINT)
def test_literal_bound_is_caught():
"""The gap that started it all.
The predecessor regex matched only `@api.constrains ... search_count`, so a
single-column bound -- the commonest case the rule governs -- passed twelve
domain checks in silence.
"""
assert fires(LITERAL_BOUND)
def test_decorator_call_does_not_disqualify_the_function():
"""@api.constrains(...) is itself an ast.Call.
Walking the whole function node counts it among the body's calls, and since
any call disqualifies a candidate, every constraint disqualified itself. The
checker looked correct and caught nothing.
"""
assert fires(LITERAL_BOUND)
assert semantics.prefer_sql_constraint_applies(LITERAL_BOUND)[0][0] == 4
def test_cross_table_access_is_silent():
"""A CHECK genuinely cannot read another table, so this must not fire.
Verified against real 30B output, not an invented sample: the reach through
rec.product_id.with_company(...) is what puts it out of scope.
"""
assert not fires(CROSS_TABLE)
def test_two_column_comparison_is_silent():
"""A declared limit, asserted so it cannot be lost by accident.
CHECK(date_end >= date_start) is valid SQL, so this is recall deliberately
traded for precision on a `high` rule. If someone later widens the checker,
this test should fail and make them say so on purpose.
"""
assert not fires(TWO_COLUMN_COMPARISON)
def test_helper_call_in_the_condition_is_silent():
assert not fires(FLOAT_UTILS)
def test_message_only_field_read_does_not_block_a_finding():
"""rec.display_name is read to phrase the error, not to decide the rule."""
code = """@api.constrains('qty')
def _check_qty(self):
for rec in self:
if rec.qty <= 0:
raise ValidationError('Bad qty on %s' % rec.display_name)"""
assert fires(code)
def test_unparseable_code_yields_no_findings():
assert semantics.prefer_sql_constraint_applies("def broken(:") == []
# ---------------------------------------------------------- is_lifted_fragment
def test_method_taking_self_is_a_lifted_fragment():
"""A method shown without its class was never going to carry its imports.
Flagging them turned a correct answer into WARN and buried the real finding
under two that could never have been otherwise.
"""
tree, _ = semantics.parse_python(LITERAL_BOUND)
assert semantics.is_lifted_fragment(tree, [])
def test_real_module_is_not_a_fragment():
"""The gate must not disable the import check wholesale."""
code = """from odoo import api
def helper(x):
return ValidationError(x)"""
tree, _ = semantics.parse_python(code)
assert not semantics.is_lifted_fragment(tree, [])
assert [m for _, m in semantics.missing_imports(tree)]
def test_repair_notes_alone_mark_a_fragment():
tree, _ = semantics.parse_python("x = 1")
assert semantics.is_lifted_fragment(tree, ["the block only parses as a class body"])
# ------------------------------------------------------------ the other checks
def test_constrains_missing_field_is_caught():
code = """@api.constrains('date_start')
def _check_dates(self):
for rec in self:
if rec.date_end < rec.date_start:
raise ValidationError('End before start')"""
hits = semantics.constrains_lists_every_field_read(code)
assert any("date_end" in m for _, m in hits)
def test_constrains_listing_every_field_is_silent():
assert semantics.constrains_lists_every_field_read(TWO_COLUMN_COMPARISON) == []
def test_depends_missing_dotted_path_is_caught():
code = """@api.depends('line_ids')
def _compute_total(self):
for rec in self:
rec.total = sum(rec.line_ids.price)"""
hits = semantics.depends_covers_compute_reads(code)
assert any("line_ids.price" in m for _, m in hits)
@pytest.mark.parametrize("name", sorted(semantics.CHECKS))
def test_every_registered_checker_is_callable_and_returns_triples(name):
"""run_check raises on an unknown name rather than passing silently.
A renamed checker that returned 'clean' would turn a critical rule into a
permanent false pass, so the registry is part of the contract.
The third element is the likelihood a finding earned from a trigger, or None
to keep its rule's grade. run_check normalises it, so a checker with no
opinion about triggers still answers in the one shape every caller unpacks.
"""
out = semantics.run_check(name, LITERAL_BOUND)
assert isinstance(out, list)
for item in out:
assert len(item) == 3
assert isinstance(item[0], int) and isinstance(item[1], str)
assert item[2] is None or item[2] in corpus.LIKELIHOOD_ORDER
@pytest.mark.parametrize("name", sorted(semantics.CONTEXT_CHECKS))
def test_a_context_checker_is_registered_and_takes_facts(name):
"""CONTEXT_CHECKS is a hand-written set, so it can name a checker that no
longer takes facts -- which would pass the tree to a positional argument
meaning something else."""
assert name in semantics.CHECKS
argspec = inspect.getfullargspec(semantics.CHECKS[name])
assert argspec.args[:2] == ["code", "facts"]
def test_unknown_checker_name_raises():
with pytest.raises(KeyError):
semantics.run_check("no_such_checker", "x = 1")
# ------------------------------------------------- ondelete, read through
ONDELETE_READ = """class Thing(models.Model):
_name = "my.thing"
task_id = fields.Many2one("my.task")
def _label(self):
for rec in self:
rec.name = rec.task_id.filepath
"""
ONDELETE_DECLARED_ONLY = """class Thing(models.Model):
_name = "my.thing"
task_id = fields.Many2one("my.task")
"""
def test_a_dereferenced_many2one_without_ondelete_is_caught():
"""The shape where 'set null' silently changes an answer: delete the parent,
the reference is nulled, and rec.task_id.filepath becomes False."""
hits = semantics.ondelete_leaves_a_dangling_read(ONDELETE_READ)
assert hits, "no finding"
assert "filepath" in hits[0][1], "name the read that makes it matter"
assert hits[0][0] == 4, "the line has to point at the declaration"
def test_a_declaration_nobody_reads_through_is_silent():
"""Nulling a reference no code dereferences changes no answer. Flagging it
produced 4.1 findings per 1000 lines on reviewed OCA code."""
assert semantics.ondelete_leaves_a_dangling_read(ONDELETE_DECLARED_ONLY) == []
def test_required_many2one_is_exempt_from_ondelete():
"""Odoo defaults a required Many2one to 'restrict', which is what the rule
recommends, so the orphaning cannot happen there."""
code = ONDELETE_READ.replace('fields.Many2one("my.task")',
'fields.Many2one("my.task", required=True)')
assert semantics.ondelete_leaves_a_dangling_read(code) == []
def test_related_many2one_is_exempt_from_ondelete():
code = ONDELETE_READ.replace('fields.Many2one("my.task")',
'fields.Many2one("my.task", related="x.task_id")')
assert semantics.ondelete_leaves_a_dangling_read(code) == []
def test_a_declared_ondelete_is_silent():
code = ONDELETE_READ.replace('fields.Many2one("my.task")',
'fields.Many2one("my.task", ondelete="restrict")')
assert semantics.ondelete_leaves_a_dangling_read(code) == []
def test_reading_the_relation_itself_is_not_a_dereference():
"""`if rec.task_id:` is a recordset test and is falsy-safe. It is
`rec.task_id.filepath` that quietly becomes False."""
code = """class Thing(models.Model):
task_id = fields.Many2one("my.task")
def _label(self):
for rec in self:
if rec.task_id:
rec.name = "yes"
"""
assert semantics.ondelete_leaves_a_dangling_read(code) == []
def test_a_read_through_mapped_stays_unread():
"""Measured and not taken (ARCHITECTURE 3.79). mapped("task_id.filepath")
reads through the relation as the attribute does, but a read is placed by
the field's name, and a mapped() is often called on records of another
model: on the three trees it added one finding, a quant's location_id read
in stock.location's file, and nothing true."""
code = ONDELETE_READ.replace(
"rec.name = rec.task_id.filepath",
'rec.name = ",".join(self.mapped("task_id.filepath"))')
assert semantics.ondelete_leaves_a_dangling_read(code) == []
# ----------------------------------------------------- check_company scope
COMPANY_SCOPED = """class Thing(models.Model):
_name = "my.thing"
company_id = fields.Many2one("res.company", required=True)
journal_id = fields.Many2one("account.journal")
"""
def test_a_company_scoped_model_needs_check_company():
hits = semantics.check_company_missing_on_company_owned_relation(COMPANY_SCOPED)
assert hits, "no finding"
assert "account.journal" in hits[0][1]
def test_check_company_present_is_silent():
code = COMPANY_SCOPED.replace('fields.Many2one("account.journal")',
'fields.Many2one("account.journal", check_company=True)')
assert semantics.check_company_missing_on_company_owned_relation(code) == []
def test_a_model_with_no_company_is_not_asked_for_check_company():
"""check_company compares the record's company against the target's. On a
model that has no company_id there is nothing to compare, and the regex
that preceded this could not see the class at all."""
code = COMPANY_SCOPED.replace(
' company_id = fields.Many2one("res.company", required=True)\n', "")
assert semantics.check_company_missing_on_company_owned_relation(code) == []
def test_res_company_itself_is_exempt():
"""Odoo's own fields.py carries a branch for check_company on a field of
res.company. Six of the 37 OCA hits were this."""
code = COMPANY_SCOPED.replace('_name = "my.thing"', '_inherit = "res.company"')
assert semantics.check_company_missing_on_company_owned_relation(code) == []
def test_a_wizard_is_exempt():
"""A TransientModel's records do not persist, and the harm the rule names is
a multi-company client seeing another tenant's data on a DOCUMENT. Eleven of
the 37 OCA hits were wizards."""
code = COMPANY_SCOPED.replace("models.Model", "models.TransientModel")
assert semantics.check_company_missing_on_company_owned_relation(code) == []
def test_a_bare_declaration_block_is_still_checked():
"""A generated answer arrives with no class around it. Treating the module
as one implicit model body keeps the rule working on snippets, and costs
nothing on real code, where fields always live in a class."""
code = """company_id = fields.Many2one("res.company", required=True)
journal_id = fields.Many2one("account.journal")
"""
assert semantics.check_company_missing_on_company_owned_relation(code)
# --------------------------------------------- storedness, and underscores
def test_a_non_stored_compute_is_outside_the_rule():
"""The rule is about STORED computes -- "written once and never recomputed,
survives restarts". None of that happens to a value the database never
holds. 30 of 95 OCA findings were non-stored computes."""
code = """total = fields.Float(compute="_compute_total")
@api.depends('line_ids')
def _compute_total(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)
"""
assert semantics.depends_covers_compute_reads(code) == []
def test_a_stored_compute_is_still_checked():
code = """total = fields.Float(compute="_compute_total", store=True)
@api.depends('line_ids')
def _compute_total(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)
"""
hits = semantics.depends_covers_compute_reads(code)
assert any("line_ids.price" in m for _, m in hits)
def test_an_undeclared_compute_keeps_the_rules_assumption():
"""Most of this checker's work is on generated snippets, where no field
declaration is present at all. Absence is unknown, not unstored."""
code = """@api.depends('line_ids')
def _compute_total(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)
"""
assert semantics.depends_covers_compute_reads(code)
# A compute that fills its field through a helper, update() or write() makes
# no attribute store, so the field it produces used to read as a dependency.
# calendar.recurrence's _compute_rrule compares and write()s its own rrule;
# OpenSPP's _compute_center_area_ids clears and fills its own through update().
OWN_OUTPUT = """class Recurrence(models.Model):
rrule = fields.Char(compute="_compute_rrule", store=True)
rrule_type = fields.Selection([("daily", "Daily")])
label = fields.Char(compute="_compute_label", store=True)
@api.depends("rrule_type")
def _compute_rrule(self):
for recurrence in self:
current = recurrence._serialize()
if recurrence.rrule != current:
recurrence.write({"rrule": current})
"""
def test_a_field_the_file_says_the_method_computes_is_its_own_output():
assert semantics.run_check("depends_covers_compute_reads", OWN_OUTPUT) == []
def test_another_computed_field_or_the_same_one_through_a_relation_still_counts():
"""The exemption is the method's own output on the same record. A field
another method computes is an input, and rrule read off a parent is a
recursive dependency, which Odoo supports and wants declared."""
code = OWN_OUTPUT.replace(
"if recurrence.rrule != current:",
"if recurrence.rrule != current or recurrence.label or recurrence.parent_id.rrule:")
reads = sorted(message.split("'")[1] for _, message, _ in
semantics.run_check("depends_covers_compute_reads", code))
assert reads == ["label", "parent_id", "parent_id.rrule"], reads
def test_a_read_through_the_methods_own_output_stays_exempt():
"""Measured and kept (ARCHITECTURE 3.73). Odoo drops @api.depends("team_id")
from team_id's own triggers but keeps "team_id.member_ids", so this read
could be declared. With the exemption narrowed to the bare field, core
gained 37 findings: 17 true, 16 of them a smart default like this one,
checking its own earlier choice, and 20 false."""
code = """class Lead(models.Model):
team_id = fields.Many2one("crm.team", compute="_compute_team_id", store=True, readonly=False)
@api.depends("user_id")
def _compute_team_id(self):
for lead in self:
if lead.team_id and lead.user_id in lead.team_id.member_ids:
continue
lead.team_id = lead._default_team(lead.user_id)
"""
assert semantics.run_check("depends_covers_compute_reads", code) == []
def test_an_underscore_name_is_not_a_field():
"""Odoo reserves the underscore prefix for ORM internals and mixin
configuration. base_time_window reads self._time_window_overlap_check_field,
and the checker told its author to declare it in @api.constrains, where the
ORM ignores it."""
code = """@api.constrains('start', 'end')
def _check_window(self):
for rec in self:
field = rec._time_window_overlap_check_field
if rec.start > rec.end:
raise ValidationError('bad')
"""
hits = semantics.constrains_lists_every_field_read(code)
assert not any("_time_window" in m for _, m in hits)
# mapped("a.b") names a field without an attribute access ever appearing. The
# checker used to read only the receiver, so this compute looked declared and
# the same read spelled as a generator was reported.
MAPPED_COMPUTE = """@api.depends({declared})
def _compute_outstanding_total(self):
for account in self:
account.outstanding_total = sum(account.invoice_ids.{call})
"""
def test_a_mapped_path_is_a_read_the_depends_must_declare():
code = MAPPED_COMPUTE.format(declared='"invoice_ids"',
call='mapped("amount_residual")')
hits = semantics.run_check("depends_covers_compute_reads", code)
assert [(line, "'invoice_ids.amount_residual'" in message)
for line, message, _ in hits] == [(4, True)], hits
def test_a_mapped_path_the_depends_declares_is_quiet():
for declared in ('"invoice_ids.amount_residual"',
'"invoice_ids.amount_residual.currency_id"'):
code = MAPPED_COMPUTE.format(declared=declared,
call='mapped("amount_residual")')
assert semantics.run_check("depends_covers_compute_reads", code) == [], \
declared
def test_a_mapped_path_keeps_the_checkers_exemptions():
"""A trailing .id names the relation, and a filtered() receiver is still
the recordset it filters."""
code = MAPPED_COMPUTE.format(
declared='"invoice_ids.partner_id"',
call='filtered(lambda i: i.posted).mapped("partner_id.id")')
assert semantics.run_check("depends_covers_compute_reads", code) == []
def test_a_mapped_lambda_or_name_is_not_guessed():
for call in ("mapped(lambda i: i.amount_residual)", "mapped(FIELD)"):
code = MAPPED_COMPUTE.format(declared='"invoice_ids"', call=call)
assert semantics.run_check("depends_covers_compute_reads", code) == [], call
# mapped()'s siblings name what they read the same way, and were silent after
# it was not. Each call below reads the paths beside it, relative to
# invoice_ids; a trailing id names the relation and is never asked for.
NAMED_READS = [
('filtered("is_paid")', ["is_paid"]),
('filtered("partner_id.is_company")', ["partner_id.is_company"]),
('grouped("journal_id")', ["journal_id"]),
('sorted("sequence")', ["sequence"]),
('sorted(key="sequence", reverse=True)', ["sequence"]),
('sorted("date desc, id")', ["date"]),
('sorted("partner_id.name DESC NULLS LAST, date:month")', ["partner_id.name", "date"]),
('filtered_domain([("state", "=", "done"), "|", (1, "=", 1), '
'("partner_id.id", "=", 7)])', ["partner_id", "state"]),
('filtered_domain([("line_ids", "any", [("paid", "=", True)])])',
["line_ids", "line_ids.paid"]),
]
@pytest.mark.parametrize("call, reads", NAMED_READS)
def test_a_field_named_in_a_string_is_a_read_the_depends_must_declare(call, reads):
code = MAPPED_COMPUTE.format(declared='"invoice_ids"', call=call)
hits = semantics.run_check("depends_covers_compute_reads", code)
reported = sorted(message.split("'")[1] for _, message, _ in hits)
assert reported == sorted("invoice_ids." + read for read in reads), hits
@pytest.mark.parametrize("call, reads", NAMED_READS)
def test_a_field_named_in_a_string_the_depends_declares_is_quiet(call, reads):
declared = ", ".join('"invoice_ids.{0}"'.format(read) for read in reads)
code = MAPPED_COMPUTE.format(declared=declared, call=call)
assert semantics.run_check("depends_covers_compute_reads", code) == []
@pytest.mark.parametrize("call", [
"filtered(lambda i: i.is_paid)",
"sorted(key=lambda i: i.sequence)",
"sorted(ORDER)",
"grouped(KEY)",
"filtered_domain(DOMAIN)",
"filtered_domain(DOMAIN + [LEAF])",
'sorted("date desc limit 1")',
'filtered("line_ids[0]")',
])
def test_a_field_not_written_as_a_literal_is_not_guessed(call):
code = MAPPED_COMPUTE.format(declared='"invoice_ids"', call=call)
assert semantics.run_check("depends_covers_compute_reads", code) == [], call
NAMED_EXEMPT = """{head}
@api.depends("invoice_ids", "line_ids.partner_id.name")
def _compute_total(self):
for rec in self:
{body}
"""
@pytest.mark.parametrize("head, body", [
# declared as a longer path: line_ids.partner_id.name covers line_ids.partner_id
("class A(models.Model):", 'rec.total = rec.line_ids.filtered("partner_id")'),
# the field this method computes
("class A(models.Model):", 'rec.total = rec.filtered("total") and 1'),
# a non-stored compute
('class A(models.Model):\n total = fields.Float(compute="_compute_total")',
'rec.total = len(rec.invoice_ids.filtered("is_paid"))'),
# a wizard
("class A(models.TransientModel):",
'rec.total = len(rec.invoice_ids.filtered("is_paid"))'),
# phrasing a message
("class A(models.Model):",
'raise UserError(rec.invoice_ids.filtered("is_paid").mapped("name"))'),
("class A(models.Model):",
'_logger.info("%s", rec.invoice_ids.sorted("date")[:1].name)'),
])
def test_a_field_named_in_a_string_keeps_the_checkers_exemptions(head, body):
code = NAMED_EXEMPT.format(head=head, body=body)
assert semantics.run_check("depends_covers_compute_reads", code) == [], body
def test_a_named_display_name_override_is_left_alone():
code = """@api.depends("invoice_ids")
def _compute_display_name(self):
for rec in self:
rec.display_name = ", ".join(rec.invoice_ids.sorted("date").mapped("name"))
"""
assert semantics.run_check("depends_covers_compute_reads", code) == []
# A subscript stopped the path. rec.line_ids[0].amount read nothing past
# line_ids, so a compute taking its value from the first line looked declared.
SUBSCRIPT_COMPUTE = """@api.depends({declared})
def _compute_first(self):
for rec in self:
rec.first = {read}
"""
@pytest.mark.parametrize("read, path", [
("rec.line_ids[0].amount", "line_ids.amount"),
("rec.line_ids[-1].amount", "line_ids.amount"),
("rec.line_ids[1:3].amount", "line_ids.amount"),
("rec.line_ids.filtered(lambda l: l.paid)[:1].amount", "line_ids.amount"),
("self[0].line_ids.amount", "line_ids.amount"),
('rec.line_ids[:1].mapped("amount")', "line_ids.amount"),
])
def test_a_read_after_a_subscript_by_position_is_a_read(read, path):
code = SUBSCRIPT_COMPUTE.format(declared='"line_ids"', read=read)
hits = semantics.run_check("depends_covers_compute_reads", code)
assert ["'{0}'".format(path) in message for _, message, _ in hits] == [True], hits
code = SUBSCRIPT_COMPUTE.format(declared='"{0}"'.format(path), read=read)
assert semantics.run_check("depends_covers_compute_reads", code) == [], read
def test_a_subscript_reads_every_segment_of_the_chain_after_it():
"""rec.partner_id.child_ids[:1].email reported partner_id.child_ids and
never the email, which is the field whose change goes unnoticed."""
code = SUBSCRIPT_COMPUTE.format(declared='"partner_id"',
read="rec.partner_id.child_ids[:1].email")
hits = semantics.run_check("depends_covers_compute_reads", code)
assert sorted(message.split("'")[1] for _, message, _ in hits) == [
"partner_id.child_ids", "partner_id.child_ids.email"], hits
def test_a_loop_over_a_slice_binds_the_same_path():
code = """@api.depends("line_ids")
def _compute_rest(self):
for rec in self:
for line in rec.line_ids.sorted("sequence")[1:]:
rec.rest += line.discount
"""
hits = semantics.run_check("depends_covers_compute_reads", code)
assert sorted(message.split("'")[1] for _, message, _ in hits) == [
"line_ids.discount", "line_ids.sequence"], hits
@pytest.mark.parametrize("read", [
"rec[fname].name", # a field read by name, not records by position
"rec.line_ids[i].amount", # a name could hold either
'rec["partner_id"].name',
"rec.line_ids[0].id", # the relation itself, which is declared
])
def test_a_subscript_that_does_not_pick_records_by_position_is_not_followed(read):
code = SUBSCRIPT_COMPUTE.format(declared='"line_ids"', read=read)
assert semantics.run_check("depends_covers_compute_reads", code) == [], read
# A call stopped the path the same way. rec.sudo().partner_id.name read nothing
# past the call, so a compute taking a partner's name through sudo() looked as
# if it read nothing from the partner (ARCHITECTURE 3.78).
@pytest.mark.parametrize("read, declared, path", [
("rec.sudo().partner_id.name", "partner_id", "partner_id.name"),
('rec.with_context(lang="fr_FR").partner_id.name', "partner_id", "partner_id.name"),
("rec.line_ids.filtered(lambda l: l.paid).amount", "line_ids", "line_ids.amount"),
("rec.line_ids.exists().amount", "line_ids", "line_ids.amount"),
("rec.sudo().line_ids[:1].amount", "line_ids", "line_ids.amount"),
])
def test_a_read_through_a_call_that_keeps_the_records_is_a_read(read, declared, path):
code = SUBSCRIPT_COMPUTE.format(declared='"{0}"'.format(declared), read=read)
hits = semantics.run_check("depends_covers_compute_reads", code)
assert ["'{0}'".format(path) in message for _, message, _ in hits] == [True], hits
code = SUBSCRIPT_COMPUTE.format(declared='"{0}"'.format(path), read=read)
assert semantics.run_check("depends_covers_compute_reads", code) == [], read
@pytest.mark.parametrize("read", [
'rec.mapped("line_ids").amount', # mapped() hands back values
'rec.env["res.partner"].browse(1).name', # records of another model
"rec._first_line().amount", # a method returns what it likes
])
def test_a_read_through_any_other_call_is_not_followed(read):
code = SUBSCRIPT_COMPUTE.format(declared='"line_ids"', read=read)
assert semantics.run_check("depends_covers_compute_reads", code) == [], read
def test_a_constraint_reading_through_sudo_must_list_the_field():
code = NAMED_CONSTRAINT.format(
declared='"date_start"',
body='if self.sudo().date_end:\n raise ValidationError("x")')
hits = semantics.run_check("constrains_lists_every_field_read", code)
assert ["reads 'date_end'" in message for _, message, _ in hits] == [True], hits
def test_a_read_through_a_name_assigned_records_stays_unread():
"""Measured and not taken (ARCHITECTURE 3.80). pending is the first
pending review exactly, and binding every name whose assignments all
resolve to one path is exact too. What the depends check then reads
through such names is mostly not a dependency: on odoo core 37 of 113
findings read true, 28 of them smart defaults, and the false ones were
values kept on purpose, fields stored nowhere, and decorators merged or
covered from outside the file. The tree facts decide 27 of the 113 now,
and at most 37 of the 86 left can be true (ARCHITECTURE 3.87)."""
depends = """@api.depends('review_ids.status', 'review_ids.sequence')
def _compute_current_tier(self):
for rec in self:
pending = rec.review_ids.filtered(lambda r: r.status == 'pending').sorted('sequence')[:1]
rec.current_tier_id = pending.tier_id
"""
constrains = """@api.constrains('is_default')
def _check_unique_default(self):
defaults = self.filtered('is_default')
companies = {rec.id: rec.company_id.id for rec in defaults}
if len(set(companies.values())) < len(companies):
raise ValidationError('bad')
"""
assert semantics.run_check("depends_covers_compute_reads", depends) == []
assert semantics.run_check("constrains_lists_every_field_read", constrains) == []
# A constraint reads a field through a string as a compute does, and writing
# that field alone skips the check. Only the root is compared, as for every
# other read here: @api.constrains cannot follow a dotted name.
NAMED_CONSTRAINT = """@api.constrains({declared})
def _check_dates(self):
{body}
"""
@pytest.mark.parametrize("body, root", [
('if any(self.mapped("date_end")):\n raise ValidationError("x")', "date_end"),
('for rec in self.filtered("active"):\n rec._check()', "active"),
('if self.sorted("sequence")[:1].date_start:\n raise ValidationError("x")',
"sequence"),
('if self.filtered_domain([("state", "=", "done")]):\n raise ValidationError("x")',
"state"),
('if self[0].code:\n raise ValidationError("x")', "code"),
('for spread in self:\n moves = spread.mapped("line_ids.move_id")', "line_ids"),
])
def test_a_constraint_reading_a_field_through_a_string_must_list_it(body, root):
code = NAMED_CONSTRAINT.format(declared='"date_start"', body=body)
hits = semantics.run_check("constrains_lists_every_field_read", code)
assert ["reads '{0}'".format(root) in message for _, message, _ in hits] == [True], hits
code = NAMED_CONSTRAINT.format(declared='"date_start", "{0}"'.format(root), body=body)
assert semantics.run_check("constrains_lists_every_field_read", code) == [], body
def test_a_constraint_naming_a_field_only_in_its_message_is_quiet():
code = NAMED_CONSTRAINT.format(
declared='"date_start"',
body='if self.date_start:\n'
' raise ValidationError(", ".join(self.mapped("name")))')
assert semantics.run_check("constrains_lists_every_field_read", code) == []
# ------------------------------------------------------ what a lambda reads
def test_a_read_through_a_lambda_parameter_stays_unread():
"""Measured and not taken (ARCHITECTURE 3.72). Binding the parameter is
exact: filtered() hands its lambda every record of the receiver. But what
a filter tests is mostly a field that selects records rather than one that
feeds the value: on odoo core 40 of 127 depends findings read true, 28 of
them smart defaults, and 9 of 27 constrains findings. The tree facts decide
32 of the 121 depends findings left since then, and at most 41 of the 89
they leave can be true (ARCHITECTURE 3.87)."""
depends = """@api.depends('line_ids.amount')
def _compute_total(self):
for rec in self:
rec.total = sum(rec.line_ids.filtered(lambda l: l.state == 'done').mapped('amount'))
"""
constrains = """@api.constrains('date_start')
def _check(self):
if self.filtered(lambda r: r.date_end and r.date_end < r.date_start):
raise ValidationError('bad')
"""
assert semantics.run_check("depends_covers_compute_reads", depends) == []
assert semantics.run_check("constrains_lists_every_field_read", constrains) == []
# ------------------------------------------- narrowed by `nexa recall --aim`
# Placed against the method each fix edited, prefer-sql-constraint scored 0
# on-target against 47 other-symbol and ondelete 1 against 34 -- the two worst
# in the corpus. These pin the narrowings that came out of reading their
# findings on 229,600 lines of OpenSPP.
def test_uniqueness_across_another_model_is_not_this_rule():
"""`self.env["other.model"].search(...)` tests uniqueness across tables, and
a UNIQUE on THIS table cannot express it. Recommending one is advice that
cannot be followed, which is worse than advice nobody wanted."""
code = '''
class Handler(models.Model):
_name = "endpoint.route.handler"
@api.constrains("route")
def _check_route_unique_across_models(self):
for model in self._consumer_models():
if self.env[model].sudo().search_count([("route", "in", self.routes)]):
raise ValidationError("Non unique route")
'''
assert semantics.run_check("prefer_sql_constraint_applies", code) == []
def test_uniqueness_on_self_is_still_reported():
"""The narrowing must not cost the case the rule is for."""
code = '''
class Client(models.Model):
_name = "api.client"
@api.constrains("client_id")
def _check_unique_client_id(self):
for record in self:
if self.search([("client_id", "=", record.client_id),
("id", "!=", record.id)], limit=1):
raise ValidationError("Client ID must be unique")
'''
found = semantics.run_check("prefer_sql_constraint_applies", code)
assert len(found) == 1
assert "cannot race" in found[0][1]
def test_a_class_that_already_declares_the_constraint_is_left_alone():
"""Telling an author to do the thing they did is how a checker gets
switched off. Both spellings: _sql_constraints, and the models.Constraint
form Odoo 17 introduced."""
modern = '''
class Client(models.Model):
_name = "api.client"
_client_id_unique = models.Constraint("UNIQUE(client_id)", "Must be unique.")
@api.constrains("client_id")
def _check_unique_client_id(self):
for record in self:
if self.search([("client_id", "=", record.client_id)], limit=1):
raise ValidationError("Client ID must be unique")
'''
classic = modern.replace(
'_client_id_unique = models.Constraint("UNIQUE(client_id)", "Must be unique.")',
'_sql_constraints = [("client_id_uniq", "UNIQUE(client_id)", "Must be unique.")]')
assert semantics.run_check("prefer_sql_constraint_applies", modern) == []
assert semantics.run_check("prefer_sql_constraint_applies", classic) == []
def test_a_constraint_on_a_different_column_does_not_excuse_this_one():
"""The narrowing is per column. A model guarding `code` has not guarded
`client_id`, and treating the class as covered would lose real findings."""
code = '''
class Client(models.Model):
_name = "api.client"
_code_unique = models.Constraint("UNIQUE(code)", "Code must be unique.")
@api.constrains("client_id")
def _check_unique_client_id(self):
for record in self:
if self.search([("client_id", "=", record.client_id)], limit=1):
raise ValidationError("Client ID must be unique")
'''
assert len(semantics.run_check("prefer_sql_constraint_applies", code)) == 1
ONDELETE_BODY = '''
class Alert(models.Model):
_name = "spp.alert"
model_id = fields.Many2one("ir.model")
def run(self):
{body}
'''
def test_a_guarded_dereference_is_not_a_dangling_read():
"""The rule's complaint is that a nulled reference yields False rather than
raising. An author who tested for it has answered that, and on OpenSPP the
guard is the majority idiom: 101 findings became 20 once it was read."""
for guard in (
" if not self.model_id:\n return 0\n"
" return self.model_id.model",
" if self.model_id:\n return self.model_id.model",
" return self.model_id and self.model_id.model or ''",
" return self.model_id.model if self.model_id else ''"):
code = ONDELETE_BODY.format(body=guard)
assert semantics.run_check("ondelete_leaves_a_dangling_read", code) == [], guard
def test_an_unguarded_dereference_is_still_reported():
"""The narrowing must not cost the case the rule is for: a compute that
reads straight through the relation and silently writes False."""
code = ONDELETE_BODY.format(body=" return self.model_id.model")
found = semantics.run_check("ondelete_leaves_a_dangling_read", code)
assert len(found) == 1
assert "set null" in found[0][1]
def test_a_guard_in_one_method_does_not_excuse_another():
"""Function-level, not file-level. A method that checks the field says
nothing about the method next to it that does not."""
code = '''
class Alert(models.Model):
_name = "spp.alert"
model_id = fields.Many2one("ir.model")
def careful(self):
if not self.model_id:
return 0
return self.model_id.model
def careless(self):
return self.model_id.name
'''
found = semantics.run_check("ondelete_leaves_a_dangling_read", code)
assert len(found) == 1
def test_an_unproven_advisory_rule_still_fires_correctly():
"""odoo.except-order-follows-the-hierarchy ships advisory because nothing
measured here has met the defect, not because the defect is imaginary. The
checker still has to work, or the label is covering for a broken rule."""
shadowed = (
"try:\n"
" record.action_approve()\n"
"except UserError as exc:\n"
" pass\n"
"except AccessError as exc:\n"
" pass\n")
found = semantics.run_check("handler_order_shadows_a_subclass", shadowed)
assert len(found) == 1
assert "never runs" in found[0][1]
assert semantics.run_check(
"handler_order_shadows_a_subclass",
shadowed.replace("UserError", "_TMP").replace("AccessError", "UserError")
.replace("_TMP", "AccessError")) == []
def test_the_odoo_hierarchy_matches_odoo_exceptions():
"""Read out of odoo/exceptions.py, identical in 16.0 and 18.0. If Odoo ever
reparents one of these the rule is wrong, and this is where that shows."""
for name in ("AccessDenied", "AccessError", "MissingError", "ValidationError"):
assert semantics._is_subclass_of(name, "UserError"), name
assert not semantics._is_subclass_of("UserError", "ValidationError")
assert not semantics._is_subclass_of("RedirectWarning", "UserError")
# ---------------------------------------------- triggers on the two conditionals
def facts(declared=(), deleted=(), multi_company=False, complete=True):
return {"walked": True, "complete": complete, "files": 1,
"models_declared": set(declared), "models_deleted": set(deleted),
"multi_company": multi_company}
UNGUARDED = ONDELETE_BODY.format(body=" return self.model_id.model")
def test_ondelete_says_nothing_when_the_parent_is_never_deleted():
"""Master data. The rule's harm is a read through a NULLED reference, and
nothing nulls one if nothing deletes the parent."""
quiet = facts(declared=["ir.model"])
assert semantics.run_check("ondelete_leaves_a_dangling_read",
UNGUARDED, quiet) == []
def test_ondelete_is_a_defect_when_the_parent_really_is_deleted():
found = semantics.run_check("ondelete_leaves_a_dangling_read", UNGUARDED,
facts(declared=["ir.model"],
deleted=["ir.model"]))
assert len(found) == 1
assert found[0][2] == "defect", "a trigger that fired should promote"
assert "does delete ir.model" in found[0][1]
def test_ondelete_keeps_its_grade_when_the_trigger_cannot_be_answered():
"""No tree, or a parent this tree does not define. The rule is reported
exactly as it was before triggers existed, which is the honest answer."""
for supplied in (None, facts(declared=["something.else"])):
found = semantics.run_check("ondelete_leaves_a_dangling_read",
UNGUARDED, supplied)
assert len(found) == 1
assert found[0][2] is None, "an unanswered trigger must not promote"
CHECK_COMPANY_BODY = '''
class Thing(models.Model):
_name = "my.thing"
company_id = fields.Many2one("res.company")
journal_id = fields.Many2one("account.journal")
'''
def test_check_company_says_nothing_in_a_single_company_codebase():
"""One company cannot leak to another. The rule describes a boundary this
codebase does not have."""
assert semantics.run_check("check_company_missing_on_company_owned_relation",
CHECK_COMPANY_BODY, facts()) == []
def test_check_company_is_a_defect_where_multi_company_is_in_use():
found = semantics.run_check("check_company_missing_on_company_owned_relation",
CHECK_COMPANY_BODY, facts(multi_company=True))
assert len(found) == 1
assert found[0][2] == "defect"
assert "runs multi-company" in found[0][1]
def test_check_company_keeps_its_grade_with_no_tree():
found = semantics.run_check("check_company_missing_on_company_owned_relation",
CHECK_COMPANY_BODY, None)
assert len(found) == 1 and found[0][2] is None
def test_an_incomplete_walk_never_silences_either_rule():
"""Promote on evidence, never drop on the absence of it. A module-sized walk
is exactly the case where absence means 'not in this module'."""
partial = facts(declared=["ir.model"], complete=False)
assert len(semantics.run_check("ondelete_leaves_a_dangling_read",
UNGUARDED, partial)) == 1
assert len(semantics.run_check("check_company_missing_on_company_owned_relation",
CHECK_COMPANY_BODY, partial)) == 1
# ------------------------------------------ depends: prefix coverage, both ways
STORED_COMPUTE_DOTTED = """total = fields.Float(compute='_c', store=True)
@api.depends('line_ids.price')
def _c(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)"""
STORED_COMPUTE_ROOT_ONLY = """total = fields.Float(compute='_c', store=True)
@api.depends('line_ids')
def _c(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)"""
def test_a_dotted_depends_covers_a_read_of_its_own_prefix():
"""@api.depends('line_ids.price') registers the whole chain, so the ORM
already recomputes when line_ids itself changes and a body reading
rec.line_ids needs nothing more.
Comparing declarations as exact strings made that correct code stale: 42 of
this checker's 104 findings on OpenSPP were this, 40% of its output. The
rule's own `correct` sample hid it by declaring 'line_ids' redundantly
alongside the dotted paths, so validate passed throughout.
"""
assert semantics.depends_covers_compute_reads(STORED_COMPUTE_DOTTED) == []
def test_a_declared_prefix_still_does_not_cover_what_hangs_off_it():
"""The converse, which is the rule itself and must not follow.
Declaring 'line_ids' cannot excuse a body reading line_ids.price -- that is
the value that goes stale, and the whole reason the checker exists. A
coverage test asserting only the first direction would happily accept a
checker that had stopped working.
"""
hits = semantics.depends_covers_compute_reads(STORED_COMPUTE_ROOT_ONLY)
assert hits, "the incomplete depends stopped being reported"
assert any("line_ids.price" in message for _, message in hits)
# -------------------------------------- decorators nothing can read statically
COMPUTED_CONSTRAINS = """@api.constrains(*FIELDS)
def _check(self):
for rec in self:
if rec.date_start > rec.date_end:
raise ValidationError('bad range')"""
def test_a_computed_decorator_is_a_gap_and_not_a_finding():
"""@api.constrains(*FIELDS) is legal code that a static pass cannot read.
It used to collapse into the same answer as "declares nothing", so every
field the body read became a missing declaration -- and the sentence it
produced said "declares only nothing", which is where the bug announced
itself. Nothing is wrong with the code, so nothing is reported; what could
not be decided is recorded instead.
"""
gaps = []
assert semantics.constrains_lists_every_field_read(
COMPUTED_CONSTRAINS, gaps=gaps) == []
assert gaps and "not string literals" in gaps[0]
assert "_check()" in gaps[0], "the gap names the method it could not read"
def test_every_checker_survives_a_decorator_it_cannot_read():
"""Four checkers read a decorator's arguments and two of them only tested
for absence. Handing them a sentinel they did not expect turned a false
positive into a crash, which scan_source reports as a broken checker."""
for name in semantics.CHECKS:
semantics.run_check(name, COMPUTED_CONSTRAINS)
# ------------------------------------------ constrains_declares_a_dotted_name
def dotted(code, gaps=None):
return semantics.constrains_declares_a_dotted_name(code, gaps=gaps)
def test_a_dotted_name_of_three_segments_is_caught():
"""The regex wanted a closing quote after the second segment. In
partner_id.country_id.code it found a dot there, and said nothing about a
name the ORM ignores exactly as it ignores a two-segment one."""
hits = dotted("""@api.constrains('partner_id.country_id.code')
def _check_country(self):
for rec in self:
if rec.partner_id.country_id.code not in ('BE', 'FR'):
raise ValidationError('Unsupported country')""")
assert [line for line, _ in hits] == [1]
assert "'partner_id.country_id.code'" in hits[0][1]
assert "never runs the check" in hits[0][1]
def test_a_dotted_name_with_a_digit_in_it_is_caught():
"""[a-z_] has no digits, so every name through an l10n_ field was invisible
to the regex. The plain name beside it still registers, and the message says
that is now the only write that runs the check."""
hits = dotted("""@api.constrains('l10n_in_gst_treatment', 'partner_id.l10n_in_pan')
def _check_pan(self):
for move in self:
if not move.partner_id.l10n_in_pan:
raise ValidationError('PAN required')""")
assert len(hits) == 1
assert "'partner_id.l10n_in_pan'" in hits[0][1]
assert "only when l10n_in_gst_treatment is written" in hits[0][1]
def test_plain_names_are_not_a_finding():
"""Every literal declaration on the three trees is this shape, 41 of them
with a digit in a name. A checker that fired here would fire on all of it."""
assert dotted("""@api.constrains('l10n_in_pan', 'date_start', 'date_end')
def _check(self):
pass""") == []
assert dotted("""@api.constrains()
def _check(self):
pass""") == []
def test_a_dotted_name_is_reported_on_its_decorator():
"""Where the names are and where a marker goes, whichever line the dotted
one sits on and whatever else decorates the method."""
hits = dotted("""class Move(models.Model):
@api.model
@api.constrains(
'name',
'partner_id.country_id',
)
def _check(self):
pass""")
assert [line for line, _ in hits] == [3]
def test_a_computed_constrains_list_is_a_gap_and_not_a_finding():
"""*FIELDS might hold a dotted name or not; nothing here can say. Reading it
as "nothing dotted" would be a clean result nobody earned."""
gaps = []
assert dotted(COMPUTED_CONSTRAINS, gaps=gaps) == []
assert gaps and "not string literals" in gaps[0] and "_check()" in gaps[0]
def test_a_method_neither_constrains_checker_can_read_is_one_gap():
"""Both constrains checkers skip @api.constrains(*FIELDS), and each says so.
`check` printed that twice, and fieldtest counted it twice as a file it
could not fully read."""
import layers
assert {"constrains_lists_every_field_read",
"constrains_declares_a_dotted_name"} <= semantics.GAP_CHECKS
rules = [r for r in corpus.load_rules() if r["stack"] == "odoo"]
gaps = []
layers.scan_source(COMPUTED_CONSTRAINS, rules, "odoo", gaps=gaps)
assert len(gaps) == 1, gaps
# ---------------------------------------------- balance_js: regex vs division
def test_a_regex_after_a_keyword_is_not_read_as_division():
"""`return /[(]/` is ordinary JavaScript. The heuristic only knew about
punctuation, so the slash after a keyword read as division and the regex
BODY was scanned as code -- its lone bracket reported as unbalanced in a
file that is fine. That reaches the user as a high finding, and in
`nexa run` it buys an escalation to the 30B over nothing at all."""
for source in ("function f(){ return /[(]/.test(s); }",
"switch (c) { case 'a': return /]/.test(v); }",
"const ok = typeof v === 'string' && /[)]/.test(v);",
"function g(){ return /[{]/.test(x) ? 1 : 2; }"):
assert semantics.balance_js(source) == [], source
def test_division_stays_division_and_a_real_imbalance_still_fires():
"""The other half of the same change. Widening the regex heuristic must not
turn arithmetic into a literal, and must not stop the scanner catching the
truncated answer it exists for."""
assert semantics.balance_js("const r = total / count; f();") == []
assert semantics.balance_js("const r = 10 / 2; f();") == []
assert semantics.balance_js("function f(){ return 1;") != []
assert semantics.balance_js("function f(){ return [1; }") != []
# ------------------------------- narrowings from the OCA classification (3.43)
WIZARD_STORED_COMPUTE = """class Wiz(models.TransientModel):
_name = "my.wizard"
total = fields.Float(compute="_c", store=True)
@api.depends("line_ids")
def _c(self):
for rec in self:
rec.total = sum(l.price for l in rec.line_ids)
"""
ONDELETE_SHAPE = """class Alert(models.{kind}):
_name = "spp.alert"
model_id = fields.Many2one("ir.model"{extra})
def run(self):
return self.model_id.model
"""
CHECK_COMPANY_SHAPE = """class Thing(models.Model):
_name = "my.thing"
company_id = fields.Many2one("res.company", required=True)
journal_id = fields.Many2one("account.journal"{extra})
"""
MULTI_COMPANY = {"walked": True, "complete": True, "files": 1,
"models_declared": set(), "models_deleted": set(),
"multi_company": True}
def test_a_wizard_is_not_asked_for_a_complete_depends():
"""A TransientModel row is vacuumed. "Written once and never recomputed,
survives restarts, diverges from reality" is about a row that outlives what
it was computed from, and a wizard's does not. Five of the 24 false
positives classified on OCA were this."""
assert semantics.depends_covers_compute_reads(WIZARD_STORED_COMPUTE) == []
def test_the_same_compute_on_a_persistent_model_still_fires():
"""The narrowing has to cost nothing. Identical code on a models.Model is
the defect the rule exists for."""
persistent = WIZARD_STORED_COMPUTE.replace("models.TransientModel", "models.Model")
assert semantics.depends_covers_compute_reads(persistent)
def test_ondelete_is_not_asked_of_a_wizard_or_a_field_with_no_column():
"""ondelete governs a foreign key. A TransientModel's row is vacuumed before
a parent can be deleted under it, and a compute= without store=True has no
column at all -- so in both the advice names something that is not there."""
assert semantics.ondelete_leaves_a_dangling_read(
ONDELETE_SHAPE.format(kind="TransientModel", extra="")) == []
assert semantics.ondelete_leaves_a_dangling_read(
ONDELETE_SHAPE.format(kind="Model", extra=', compute="_c"')) == []
def test_ondelete_still_fires_on_a_real_column():
"""Both halves of the same predicate: a persistent model, and a compute that
IS stored, still have a foreign key to govern."""
assert semantics.ondelete_leaves_a_dangling_read(
ONDELETE_SHAPE.format(kind="Model", extra=""))
assert semantics.ondelete_leaves_a_dangling_read(
ONDELETE_SHAPE.format(kind="Model", extra=', compute="_c", store=True'))
def test_check_company_is_not_asked_of_a_mirror_or_a_field_with_no_column():
"""A related= field mirrors a value owned by another record, so the
constraint belongs on the source -- which the same run flags in its own
right, making this the same decision reported twice. A computed field with
no store has no column to constrain."""
assert semantics.check_company_missing_on_company_owned_relation(
CHECK_COMPANY_SHAPE.format(extra=', related="parent_id.journal_id"'),
MULTI_COMPANY) == []
assert semantics.check_company_missing_on_company_owned_relation(
CHECK_COMPANY_SHAPE.format(extra=', compute="_c"'), MULTI_COMPANY) == []
def test_check_company_still_fires_on_a_stored_relation():
assert semantics.check_company_missing_on_company_owned_relation(
CHECK_COMPANY_SHAPE.format(extra=""), MULTI_COMPANY)
assert semantics.check_company_missing_on_company_owned_relation(
CHECK_COMPANY_SHAPE.format(extra=', compute="_c", store=True'), MULTI_COMPANY)
MESSAGE_IN_A_VARIABLE = """@api.constrains("sequence_id")
def _check(self):
for j in self:
if j.sequence_id and not j.sequence_id.company_id:
msg = _("no company on %(s)s for %(j)s", s=j.sequence_id.name, j=j.display_name)
raise ValidationError(msg)
"""
def test_a_message_built_into_a_variable_is_still_only_a_message():
"""A field read to phrase an error does not decide whether the invariant
holds, so it cannot decide when the constraint must re-run. That exemption
existed and knew only the inline spelling -- OCA builds the message one line
before the raise, and display_name was reported as a dependency."""
assert semantics.constrains_lists_every_field_read(MESSAGE_IN_A_VARIABLE) == []
def test_a_field_read_in_the_condition_is_still_caught():
"""The exemption is about the nodes inside the message, not the field name.
A constraint that TESTS an undeclared field is the defect either way."""
hits = semantics.constrains_lists_every_field_read(
MESSAGE_IN_A_VARIABLE.replace("not j.sequence_id.company_id",
"not j.active"))
assert any("active" in message for _, message in hits)
CALLERS_CURSOR = """def go(self):
for rec in self:
rec.write({"x": 1})
self.env.cr.commit()
"""
OWN_CURSOR = """def batch_unlink(self):
with Registry(self.env.cr.dbname).cursor() as new_cr:
new_env = api.Environment(new_cr, self.env.uid, self.env.context)
while self:
batch = self[0:1000]
self -= batch
batch.with_env(new_env).unlink()
new_env.cr.commit()
"""
def test_a_commit_on_a_cursor_this_function_opened_is_not_the_callers():
"""The rule says let the CALLER own the transaction, and a cursor opened
three lines earlier is not the caller's. Both of this rule's real-world
instances are the two sides of that line: OpenSPP's expiry cron commits
self.env.cr and is a true positive its author marked and kept, OCA's
batch_unlink commits one it opened itself."""
assert semantics.commit_inside_a_loop(CALLERS_CURSOR)
assert semantics.commit_inside_a_loop(OWN_CURSOR) == []
def test_the_independent_cursor_exemption_does_not_leak_between_functions():
"""Bound positively and scoped per function. A new_cr opened in one method
must not excuse a commit in another that reuses the name -- which is what a
file-wide scan of the names would have done."""
hits = semantics.commit_inside_a_loop(CALLERS_CURSOR + OWN_CURSOR)
assert len(hits) == 1, hits
# ------------------------------ a constraint the series will not create (3.45)
UNIQUENESS_BESIDE = """class Thing(models.Model):
_name = "my.thing"
{declaration}
@api.constrains("code")
def _check_unique_code(self):
for rec in self:
if self.search_count([("code", "=", rec.code), ("id", "!=", rec.id)]):
raise ValidationError("Code must be unique")
"""
LEGACY = '_sql_constraints = [("code_uniq", "UNIQUE(code)", "Code must be unique.")]'
MODERN = '_code_uniq = models.Constraint("UNIQUE(code)", "Code must be unique.")'
def test_a_legacy_sql_constraint_is_not_a_constraint_on_odoo_19():
"""Odoo 19 ignores the _sql_constraints attribute: the registry logs a
warning at load and the constraint is never created. Reading it as the
column being spoken for exempts a class on the strength of nothing, and
leaves the racing Python check as the only thing standing."""
code = UNIQUENESS_BESIDE.format(declaration=LEGACY)
assert semantics.prefer_sql_constraint_applies(code, {"odoo_series": "19.0"})
def test_the_same_declaration_still_counts_on_16():
"""It is a real constraint there, and firing on every 16, 17 and 18 module
that did exactly the right thing is the worse trade."""
code = UNIQUENESS_BESIDE.format(declaration=LEGACY)
assert semantics.prefer_sql_constraint_applies(code, {"odoo_series": "16.0"}) == []
assert semantics.prefer_sql_constraint_applies(code, {"odoo_series": None}) == []
assert semantics.prefer_sql_constraint_applies(code) == []
def test_models_constraint_counts_on_every_series():
"""The spelling that works from 17 onwards is never the dead one."""
code = UNIQUENESS_BESIDE.format(declaration=MODERN)
for series in (None, "16.0", "17.0", "19.0"):
assert semantics.prefer_sql_constraint_applies(
code, {"odoo_series": series}) == [], series
def test_the_corpus_does_not_advise_a_declaration_odoo_19_ignores():
"""Two rules told an author to write _sql_constraints, which from 19 creates
nothing. A corpus whose subject is 'a check that looks like it holds and
does not' cannot be the thing recommending one."""
advising = []
for rule in corpus.load_rules():
for line in rule["correct"].splitlines():
# In a comment it is documentation of the older spelling, which is
# the point. Uncommented, it is the advice.
if "_sql_constraints" in line and not line.lstrip().startswith("#"):
advising.append(rule["id"])
assert advising == [], advising
def test_the_onchange_guard_knows_every_spelling_of_a_constraint():
"""The guard has to accept the legacy attribute: that IS a constraint on 16
through 18. Narrowing it to models.Constraint alone was tried and reverted --
the next field test reported OCA's account_loan, branch 18.0, with a real
_sql_constraints block and an onchange properly backed by it.
The 19 case is closed where the series is legible instead, in
prefer_sql_constraint_applies.
Asserted against the checker rather than against a satisfied_pattern,
which is where the guard moved when the rule became an AST pass. The
three spellings are the same three."""
body = ('class M(models.Model):\n'
' @api.onchange("qty")\n'
' def _onchange_qty(self):\n'
' if self.qty <= 0:\n'
' raise ValidationError("no")\n')
assert semantics.run_check("onchange_tries_to_enforce", body), \
"an unbacked onchange that raises is the violation"
for spelling in (' @api.constrains("qty")\n def _check(self):\n pass\n',
' _sql_constraints = [("q", "CHECK(qty > 0)", "no")]\n',
' _qty = models.Constraint("CHECK(qty > 0)", "no")\n'):
backed = body + spelling
assert not semantics.run_check("onchange_tries_to_enforce", backed), spelling
def test_a_dead_sql_constraint_does_not_back_an_onchange_on_19():
"""From 19 the registry never creates a _sql_constraints block, so it
backs nothing. On 18 and on an unknown series it still counts, as the
test above requires."""
body = ('class M(models.Model):\n'
' _sql_constraints = [("q", "CHECK(qty > 0)", "no")]\n'
'\n'
' @api.onchange("qty")\n'
' def _onchange_qty(self):\n'
' if self.qty <= 0:\n'
' raise ValidationError("no")\n')
on_19 = semantics.run_check("onchange_tries_to_enforce", body,
{"odoo_series": "19.0"})
assert [line for line, _, _ in on_19] == [4], on_19
for series in ("18.0", None):
assert semantics.run_check("onchange_tries_to_enforce", body,
{"odoo_series": series}) == [], series
real = body.replace(' _sql_constraints = [("q", "CHECK(qty > 0)", "no")]\n',
' _q = models.Constraint("CHECK(qty > 0)", "no")\n')
assert semantics.run_check("onchange_tries_to_enforce", real,
{"odoo_series": "19.0"}) == []
CREDIT_ONCHANGE = ('class M(models.Model):\n'
'{constraint}'
' @api.onchange("credit_limit")\n'
' def _onchange_credit_limit(self):\n'
' if self.credit_limit < 0:\n'
' raise ValidationError("no")\n')
@pytest.mark.parametrize("constraint", [
' _ref = models.Constraint("UNIQUE(reference)", "no")\n',
' _sql_constraints = [("ref", "UNIQUE(reference)", "no")]\n',
' @api.constrains("reference")\n def _check(self):\n pass\n',
' @api.constrains("partner_id.credit_limit")\n def _check(self):\n pass\n',
])
def test_a_constraint_on_another_field_does_not_back_an_onchange(constraint):
"""A uniqueness rule on `reference` used to silence an onchange raising
on `credit_limit`. A dotted @api.constrains name registers nothing."""
code = CREDIT_ONCHANGE.format(constraint=constraint)
assert semantics.run_check("onchange_tries_to_enforce", code), constraint
@pytest.mark.parametrize("constraint", [
' _c = models.Constraint("CHECK(credit_limit >= 0)", "no")\n',
' @api.constrains("credit_limit")\n def _check(self):\n pass\n',
' @api.constrains(*FIELDS)\n def _check(self):\n pass\n',
' _sql_constraints = BASE + [("c", "CHECK(credit_limit >= 0)", "no")]\n',
])
def test_a_constraint_on_a_watched_field_or_an_unreadable_one_backs_it(constraint):
code = CREDIT_ONCHANGE.format(constraint=constraint)
assert semantics.run_check("onchange_tries_to_enforce", code) == [], constraint
WARNING_ONCHANGE = ('class M(models.Model):\n'
' @api.onchange("qty")\n'
' def _onchange_qty(self):\n'
' self.hint = False\n'
' if self.qty < 0:\n'
'{branch}'
' return {{"warning": {{"title": "x", "message": "y"}}}}\n')
@pytest.mark.parametrize("branch,refuses", [
("", False), # a notice
(" self.qty = 0\n", True), # the watched field back
(" self.update({'qty': 0})\n", True),
(" self.partner_id = False\n", True), # a field cleared
(" self.location_id = self.other\n", False), # a suggestion
])
def test_a_warning_refuses_only_when_its_branch_takes_the_value_back(branch, refuses):
"""Most warning dicts on core inform: "this reference already exists",
"the rate is far from the previous one". The user saves past them in the
form as in an import, so nothing is enforced anywhere. `self.hint = False`
outside the branch does not count: it is a UI flag, not the refusal."""
code = WARNING_ONCHANGE.format(branch=branch)
found = semantics.run_check("onchange_tries_to_enforce", code)
assert bool(found) is refuses, (branch, found)
UNPLAN = ('_("It is not possible to unplan one single Work Order. "\n'
' "Unplan the manufacturing order instead.")')
WORKORDER = ('class W(models.Model):\n'
' _name = "w"\n'
' date_finished = fields.Datetime({field})\n'
'\n'
' def {server}(self):\n'
' if not self.date_finished:\n'
' raise UserError({server_message})\n'
'\n'
' @api.onchange("date_finished")\n'
' def _onchange_date_finished(self):\n'
' if not self.date_finished:\n'
' raise UserError(' + UNPLAN + ')\n')
@pytest.mark.parametrize("field,server,message,backed", [
('inverse="_set_dates"', "_set_dates", UNPLAN, True), # mrp.workorder
('', "write", UNPLAN, True),
('', "action_unplan", UNPLAN, False), # not run on every write
('inverse="_set_dates"', "_set_dates",
'_("Unplanning one work order alone is not allowed at all.")', False),
])
def test_the_server_raising_the_same_message_backs_an_onchange(field, server,
message, backed):
"""The same sentence raised again where the server runs it on every
write is the same check. A paraphrase is not, and neither is a method
the client has to choose to call."""
code = WORKORDER.format(field=field, server=server, server_message=message)
found = semantics.run_check("onchange_tries_to_enforce", code)
assert (found == []) is backed, found
APIKEY = ('class D(models.TransientModel):\n'
' _name = "d"\n'
'\n'
' @api.onchange("expiration_date")\n'
' def _onchange_expiration_date(self):\n'
' self.env["k"]._check_expiration_date({onchange_arg})\n'
' if not self.expiration_date:\n'
' raise UserError("no")\n'
'\n'
' def create(self, vals_list):\n'
' res = super().create(vals_list)\n'
' self.env["k"]._check_expiration_date(res.expiration_date)\n'
' return res\n')
def test_the_same_check_helper_on_the_watched_field_backs_an_onchange():
"""res.users.apikeys.description: the onchange and create() both call
_check_expiration_date on the watched field. A check helper handed
nothing the onchange watches, like a bare _check_company(), is not."""
backed = APIKEY.format(onchange_arg="self.expiration_date")
assert semantics.run_check("onchange_tries_to_enforce", backed) == []
other = APIKEY.format(onchange_arg="self.company_id")
assert semantics.run_check("onchange_tries_to_enforce", other)
# ------------------------------------------------ the value-origin trace
#
# Four rules were classified false for one reason: each read a variable's NAME
# and treated that as evidence about its value. Every case below is a finding
# somebody read by hand and recorded a verdict for, in
# artifacts/classification-{oca,openspp,python}-2026-09.json.
INT_ZERO_CASES = [
("search_count", """
def f(self):
total_population = self.env["res.partner"].search_count([])
if total_population == 0:
return 0
"""),
("read_group _count, keyed by an f-string", """
def f(self, field_name, rows):
for row in rows:
group_total = row[f"{field_name}_count"]
if group_total == 0:
continue
"""),
("SELECT COUNT(*) through a cursor", """
def f(cr, table, where):
cr.execute(f"SELECT COUNT(*) FROM {table} WHERE {where}")
total = cr.fetchone()[0]
if total == 0:
return []
"""),
("a field the same file declares as fields.Integer", """
class L(models.Model):
qty = fields.Integer(string="Quantity")
def f(self):
self.line_ids.filtered(lambda x: x.qty == 0).unlink()
"""),
]
@pytest.mark.parametrize("label,body", INT_ZERO_CASES,
ids=[c[0] for c in INT_ZERO_CASES])
def test_an_integer_compared_to_zero_is_not_a_float_comparison(label, body):
"""All five false positives on this rule compared an INTEGER to zero, where
== is exact and the rule has nothing to say. Three of them and both true
positives were spelled `total == 0`, so the name could never have separated
them."""
assert semantics.run_check("float_equality_against_zero", body) == [], label
def test_a_float_compared_to_zero_still_fires():
"""The guard suppresses; it must not suppress everything. `sum()` over
amounts is the true positive that shares its spelling with three of the
false ones."""
body = """
def f(sorted_amounts):
total = sum(sorted_amounts)
if total == 0:
return 0.0
"""
assert semantics.run_check("float_equality_against_zero", body)
def test_a_locally_built_filename_is_not_a_traversal():
"""0 true, 4 false. All four join a configured directory to a name the
module itself produced, and the pattern matched the word `filename`."""
body = """
def f(self, data_path):
for file_config in [{"filename": "a.csv"}]:
file_path = os.path.join(data_path, file_config["filename"])
"""
assert semantics.run_check("path_join_takes_an_external_name", body) == []
def test_a_component_from_outside_the_file_still_fires():
body = "full = os.path.join(UPLOAD_DIR, request.args['name'])\n"
assert semantics.run_check("path_join_takes_an_external_name", body)
def test_resolving_the_path_clears_the_join_in_that_function_only():
"""The fix is to resolve and compare, and it is written beside the join. A
realpath in a DIFFERENT function says nothing about this one."""
fixed = """
def f(base):
full = os.path.realpath(os.path.join(base, request.args['name']))
if os.path.commonpath([base, full]) != base:
raise ValueError("escapes")
"""
assert semantics.run_check("path_join_takes_an_external_name", fixed) == []
elsewhere = fixed + """
def g(base):
return open(os.path.join(base, request.args['other']), 'rb')
"""
assert semantics.run_check("path_join_takes_an_external_name", elsewhere)
def test_the_orm_monetary_idiom_is_not_a_decimal_violation():
"""Odoo's Monetary field IS a float -- the ORM defines it that way -- so
inside a module the rule fired on the idiom and named a remedy that cannot
be taken. Both shapes below were classified false."""
for body in ("""
class E(models.Model):
def f(self):
ctx = {"base_amount": float(self.amount or 0.0)}
""", """
def f(self):
result = self.env["spp.cel.service"].evaluate_expression(self.e, {})
amount = float(result)
"""):
assert semantics.run_check("float_coercion_of_external_money", body) == [], body
def test_a_money_string_from_outside_still_fires():
"""Where the value arrives as a string, Decimal(value) IS available and the
rule's remedy can be taken. This is the rule's own wrong sample."""
body = "price = float(payload['price'])\n"
assert semantics.run_check("float_coercion_of_external_money", body)
def test_an_onchange_does_not_answer_for_the_method_below_it():
"""All three false positives on this rule were the same failure: a short
onchange that assigns a field, followed by an action method that raises,
with the regex's 400-character window crossing between them.
spp_studio/wizard/variable_remap_wizard.py is the shape, verbatim."""
body = """
class W(models.TransientModel):
@api.onchange("variable_id")
def _onchange_variable_id(self):
if self.variable_id and self.variable_id.source_model:
self.target_model = self.variable_id.source_model
def action_remap(self):
self.ensure_one()
if not self.new_field_id:
raise UserError(_("Please select a field to map to."))
"""
assert semantics.run_check("onchange_tries_to_enforce", body) == []
def test_the_onchange_finding_anchors_on_the_decorator():
"""Where the rule is, and where a marker about it gets written. A
FunctionDef's own lineno is the `def`, one line further down."""
body = ('class M(models.Model):\n'
' @api.onchange("qty")\n'
' def _onchange_qty(self):\n'
' raise ValidationError("no")\n')
found = semantics.run_check("onchange_tries_to_enforce", body)
assert [line for line, _, _ in found] == [2]
# ------------------------------------------------- sudo, all three clauses
@pytest.mark.parametrize("clause,body", [
("read", "def f(self):\n"
" return self.env['res.partner'].sudo().search([])\n"),
("create", "def f(self, vals):\n"
" return self.env['res.partner'].sudo().create(vals)\n"),
("write", "def f(self):\n"
" self.env['res.partner'].sudo().write({'name': 'x'})\n"),
("unlink", "def f(self):\n"
" self.env['res.partner'].sudo().unlink()\n"),
("handed to a call", "def f(self):\n"
" p = self.env['res.partner'].sudo()\n"
" return self._handle(p)\n"),
("returned", "def f(self):\n"
" p = self.env['res.partner'].sudo()\n"
" return p\n"),
])
def test_sudo_covers_the_three_clauses_its_rule_states(clause, body):
"""The rule says three things -- scope it narrowly, re-apply company
filtering, never pass a sudo recordset onward -- and only the read was
checked. `.sudo(` is the largest cluster in the recall harvest at 70
missed against 6 caught, and on the OpenSPP tree the unchecked write
clause has 136 call sites against the read clause's 116."""
assert semantics.run_check("sudo_drops_record_rules", body), clause
@pytest.mark.parametrize("why,body", [
("the call re-applies company scoping by hand",
"def f(self):\n"
" return self.env['res.partner'].sudo().search("
"[('company_id', 'in', self.env.company.ids)])\n"),
("the recordset never leaves the function",
"def f(self):\n"
" p = self.env['res.partner'].sudo()\n"
" return p.mapped('name')\n"),
])
def test_sudo_stays_quiet_where_the_rule_is_already_followed(why, body):
assert semantics.run_check("sudo_drops_record_rules", body) == [], why
# ------------------------------------------------------------- docstrings
def test_a_docstring_is_prose_and_is_not_matched_as_code():
"""Two of the 50 findings the Python composition added were this: `eval()`
named in a docstring in a sentence arguing against using it, and a
`datetime.now()` inside a doctest example."""
src = ('def f():\n'
' """Never use eval() here.\n'
'\n'
' >>> datetime.now()\n'
' """\n'
' return 1\n')
view = semantics.strip_comments(src)
assert "eval(" not in view
assert "datetime.now()" not in view
def test_a_real_string_literal_survives_and_the_geometry_does_too():
"""Only docstrings. A triple-quoted SQL query is data the checkers have a
legitimate interest in, and a line or column that moved would misreport
every finding below it."""
src = 'Q = """SELECT COUNT(*) FROM t"""\n\ndef f():\n """doc"""\n return Q\n'
view = semantics.strip_comments(src)
assert "SELECT COUNT(*)" in view
assert "doc" not in view
assert len(view) == len(src)
assert view.count("\n") == src.count("\n")
def test_rebinding_self_to_sudo_elevates_the_rest_of_the_method():
"""The shape behind the one miss the README names by hand.
OCA server-tools cbefaa511485, "[FIX] excel_import_export: remove sudo()
when importing record", deletes a single line -- `self = self.sudo()` --
and says what it cost: "finding multiple records across all companies
during import when they have same record name. Previously, importing with
sudo could bypass multi-company record rules."
Nothing below that line names sudo, so every clause that looks for
`.sudo().<method>()` is blind to it, and the recordset is never passed
anywhere either. It replaces the thing the whole method is written
against, which is the escape clause at its widest.
Run against that commit's own before and after images, the finding sits on
line 283, is gone afterwards, and 283 is one of the two lines the fix
changed -- an on-target catch by recall's own definition.
"""
before = ("class X(models.AbstractModel):\n"
" def import_xlsx(self, res_id, template):\n"
" self = self.sudo()\n"
" record = self.env[template.res_model].browse(res_id)\n"
" return record\n")
after = before.replace(" self = self.sudo()\n", "")
found = semantics.run_check("sudo_drops_record_rules", before)
assert [line for line, _, _ in found] == [3]
assert not semantics.run_check("sudo_drops_record_rules", after)
def test_a_rebind_keeps_the_rules_own_grade():
"""The touch and escape clauses report `unproven` because nothing measured
has met a real one. This clause has: a fix commit that deletes the line and
explains the multi-company leak it caused. So it carries no grade of its
own and keeps the rule's `defect`."""
body = ("def f(self):\n"
" self = self.sudo()\n"
" return self.env['res.partner'].search([])\n")
grades = {grade for _, _, grade in
semantics.run_check("sudo_drops_record_rules", body)}
assert None in grades, "a rebind must not be downgraded to unproven"
def test_a_sudo_recordset_bound_to_a_fresh_name_is_not_a_rebind():
"""`p = self.env['x'].sudo()` binds a new name and elevates nothing that
was not already written against it. Only rebinding a name to its OWN sudo
form silently elevates the code below."""
body = ("def f(self):\n"
" p = self.env['res.partner'].sudo()\n"
" return p.mapped('name')\n")
assert semantics.run_check("sudo_drops_record_rules", body) == []
def test_sudo_false_is_de_escalation_and_is_never_a_finding():
"""`sudo(False)` turns record rules back ON.
It is how a method that must run under the caller's own rights says so.
Odoo core does it 60 times, including mail's `_check_attachments_access`,
whose docstring reads "This method relies on access rules/rights and
therefore it should not be called from a sudo env." Reading it as an
escalation reports the safeguard as the defect.
Neither OpenSPP nor the OCA trees contain a single `sudo(False)`, so this
was invisible until the corpus was pointed at odoo core.
"""
for body in ("class A(models.Model):\n"
" def f(self, toks):\n"
" self = self.sudo(False)\n"
" return self.env['x'].search([])\n",
"def f(self):\n"
" return self.env['res.partner'].sudo(False).search([])\n",
"def f(self, vals):\n"
" return self.env['res.partner'].sudo(False).create(vals)\n"):
assert semantics.run_check("sudo_drops_record_rules", body) == [], body
def test_a_computed_sudo_flag_still_counts_as_escalation():
"""`sudo(flag)` can be either, and an unreadable argument must leave the
finding standing rather than suppress it -- the same direction every other
trigger on this rule errs in."""
body = ("def f(self, flag):\n"
" self = self.sudo(flag)\n"
" return self.env['x'].search([])\n")
assert semantics.run_check("sudo_drops_record_rules", body)
def test_an_unstored_framework_field_is_not_a_stored_compute():
"""odoo/models.py adds display_name to every model as
`fields.Char(automatic=True, compute='_compute_display_name', search=...)`
with no store. An override that omits a dependency therefore cannot leave
a stale row, because there is no row.
The checker read "no declaration in this file" as unknown storedness and
kept the finding, which is right for an ordinary field declared in another
module and wrong here: the framework's declaration is knowable and says
not stored. 121 of the 1,051 findings this rule made on odoo core were
this shape.
"""
body = ("class M(models.Model):\n"
" _inherit = 'res.partner'\n"
"\n"
" @api.depends('phone')\n"
" def _compute_display_name(self):\n"
" for rec in self:\n"
" rec.display_name = rec.name + rec.phone\n")
assert semantics.run_check("depends_covers_compute_reads", body) == []
def test_a_model_that_stores_display_name_itself_is_still_checked():
"""The narrowing is about the framework's default, not about the name. A
model that declares `display_name = fields.Char(..., store=True)` in its
own file has opted into a stored row, and all 10 hand-verified true
positives that compute display_name -- every one of them in OpenSPP -- do
exactly that. Losing those is what this test exists to prevent.
"""
body = ("class M(models.Model):\n"
" _name = 'a.b'\n"
"\n"
" display_name = fields.Char(compute='_compute_display_name', store=True)\n"
"\n"
" @api.depends('phone')\n"
" def _compute_display_name(self):\n"
" for rec in self:\n"
" rec.display_name = rec.name + rec.phone\n")
found = semantics.run_check("depends_covers_compute_reads", body)
assert any("'name'" in message for _, message, _ in found), found
def test_a_sql_view_model_has_no_foreign_key_to_decide_about():
"""`_auto = False` means odoo builds no table for the model, so there is
no foreign key, and `ondelete` is not a decision anybody can make about
it. Two of the 15 sampled ondelete findings on odoo core were report
models of exactly this kind: hr.contract.history and
purchase.bill.line.match.
None of the 26 hand-verified true positives for this rule sits on an
_auto = False model, which is what makes the exemption free.
"""
view = ("class R(models.Model):\n"
" _name = 'a.report'\n"
" _auto = False\n"
"\n"
" employee_id = fields.Many2one('hr.employee', readonly=True)\n"
"\n"
" def f(self):\n"
" return self.employee_id.name\n")
assert semantics.run_check("ondelete_leaves_a_dangling_read", view) == []
table = view.replace(" _auto = False\n", "")
assert semantics.run_check("ondelete_leaves_a_dangling_read", table), \
"a real table with the same declaration is still the rule's subject"
def test_sudo_de_escalation_is_recognised_by_keyword_too():
"""odoo's signature is `sudo(self, flag=True)`, so `sudo(flag=False)` is
the same de-escalation as `sudo(False)`. Reading only positional
arguments missed it, and core writes it that way twice."""
body = "def f(self):\n return self.env['res.partner'].sudo(flag=False).search([])\n"
assert semantics.run_check("sudo_drops_record_rules", body) == []
def test_restoring_the_ambient_level_is_not_an_escalation():
"""`x.sudo(self.env.su)` sets su to whatever the environment already has,
so it cannot raise privilege above ambient. It is how a method hands a
record back at the caller's own level, and odoo core writes it beside the
comment "Unsudo the invoice after creation if not already sudoed".
`sudo(self.env.su or field.compute_sudo)` is not the same thing: the `or`
can turn it on, so only the bare attribute reads as ambient.
"""
ambient = "def f(self, invoice):\n invoice = invoice.sudo(self.env.su)\n return invoice\n"
assert semantics.run_check("sudo_drops_record_rules", ambient) == []
may_raise = ("def f(self, rec):\n"
" rec = rec.sudo(self.env.su or self.compute_sudo)\n"
" return rec\n")
assert semantics.run_check("sudo_drops_record_rules", may_raise)
# ------------------------------------------------------ parent_can_form_a_cycle
def _hierarchy(header, field='parent_id = fields.Many2one("my.category", "Parent")'):
return ("from odoo import fields, models\n\n\n"
"class Category(models.Model):\n"
' _name = "my.category"\n' + header +
" " + field + "\n")
def test_a_self_referencing_parent_with_no_guard_is_caught():
"""The rule's subject: spp.dms.directory, which declares a parent_path
Char but never turns _parent_store on, so nothing refuses a loop."""
found = semantics.run_check("parent_can_form_a_cycle", _hierarchy(""))
assert [line for line, _, _ in found] == [6]
assert "my.category" in found[0][1]
def test_parent_store_already_refuses_the_cycle():
"""odoo's _parent_store_update raises "Recursion Detected." on a write that
would close a loop. Six of seven verdicts once classified true were this."""
body = _hierarchy(" _parent_store = True\n")
assert semantics.run_check("parent_can_form_a_cycle", body) == []
def test_parent_store_on_another_chain_leaves_parent_id_bare():
body = _hierarchy(' _parent_store = True\n _parent_name = "location_id"\n')
assert semantics.run_check("parent_can_form_a_cycle", body)
def test_a_parent_on_another_model_cannot_close_a_loop():
"""OCA's account.cash.deposit.line.parent_id points at account.cash.deposit:
an ordinary foreign key, classified false as wrong-construct."""
body = _hierarchy("", 'parent_id = fields.Many2one("my.batch", ondelete="cascade")')
assert semantics.run_check("parent_can_form_a_cycle", body) == []
def test_self_reference_is_read_through_name_and_a_module_constant():
"""spp.area writes Many2one(_name, ...); spp.area.type assigns both _name
and the comodel from one module constant."""
by_name = _hierarchy("", 'parent_id = fields.Many2one(_name, "Parent")')
assert semantics.run_check("parent_can_form_a_cycle", by_name)
shared = ('from odoo import fields, models\n\n_type_model = "my.type"\n\n\n'
"class AreaType(models.Model):\n"
" _name = _type_model\n"
' parent_id = fields.Many2one(_type_model, "Parent")\n')
assert semantics.run_check("parent_can_form_a_cycle", shared)
elsewhere = shared.replace('fields.Many2one(_type_model', 'fields.Many2one("my.zone"')
assert semantics.run_check("parent_can_form_a_cycle", elsewhere) == []
def test_either_guard_clears_the_hierarchy_rule():
for guard in ("_has_cycle", "_check_recursion"):
body = _hierarchy("") + (
"\n @api.constrains('parent_id')\n"
" def _check_parent(self):\n"
" if self.{0}():\n"
" raise ValidationError('loop')\n").format(guard)
assert semantics.run_check("parent_can_form_a_cycle", body) == [], guard
def test_a_chained_name_assignment_is_still_the_models_name():
"""odoo's test models write `_name = _description = 'x'`. Read as nameless,
a parent_id to a different model fired as if it could loop."""
body = ("from odoo import fields, models\n\n\n"
"class Child(models.Model):\n"
" _name = _description = 'my.child'\n"
" parent_id = fields.Many2one('my.parent')\n")
assert semantics.run_check("parent_can_form_a_cycle", body) == []
def test_a_view_and_a_redeclaration_are_not_the_hierarchy_rules_subject():
view = _hierarchy(" _auto = False\n")
assert semantics.run_check("parent_can_form_a_cycle", view) == []
override = _hierarchy("", "parent_id = fields.Many2one(tracking=3)")
assert semantics.run_check("parent_can_form_a_cycle", override) == []
# ------------------------------------------------ editable_record_without_noupdate
PARAMS = """<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_thing_form" model="ir.ui.view">
<field name="name">thing.form</field>
</record>
<record id="retention_days" model="ir.config_parameter">
<field name="key">my.retention_days</field>
<field name="value">90</field>
</record>
<data noupdate="1">
<record id="seq_thing" model="ir.sequence">
<field name="name">Thing</field>
</record>
</data>
</odoo>
"""
def test_an_editable_record_outside_noupdate_is_reported_on_its_own_line():
"""The regex this replaced fired once, on <odoo>, for any record at all --
and one noupdate block anywhere cleared the whole file."""
found = semantics.run_check("editable_record_without_noupdate", PARAMS)
assert [line for line, _, _ in found] == [6]
assert "retention_days" in found[0][1] and "migration" in found[0][1]
def test_a_view_is_not_the_noupdate_rules_subject():
"""Views, menus and actions have to upgrade: core keeps 0-2% of them under
noupdate, so reporting them would be advice nobody should take."""
body = PARAMS.replace('model="ir.config_parameter"', 'model="ir.actions.act_window"')
assert semantics.run_check("editable_record_without_noupdate", body) == []
def test_noupdate_is_inherited_and_can_be_switched_back_off():
root = PARAMS.replace("<odoo>", '<odoo noupdate="1">')
assert semantics.run_check("editable_record_without_noupdate", root) == []
inner = root.replace('<data noupdate="1">', '<data noupdate="0">')
assert [l for l, _, _ in semantics.run_check(
"editable_record_without_noupdate", inner)] == [11]
def test_stages_and_precisions_are_editable_data_too():
for model in ("crm.stage", "decimal.precision", "ir.sequence"):
body = PARAMS.replace('model="ir.config_parameter"', 'model="{0}"'.format(model))
assert semantics.run_check("editable_record_without_noupdate", body), model
def test_a_cron_counts_only_when_it_ships_disabled():
"""57 of 62 cron findings read false: a schedule is usually the mechanism.
The shape that held up ships active=False for an operator to switch on,
which is the decision an upgrade then undoes."""
running = PARAMS.replace('model="ir.config_parameter"', 'model="ir.cron"')
assert semantics.run_check("editable_record_without_noupdate", running) == []
for spelling in ('<field name="active" eval="False"/>',
'<field name="active">0</field>'):
disabled = running.replace('<field name="value">90</field>', spelling)
assert semantics.run_check("editable_record_without_noupdate", disabled), spelling
def test_xml_inside_an_answer_is_still_read():
"""A generated answer carries its XML among prose and Python; the whole text
does not parse, the document inside it does."""
answer = "Add this data file:\n\n" + PARAMS.split("\n", 1)[1] + "\nand then def f(): pass\n"
found = semantics.run_check("editable_record_without_noupdate", answer)
# Line 7 of the answer: two lines of prose, and the declaration dropped.
assert [line for line, _, _ in found] == [7]
def test_check_company_is_not_asked_of_a_sql_view():
"""A SQL view has no table and so no foreign key to guard; OpenSPP's
fund_report was this rule's last live false positive."""
body = ("from odoo import fields, models\n\n\n"
"class FundReport(models.Model):\n"
" _name = 'my.fund.report'\n"
" _auto = False\n"
" company_id = fields.Many2one('res.company')\n"
" journal_id = fields.Many2one('account.journal')\n")
assert semantics.run_check("check_company_missing_on_company_owned_relation", body) == []
table = body.replace(" _auto = False\n", "")
assert semantics.run_check("check_company_missing_on_company_owned_relation", table)
def test_a_field_read_only_to_log_is_not_a_dependency():
"""Five of OpenSPP's live false positives read rec.name only to phrase a
log line. A value read to phrase something decides nothing."""
body = ("from odoo import api, models\n\n\n"
"class Thing(models.Model):\n"
" _name = 'my.thing'\n\n"
" @api.constrains('source_type')\n"
" def _check_source(self):\n"
" for rec in self:\n"
" if rec.source_type == 'external':\n"
" _logger.warning('thing %s has no provider', rec.name)\n")
assert semantics.run_check("constrains_lists_every_field_read", body) == []
decides = body.replace("if rec.source_type == 'external':",
"if rec.source_type == 'external' and rec.name:")
found = semantics.run_check("constrains_lists_every_field_read", decides)
assert found and "'name'" in found[0][1], "a read in the condition still counts"
def test_a_message_collected_before_it_is_raised_is_still_a_message():
"""odoo core's event seats check appends each line to a list and raises
the joined list; event.name only ever phrases it."""
body = ("from odoo import api, models\n\n\n"
"class Event(models.Model):\n"
" _name = 'my.event'\n\n"
" @api.constrains('seats_max')\n"
" def _check_seats(self):\n"
" sold_out = []\n"
" for event in self:\n"
" if event.seats_max < 0:\n"
" sold_out.append(_('%(n)s', n=event.name))\n"
" if sold_out:\n"
" raise ValidationError('\n'.join(sold_out))\n")
assert semantics.run_check("constrains_lists_every_field_read", body) == []
def test_an_override_that_calls_super_adds_triggers_for_the_chain():
"""Odoo merges @api.depends across overrides, so an override that calls
super() is adding triggers for fields the parent's body reads. Three of
core's sampled depends-unread findings were this, the partner avatars
among them."""
body = ("from odoo import api, models\n\n\n"
"class Partner(models.Model):\n"
" _inherit = 'res.partner'\n\n"
" @api.depends('name', 'image_128', 'is_company')\n"
" def _compute_avatar_128(self):\n"
" super()._compute_avatar_128()\n")
assert semantics.run_check("depends_names_unread_fields", body) == []
def test_a_body_that_only_assigns_a_constant_stays_a_finding():
"""Tried as an exemption and withdrawn: on core such bodies were stubs or
deliberate resets, on OpenSPP and OCA three were leftovers classified true
by hand. The file cannot tell which."""
body = ("from odoo import api, fields, models\n\n\n"
"class Field(models.Model):\n"
" _name = 'my.field'\n\n"
" @api.depends('target_type')\n"
" def _compute_target_model(self):\n"
" for record in self:\n"
" record.target_model = 'res.partner'\n")
assert semantics.run_check("depends_names_unread_fields", body)
_API = "from datetime import datetime\nfrom fastapi import APIRouter\n\n\n"
@pytest.mark.parametrize("why, body", [
("a pydantic response built with a naive timestamp: OpenSPP's consent revoke",
_API + "def revoke(cid):\n"
" return ConsentRevokeResponse(consent_id=cid, revoked_at=datetime.utcnow())\n"),
("a returned dict carrying the ISO string: the commit recall credits",
_API + "async def notify():\n"
" return {'status': 'rjct', 'timestamp': datetime.utcnow().isoformat()}\n"),
("an ISO string into a signed document, no HTTP anywhere: l10n_es_edi_tbai",
"from datetime import datetime\n\n\n"
"def sign(self):\n"
" return {'dsig': {'iso_now': datetime.now().isoformat()}}\n"),
("the value reached through a name",
_API + "def status():\n"
" now = datetime.now()\n"
" return StatusResponse(checked_at=now)\n"),
("the server's datetime into a vals key named date: purchase_unreconciled",
"from datetime import datetime\n\n\n"
"def writeoff(self, vals):\n"
" move_date = vals.get('date', datetime.now())\n"
" return self.env['account.move'].create({'date': move_date})\n"),
("the server's date into a domain on a date field",
"from datetime import datetime\n\n\n"
"def due(self):\n"
" return self.search([('date_due', '<', datetime.now().date())])\n"),
("a Date field defaulting to the server's date",
"from datetime import datetime\nfrom odoo import fields, models\n\n\n"
"class Visit(models.Model):\n"
" _name = 'my.visit'\n"
" visit_date = fields.Date(default=lambda self: datetime.now().date())\n"),
])
def test_datetime_reports_the_two_shapes_that_survived(why, body):
assert semantics.run_check("naive_time_leaves_or_dates_a_day", body), why
@pytest.mark.parametrize("why, body", [
("a comparison inside the ORM: naive UTC against naive UTC",
"from datetime import datetime, timedelta\n\n\n"
"def vacuum(self, days):\n"
" deadline = datetime.now() - timedelta(days=days)\n"
" return self.search([('create_date', '<', deadline)])\n"),
("a filename: display only",
"from datetime import datetime\n\n\n"
"def backup(self):\n"
" return self.filename(datetime.now(), ext='zip')\n"),
("a JWT payload: PyJWT reads a naive exp as UTC",
_API + "def token(client):\n"
" now = datetime.utcnow()\n"
" payload = {'iat': now, 'sub': client}\n"
" return jwt.encode(payload, 'k')\n"),
("the offset written by hand after the ISO string",
_API + "def stamp():\n"
" return {'at': f\"{datetime.now().isoformat()}Z\"}\n"),
("a returned dict in a model file, not at an HTTP edge",
"from datetime import datetime\n\n\n"
"def aggregate(self):\n"
" return {'computed_at': datetime.now()}\n"),
("a naive time into a key that may be a Datetime field",
"from datetime import datetime\n\n\n"
"def schedule(self):\n"
" return self.create({'scheduled_date': datetime.now()})\n"),
("the fix at the edge",
"from datetime import datetime, timezone\nfrom fastapi import APIRouter\n\n\n"
"def revoke(cid):\n"
" return ConsentRevokeResponse(revoked_at=datetime.now(timezone.utc))\n"),
])
def test_datetime_leaves_the_shapes_that_did_not(why, body):
assert semantics.run_check("naive_time_leaves_or_dates_a_day", body) == [], why
_LEGACY = ("from odoo import models\n\n\n"
"class ReasonDocument(models.Model):\n"
" _name = 'spp.cr.type.reason.document'\n\n"
" _sql_constraints = [\n"
" ('reason_uniq', 'UNIQUE(cr_type_id, reason)', 'One rule per reason.'),\n"
" ]\n")
def test_sql_constraints_on_19_are_a_constraint_that_does_not_exist():
"""OpenSPP's 208d9758: the block sat on a 19.0 module and created nothing."""
found = semantics.run_check("legacy_sql_constraints_on_19", _LEGACY,
facts={"walked": True, "odoo_series": "19.0"})
assert [(line, grade) for line, _, grade in found] == [(7, None)]
def test_sql_constraints_before_19_are_the_only_spelling():
"""18.0 core declares the attribute in 174 files and has no
models.Constraint, so a finding there is advice that cannot be taken."""
for series in ("16.0", "17.0", "18.0"):
assert semantics.run_check("legacy_sql_constraints_on_19", _LEGACY,
facts={"walked": True, "odoo_series": series}) == []
def test_sql_constraints_series_unknown_after_a_walk_is_silent():
"""odoo core's own manifests pin no series; 174 findings there would all
be wrong."""
assert semantics.run_check("legacy_sql_constraints_on_19", _LEGACY,
facts={"walked": True, "odoo_series": None}) == []
def test_sql_constraints_with_nothing_walked_is_advisory():
"""Nobody looked is not the same answer as looked and it does not say."""
found = semantics.run_check("legacy_sql_constraints_on_19", _LEGACY)
assert [grade for _, _, grade in found] == [semantics.UNPROVEN]
def test_an_empty_sql_constraints_list_declares_nothing():
body = ("from odoo import models\n\n\n"
"class Thing(models.Model):\n"
" _name = 'my.thing'\n"
" _sql_constraints = []\n")
assert semantics.run_check("legacy_sql_constraints_on_19", body,
facts={"walked": True, "odoo_series": "19.0"}) == []
def _sudo_in_method(body_line, params="self"):
return ("from odoo import models\n\n\n"
"class Employee(models.Model):\n"
" _inherit = 'hr.employee'\n\n"
" def f(" + params + "):\n"
" " + body_line + "\n")
@pytest.mark.parametrize("why, line", [
("contracts of the employees the caller holds",
"return self.env['hr.contract'].sudo().search([('employee_id', 'in', self.ids)])"),
("the moves of one journal, reached from self",
"return self.env['account.move'].sudo().search([('journal_id', '=', self.journal_id.id)])"),
("the user's own partner",
"return self.env['loyalty.card'].sudo().search([('partner_id', '=', self.env.user.partner_id.id)])"),
("a subset of self, filtered",
"return self.env['hr.leave'].sudo().search([('employee_id', 'in', self.filtered('active').ids)])"),
])
def test_sudo_tied_to_held_records_is_re_scoped(why, line):
assert semantics.run_check("sudo_drops_record_rules", _sudo_in_method(line)) == [], why
@pytest.mark.parametrize("why, line, params", [
("ids the caller passed in are not records it was shown",
"return self.env['hr.contract'].sudo().search([('employee_id', 'in', employee_ids)])",
"self, employee_ids"),
("browse applies no rule, so it holds whatever it was handed",
"return self.env['hr.contract'].sudo().search([('employee_id', 'in', self.env['hr.employee'].browse(ids).ids)])",
"self, ids"),
("a helper on self may sudo inside; its result is not held",
"return self.env['hr.contract'].sudo().search([('employee_id', 'in', self._others().ids)])",
"self"),
("an ordering against a held value selects everything else: set_sequence_up",
"return self.sudo().search([('website_sequence', '<', self.website_sequence)])",
"self"),
("not in a held set is the opposite of scoping",
"return self.env['hr.contract'].sudo().search([('employee_id', 'not in', self.ids)])",
"self"),
])
def test_sudo_not_tied_to_held_records_stays_a_finding(why, line, params):
assert semantics.run_check("sudo_drops_record_rules", _sudo_in_method(line, params)), why
def _sudo_on_a_page(body, auth="public", website="True"):
return ("from odoo import http\n"
"from odoo.http import request\n\n\n"
"class Customers(http.Controller):\n\n"
" @http.route(['/customers'], type='http', auth=" + repr(auth)
+ ", website=" + website + ")\n"
" def customers(self, search=None, **post):\n"
+ "".join(" " + line + "\n" for line in body))
@pytest.mark.parametrize("why, body", [
("the /customers listing: published partners only",
["return request.env['res.partner'].sudo().search([('website_published', '=', True)])"]),
("built in a name, a search term ORed below the published leaf: website_customer",
["domain = [('website_published', '=', True), ('assigned_partner_id', '!=', False)]",
"if search:",
" domain += ['|', ('name', 'ilike', search), ('website_description', 'ilike', search)]",
"return request.env['res.partner'].sudo().read_group(domain, ['id'], groupby='country_id')"]),
("copied with list() and added to: website_crm_partner_assign",
["base = [('is_company', '=', True), ('website_published', '=', True)]",
"country_domain = list(base)",
"return request.env['res.partner'].sudo().search_count(country_domain + [('country_id', '!=', False)])"]),
])
def test_sudo_published_on_a_public_page_is_re_scoped(why, body):
assert semantics.run_check("sudo_drops_record_rules", _sudo_on_a_page(body)) == [], why
@pytest.mark.parametrize("why, body, auth, website", [
("the published leaf ORed with something else admits unpublished records",
["return request.env['res.partner'].sudo().search(['|', ('website_published', '=', True), ('customer_rank', '>', 0)])"],
"public", "True"),
("an operator glued in front makes the leaf one side of an OR",
["domain = [('website_published', '=', True)]",
"return request.env['res.partner'].sudo().search(['|', ('email', '!=', False)] + domain)"],
"public", "True"),
("insert can put an operator in front of the leaf",
["domain = [('website_published', '=', True), ('email', '!=', False)]",
"domain.insert(0, '|')",
"return request.env['res.partner'].sudo().search(domain)"],
"public", "True"),
("reassigned without the leaf on one path",
["domain = [('website_published', '=', True)]",
"if search:",
" domain = [('name', 'ilike', search)]",
"return request.env['res.partner'].sudo().search(domain)"],
"public", "True"),
("is_published applies no website, and is not the leaf this reads",
["return request.env['res.partner'].sudo().search([('is_published', '=', True)])"],
"public", "True"),
("compared to a value rather than True: set_sequence_up's spelling",
["return request.env['res.partner'].sudo().search([('website_published', '=', post.get('p'))])"],
"public", "True"),
("a route that is not a website page has no current website",
["return request.env['res.partner'].sudo().search([('website_published', '=', True)])"],
"public", "False"),
("a user route is not a public page",
["return request.env['res.partner'].sudo().search([('website_published', '=', True)])"],
"user", "True"),
("a write is not a read of what a page shows",
["return request.env['res.partner'].sudo().search([('website_published', '=', True)]).sudo().write({'x': 1})"],
"public", "True"),
])
def test_sudo_published_not_on_a_public_page_stays_a_finding(why, body, auth, website):
found = semantics.run_check("sudo_drops_record_rules",
_sudo_on_a_page(body, auth, website))
assert found, why
# --------------------------------- a constraint reading a field it cannot name
def _seats(declaration, depends, constrains, compute_name="_compute_seats"):
return ("from odoo import api, fields, models\n\n\n"
"class Event(models.Model):\n"
" _name = 'my.event'\n\n"
" seats_max = fields.Integer()\n"
" seats_taken = fields.Integer()\n"
" seats_available = fields.Integer({0})\n\n"
" @api.depends({1})\n"
" def {3}(self):\n"
" for event in self:\n"
" event.seats_available = event.seats_max - event.seats_taken\n\n"
" @api.constrains({2})\n"
" def _check_seats(self):\n"
" for event in self:\n"
" if event.seats_available < 0:\n"
" raise ValidationError('sold out')\n").format(
declaration, depends, constrains, compute_name)
def test_a_constraint_naming_every_input_of_an_unstored_field_is_covered():
"""Odoo never runs a constraint for a field that is neither stored nor
inversible, and logs "@constrains parameter ... is not writeable" when one
is named. A check reading seats_available that names what it is computed
from runs whenever the value can change, so asking for seats_available was
asking for a name that does nothing. core's event seats are two of the
eight findings that had this shape (ARCHITECTURE 3.82)."""
body = _seats("compute='_compute_seats'", "'seats_max', 'seats_taken'",
"'seats_max', 'seats_taken'")
assert semantics.run_check("constrains_lists_every_field_read", body) == []
def test_a_constraint_missing_an_input_is_told_which_one():
body = _seats("compute='_compute_seats'", "'seats_max', 'seats_taken'",
"'seats_max'")
found = semantics.run_check("constrains_lists_every_field_read", body)
assert len(found) == 1
message = found[0][1]
assert "not stored" in message and "writing 'seats_taken' alone" in message
assert "writing 'seats_available'" not in message, "advice that cannot be taken"
@pytest.mark.parametrize("declaration", [
"compute='_compute_seats', store=True",
"compute='_compute_seats', inverse='_inverse_seats'",
])
def test_a_stored_or_inversible_field_is_still_asked_for(declaration):
"""Stored, a recompute validates it; inversible, a write of it runs the
check. Naming it works either way, so the finding asks for it as before."""
body = _seats(declaration, "'seats_max', 'seats_taken'",
"'seats_max', 'seats_taken'")
found = semantics.run_check("constrains_lists_every_field_read", body)
assert len(found) == 1 and "writing 'seats_available' alone" in found[0][1]
@pytest.mark.parametrize("extra, quiet", [
("", True),
(", readonly=True", True),
(", readonly=False", False),
])
def test_a_related_field_is_reached_through_its_first_field(extra, quiet):
"""A related field is readonly unless it says otherwise, and only then
gets an inverse. Readonly, the relation it starts from is what a write
can touch."""
body = ("from odoo import api, fields, models\n\n\n"
"class Line(models.Model):\n"
" _name = 'my.line'\n\n"
" partner_id = fields.Many2one('res.partner')\n"
" country_code = fields.Char(related='partner_id.country_id.code'"
+ extra + ")\n\n"
" @api.constrains('partner_id')\n"
" def _check_country(self):\n"
" for line in self:\n"
" if line.country_code == 'XX':\n"
" raise ValidationError('no')\n")
found = semantics.run_check("constrains_lists_every_field_read", body)
assert (found == []) is quiet, found
def test_an_unstored_input_is_followed_to_what_it_is_computed_from():
body = _seats("compute='_compute_seats'", "'seats_max', 'seats_reserved'",
"'seats_max'").replace(
" seats_taken = fields.Integer()\n",
" seats_taken = fields.Integer()\n"
" seats_reserved = fields.Integer(compute='_compute_reserved')\n\n"
" @api.depends('registration_ids')\n"
" def _compute_reserved(self):\n"
" pass\n\n")
found = semantics.run_check("constrains_lists_every_field_read", body)
assert len(found) == 1
assert "writing 'registration_ids' alone" in found[0][1]
assert "'seats_reserved'" not in found[0][1].split("--")[1]
def test_an_unstored_field_whose_inputs_cannot_be_read_says_so():
"""The compute lives elsewhere, so what it reads is unknown. The finding
still stands, and says the field itself cannot be named."""
body = _seats("compute='_compute_elsewhere'", "'seats_max', 'seats_taken'",
"'seats_max'")
found = semantics.run_check("constrains_lists_every_field_read", body)
assert len(found) == 1
assert "not stored" in found[0][1]
assert "writing 'seats_available'" not in found[0][1]
|