158 <td><a href="#maximum_line_length">maximum_line_length</a></td> |
158 <td><a href="#maximum_line_length">maximum_line_length</a></td> |
159 <td>Limit all lines to a maximum of 79 characters.</td> |
159 <td>Limit all lines to a maximum of 79 characters.</td> |
160 </tr> |
160 </tr> |
161 <tr> |
161 <tr> |
162 <td><a href="#missing_whitespace">missing_whitespace</a></td> |
162 <td><a href="#missing_whitespace">missing_whitespace</a></td> |
163 <td>Each comma, semicolon or colon should be followed by whitespace.</td> |
163 <td>Surround operators with the correct amount of whitespace.</td> |
164 </tr> |
164 </tr> |
165 <tr> |
165 <tr> |
166 <td><a href="#missing_whitespace_after_keyword">missing_whitespace_after_keyword</a></td> |
166 <td><a href="#missing_whitespace_after_keyword">missing_whitespace_after_keyword</a></td> |
167 <td>Keywords should be followed by whitespace.</td> |
167 <td>Keywords should be followed by whitespace.</td> |
168 </tr> |
168 </tr> |
169 <tr> |
169 <tr> |
170 <td><a href="#missing_whitespace_around_operator">missing_whitespace_around_operator</a></td> |
|
171 <td>Surround operators with a single space on either side.</td> |
|
172 </tr> |
|
173 <tr> |
|
174 <td><a href="#module_imports_on_top_of_file">module_imports_on_top_of_file</a></td> |
170 <td><a href="#module_imports_on_top_of_file">module_imports_on_top_of_file</a></td> |
175 <td>Place imports at the top of the file.</td> |
171 <td>Place imports at the top of the file.</td> |
176 </tr> |
172 </tr> |
177 <tr> |
173 <tr> |
178 <td><a href="#mute_string">mute_string</a></td> |
174 <td><a href="#mute_string">mute_string</a></td> |
189 <tr> |
185 <tr> |
190 <td><a href="#process_options">process_options</a></td> |
186 <td><a href="#process_options">process_options</a></td> |
191 <td>Process options passed either via arglist or command line args.</td> |
187 <td>Process options passed either via arglist or command line args.</td> |
192 </tr> |
188 </tr> |
193 <tr> |
189 <tr> |
194 <td><a href="#python_3000_async_await_keywords">python_3000_async_await_keywords</a></td> |
|
195 <td>'async' and 'await' are reserved keywords starting at Python 3.7.</td> |
|
196 </tr> |
|
197 <tr> |
|
198 <td><a href="#python_3000_backticks">python_3000_backticks</a></td> |
|
199 <td>Use repr() instead of backticks in Python 3.</td> |
|
200 </tr> |
|
201 <tr> |
|
202 <td><a href="#python_3000_has_key">python_3000_has_key</a></td> |
|
203 <td>The {}.has_key() method is removed in Python 3: use the 'in' operator.</td> |
|
204 </tr> |
|
205 <tr> |
|
206 <td><a href="#python_3000_invalid_escape_sequence">python_3000_invalid_escape_sequence</a></td> |
190 <td><a href="#python_3000_invalid_escape_sequence">python_3000_invalid_escape_sequence</a></td> |
207 <td>Invalid escape sequences are deprecated in Python 3.6.</td> |
191 <td>Invalid escape sequences are deprecated in Python 3.6.</td> |
208 </tr> |
|
209 <tr> |
|
210 <td><a href="#python_3000_not_equal">python_3000_not_equal</a></td> |
|
211 <td>New code should always use != instead of <>.</td> |
|
212 </tr> |
|
213 <tr> |
|
214 <td><a href="#python_3000_raise_comma">python_3000_raise_comma</a></td> |
|
215 <td>When raising an exception, use "raise ValueError('message')".</td> |
|
216 </tr> |
192 </tr> |
217 <tr> |
193 <tr> |
218 <td><a href="#read_config">read_config</a></td> |
194 <td><a href="#read_config">read_config</a></td> |
219 <td>Read and parse configurations.</td> |
195 <td>Read and parse configurations.</td> |
220 </tr> |
196 </tr> |
1208 <p> |
1184 <p> |
1209 Do not compare types directly. |
1185 Do not compare types directly. |
1210 </p> |
1186 </p> |
1211 <p> |
1187 <p> |
1212 Okay: if isinstance(obj, int): |
1188 Okay: if isinstance(obj, int): |
1213 E721: if type(obj) is type(1): |
1189 Okay: if type(obj) is int: |
1214 </p> |
1190 E721: if type(obj) == type(1): |
1215 <p> |
|
1216 When checking if an object is a string, keep in mind that it might |
|
1217 be a unicode string too! In Python 2.3, str and unicode have a |
|
1218 common base class, basestring, so you can do: |
|
1219 </p> |
|
1220 <p> |
|
1221 Okay: if isinstance(obj, basestring): |
|
1222 Okay: if type(a1) is type(b1): |
|
1223 </p> |
1191 </p> |
1224 <div align="right"><a href="#top">Up</a></div> |
1192 <div align="right"><a href="#top">Up</a></div> |
1225 <hr /> |
1193 <hr /> |
1226 <hr /> |
1194 <hr /> |
1227 <a NAME="compound_statements" ID="compound_statements"></a> |
1195 <a NAME="compound_statements" ID="compound_statements"></a> |
1310 Return the amount of indentation. |
1278 Return the amount of indentation. |
1311 </p> |
1279 </p> |
1312 <p> |
1280 <p> |
1313 Tabs are expanded to the next multiple of 8. |
1281 Tabs are expanded to the next multiple of 8. |
1314 </p> |
1282 </p> |
1315 <p> |
|
1316 >>> expand_indent(' ') |
|
1317 4 |
|
1318 >>> expand_indent('\t') |
|
1319 8 |
|
1320 >>> expand_indent(' \t') |
|
1321 8 |
|
1322 >>> expand_indent(' \t') |
|
1323 16 |
|
1324 </p> |
|
1325 <div align="right"><a href="#top">Up</a></div> |
1283 <div align="right"><a href="#top">Up</a></div> |
1326 <hr /> |
1284 <hr /> |
1327 <hr /> |
1285 <hr /> |
1328 <a NAME="explicit_line_join" ID="explicit_line_join"></a> |
1286 <a NAME="explicit_line_join" ID="explicit_line_join"></a> |
1329 <h2>explicit_line_join</h2> |
1287 <h2>explicit_line_join</h2> |
1500 <div align="right"><a href="#top">Up</a></div> |
1458 <div align="right"><a href="#top">Up</a></div> |
1501 <hr /> |
1459 <hr /> |
1502 <hr /> |
1460 <hr /> |
1503 <a NAME="missing_whitespace" ID="missing_whitespace"></a> |
1461 <a NAME="missing_whitespace" ID="missing_whitespace"></a> |
1504 <h2>missing_whitespace</h2> |
1462 <h2>missing_whitespace</h2> |
1505 <b>missing_whitespace</b>(<i>logical_line</i>) |
1463 <b>missing_whitespace</b>(<i>logical_line, tokens</i>) |
1506 |
1464 |
1507 <p> |
1465 <p> |
1508 Each comma, semicolon or colon should be followed by whitespace. |
1466 Surround operators with the correct amount of whitespace. |
1509 </p> |
1467 </p> |
1510 <p> |
1468 <p> |
|
1469 - Always surround these binary operators with a single space on |
|
1470 either side: assignment (=), augmented assignment (+=, -= etc.), |
|
1471 comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), |
|
1472 Booleans (and, or, not). |
|
1473 </p> |
|
1474 <p> |
|
1475 - Each comma, semicolon or colon should be followed by whitespace. |
|
1476 </p> |
|
1477 <p> |
|
1478 - If operators with different priorities are used, consider adding |
|
1479 whitespace around the operators with the lowest priorities. |
|
1480 </p> |
|
1481 <p> |
|
1482 Okay: i = i + 1 |
|
1483 Okay: submitted += 1 |
|
1484 Okay: x = x * 2 - 1 |
|
1485 Okay: hypot2 = x * x + y * y |
|
1486 Okay: c = (a + b) * (a - b) |
|
1487 Okay: foo(bar, key='word', *args, **kwargs) |
|
1488 Okay: alpha[:-i] |
1511 Okay: [a, b] |
1489 Okay: [a, b] |
1512 Okay: (3,) |
1490 Okay: (3,) |
1513 Okay: a[3,] = 1 |
1491 Okay: a[3,] = 1 |
1514 Okay: a[1:4] |
1492 Okay: a[1:4] |
1515 Okay: a[:4] |
1493 Okay: a[:4] |
1516 Okay: a[1:] |
1494 Okay: a[1:] |
1517 Okay: a[1:4:2] |
1495 Okay: a[1:4:2] |
1518 E231: ['a','b'] |
|
1519 E231: foo(bar,baz) |
|
1520 E231: [{'a':'b'}] |
|
1521 </p> |
|
1522 <div align="right"><a href="#top">Up</a></div> |
|
1523 <hr /> |
|
1524 <hr /> |
|
1525 <a NAME="missing_whitespace_after_keyword" ID="missing_whitespace_after_keyword"></a> |
|
1526 <h2>missing_whitespace_after_keyword</h2> |
|
1527 <b>missing_whitespace_after_keyword</b>(<i>logical_line, tokens</i>) |
|
1528 |
|
1529 <p> |
|
1530 Keywords should be followed by whitespace. |
|
1531 </p> |
|
1532 <p> |
|
1533 Okay: from foo import (bar, baz) |
|
1534 E275: from foo import(bar, baz) |
|
1535 E275: from importable.module import(bar, baz) |
|
1536 E275: if(foo): bar |
|
1537 </p> |
|
1538 <div align="right"><a href="#top">Up</a></div> |
|
1539 <hr /> |
|
1540 <hr /> |
|
1541 <a NAME="missing_whitespace_around_operator" ID="missing_whitespace_around_operator"></a> |
|
1542 <h2>missing_whitespace_around_operator</h2> |
|
1543 <b>missing_whitespace_around_operator</b>(<i>logical_line, tokens</i>) |
|
1544 |
|
1545 <p> |
|
1546 Surround operators with a single space on either side. |
|
1547 </p> |
|
1548 <p> |
|
1549 - Always surround these binary operators with a single space on |
|
1550 either side: assignment (=), augmented assignment (+=, -= etc.), |
|
1551 comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), |
|
1552 Booleans (and, or, not). |
|
1553 </p> |
|
1554 <p> |
|
1555 - If operators with different priorities are used, consider adding |
|
1556 whitespace around the operators with the lowest priorities. |
|
1557 </p> |
|
1558 <p> |
|
1559 Okay: i = i + 1 |
|
1560 Okay: submitted += 1 |
|
1561 Okay: x = x * 2 - 1 |
|
1562 Okay: hypot2 = x * x + y * y |
|
1563 Okay: c = (a + b) * (a - b) |
|
1564 Okay: foo(bar, key='word', *args, **kwargs) |
|
1565 Okay: alpha[:-i] |
|
1566 </p> |
1496 </p> |
1567 <p> |
1497 <p> |
1568 E225: i=i+1 |
1498 E225: i=i+1 |
1569 E225: submitted +=1 |
1499 E225: submitted +=1 |
1570 E225: x = x /2 - 1 |
1500 E225: x = x /2 - 1 |
1572 E225: z = 1and 1 |
1502 E225: z = 1and 1 |
1573 E226: c = (a+b) * (a-b) |
1503 E226: c = (a+b) * (a-b) |
1574 E226: hypot2 = x*x + y*y |
1504 E226: hypot2 = x*x + y*y |
1575 E227: c = a|b |
1505 E227: c = a|b |
1576 E228: msg = fmt%(errno, errmsg) |
1506 E228: msg = fmt%(errno, errmsg) |
|
1507 E231: ['a','b'] |
|
1508 E231: foo(bar,baz) |
|
1509 E231: [{'a':'b'}] |
|
1510 </p> |
|
1511 <div align="right"><a href="#top">Up</a></div> |
|
1512 <hr /> |
|
1513 <hr /> |
|
1514 <a NAME="missing_whitespace_after_keyword" ID="missing_whitespace_after_keyword"></a> |
|
1515 <h2>missing_whitespace_after_keyword</h2> |
|
1516 <b>missing_whitespace_after_keyword</b>(<i>logical_line, tokens</i>) |
|
1517 |
|
1518 <p> |
|
1519 Keywords should be followed by whitespace. |
|
1520 </p> |
|
1521 <p> |
|
1522 Okay: from foo import (bar, baz) |
|
1523 E275: from foo import(bar, baz) |
|
1524 E275: from importable.module import(bar, baz) |
|
1525 E275: if(foo): bar |
1577 </p> |
1526 </p> |
1578 <div align="right"><a href="#top">Up</a></div> |
1527 <div align="right"><a href="#top">Up</a></div> |
1579 <hr /> |
1528 <hr /> |
1580 <hr /> |
1529 <hr /> |
1581 <a NAME="module_imports_on_top_of_file" ID="module_imports_on_top_of_file"></a> |
1530 <a NAME="module_imports_on_top_of_file" ID="module_imports_on_top_of_file"></a> |
1592 <p> |
1541 <p> |
1593 Okay: import os |
1542 Okay: import os |
1594 Okay: # this is a comment\nimport os |
1543 Okay: # this is a comment\nimport os |
1595 Okay: '''this is a module docstring'''\nimport os |
1544 Okay: '''this is a module docstring'''\nimport os |
1596 Okay: r'''this is a module docstring'''\nimport os |
1545 Okay: r'''this is a module docstring'''\nimport os |
1597 Okay: |
|
1598 try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y |
|
1599 Okay: |
|
1600 try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y |
|
1601 E402: a=1\nimport os |
1546 E402: a=1\nimport os |
1602 E402: 'One string'\n"Two string"\nimport os |
1547 E402: 'One string'\n"Two string"\nimport os |
1603 E402: a=1\nfrom sys import x |
1548 E402: a=1\nfrom sys import x |
1604 </p> |
1549 </p> |
1605 <p> |
1550 <p> |
1661 flake8 to specify their own options to be processed in pycodestyle. |
1598 flake8 to specify their own options to be processed in pycodestyle. |
1662 </p> |
1599 </p> |
1663 <div align="right"><a href="#top">Up</a></div> |
1600 <div align="right"><a href="#top">Up</a></div> |
1664 <hr /> |
1601 <hr /> |
1665 <hr /> |
1602 <hr /> |
1666 <a NAME="python_3000_async_await_keywords" ID="python_3000_async_await_keywords"></a> |
|
1667 <h2>python_3000_async_await_keywords</h2> |
|
1668 <b>python_3000_async_await_keywords</b>(<i>logical_line, tokens</i>) |
|
1669 |
|
1670 <p> |
|
1671 'async' and 'await' are reserved keywords starting at Python 3.7. |
|
1672 </p> |
|
1673 <p> |
|
1674 W606: async = 42 |
|
1675 W606: await = 42 |
|
1676 Okay: async def read(db):\n data = await db.fetch('SELECT ...') |
|
1677 </p> |
|
1678 <div align="right"><a href="#top">Up</a></div> |
|
1679 <hr /> |
|
1680 <hr /> |
|
1681 <a NAME="python_3000_backticks" ID="python_3000_backticks"></a> |
|
1682 <h2>python_3000_backticks</h2> |
|
1683 <b>python_3000_backticks</b>(<i>logical_line</i>) |
|
1684 |
|
1685 <p> |
|
1686 Use repr() instead of backticks in Python 3. |
|
1687 </p> |
|
1688 <p> |
|
1689 Okay: val = repr(1 + 2) |
|
1690 W604: val = `1 + 2` |
|
1691 </p> |
|
1692 <div align="right"><a href="#top">Up</a></div> |
|
1693 <hr /> |
|
1694 <hr /> |
|
1695 <a NAME="python_3000_has_key" ID="python_3000_has_key"></a> |
|
1696 <h2>python_3000_has_key</h2> |
|
1697 <b>python_3000_has_key</b>(<i>logical_line, noqa</i>) |
|
1698 |
|
1699 <p> |
|
1700 The {}.has_key() method is removed in Python 3: use the 'in' |
|
1701 operator. |
|
1702 </p> |
|
1703 <p> |
|
1704 Okay: if "alph" in d:\n print d["alph"] |
|
1705 W601: assert d.has_key('alph') |
|
1706 </p> |
|
1707 <div align="right"><a href="#top">Up</a></div> |
|
1708 <hr /> |
|
1709 <hr /> |
|
1710 <a NAME="python_3000_invalid_escape_sequence" ID="python_3000_invalid_escape_sequence"></a> |
1603 <a NAME="python_3000_invalid_escape_sequence" ID="python_3000_invalid_escape_sequence"></a> |
1711 <h2>python_3000_invalid_escape_sequence</h2> |
1604 <h2>python_3000_invalid_escape_sequence</h2> |
1712 <b>python_3000_invalid_escape_sequence</b>(<i>logical_line, tokens, noqa</i>) |
1605 <b>python_3000_invalid_escape_sequence</b>(<i>logical_line, tokens, noqa</i>) |
1713 |
1606 |
1714 <p> |
1607 <p> |
1715 Invalid escape sequences are deprecated in Python 3.6. |
1608 Invalid escape sequences are deprecated in Python 3.6. |
1716 </p> |
1609 </p> |
1717 <p> |
1610 <p> |
1718 Okay: regex = r'\.png$' |
1611 Okay: regex = r'\.png$' |
1719 W605: regex = '\.png$' |
1612 W605: regex = '\.png$' |
1720 </p> |
|
1721 <div align="right"><a href="#top">Up</a></div> |
|
1722 <hr /> |
|
1723 <hr /> |
|
1724 <a NAME="python_3000_not_equal" ID="python_3000_not_equal"></a> |
|
1725 <h2>python_3000_not_equal</h2> |
|
1726 <b>python_3000_not_equal</b>(<i>logical_line</i>) |
|
1727 |
|
1728 <p> |
|
1729 New code should always use != instead of <>. |
|
1730 </p> |
|
1731 <p> |
|
1732 The older syntax is removed in Python 3. |
|
1733 </p> |
|
1734 <p> |
|
1735 Okay: if a != 'no': |
|
1736 W603: if a <> 'no': |
|
1737 </p> |
|
1738 <div align="right"><a href="#top">Up</a></div> |
|
1739 <hr /> |
|
1740 <hr /> |
|
1741 <a NAME="python_3000_raise_comma" ID="python_3000_raise_comma"></a> |
|
1742 <h2>python_3000_raise_comma</h2> |
|
1743 <b>python_3000_raise_comma</b>(<i>logical_line</i>) |
|
1744 |
|
1745 <p> |
|
1746 When raising an exception, use "raise ValueError('message')". |
|
1747 </p> |
|
1748 <p> |
|
1749 The older form is removed in Python 3. |
|
1750 </p> |
|
1751 <p> |
|
1752 Okay: raise DummyError("Message") |
|
1753 W602: raise DummyError, "Message" |
|
1754 </p> |
1613 </p> |
1755 <div align="right"><a href="#top">Up</a></div> |
1614 <div align="right"><a href="#top">Up</a></div> |
1756 <hr /> |
1615 <hr /> |
1757 <hr /> |
1616 <hr /> |
1758 <a NAME="read_config" ID="read_config"></a> |
1617 <a NAME="read_config" ID="read_config"></a> |