|
68 | 68 | ApproximateConstantViolation
|
69 | 69 | StringConstantRedefinedViolation
|
70 | 70 | IncorrectExceptOrderViolation
|
| 71 | + FloatKeyViolation |
71 | 72 |
|
72 | 73 | Best practices
|
73 | 74 | --------------
|
|
121 | 122 | .. autoclass:: ApproximateConstantViolation
|
122 | 123 | .. autoclass:: StringConstantRedefinedViolation
|
123 | 124 | .. autoclass:: IncorrectExceptOrderViolation
|
| 125 | +.. autoclass:: FloatKeyViolation |
124 | 126 |
|
125 | 127 | """
|
126 | 128 |
|
@@ -1867,9 +1869,11 @@ class StringConstantRedefinedViolation(ASTViolation):
|
1867 | 1869 | @final
|
1868 | 1870 | class IncorrectExceptOrderViolation(ASTViolation):
|
1869 | 1871 | """
|
1870 |
| - Forbids the use incorrect order of ``except``. |
| 1872 | + Forbids the use of incorrect order of ``except``. |
1871 | 1873 |
|
1872 | 1874 | Note, we only check for built-in exceptions.
|
| 1875 | + Because we cannot statically identify |
| 1876 | + the inheritance order of custom ones. |
1873 | 1877 |
|
1874 | 1878 | Reasoning:
|
1875 | 1879 | Using incorrect order of exceptions is error-prone, since
|
@@ -1905,3 +1909,35 @@ class IncorrectExceptOrderViolation(ASTViolation):
|
1905 | 1909 |
|
1906 | 1910 | error_template = 'Found incorrect exception order'
|
1907 | 1911 | code = 448
|
| 1912 | + |
| 1913 | + |
| 1914 | +@final |
| 1915 | +class FloatKeyViolation(ASTViolation): |
| 1916 | + """ |
| 1917 | + Forbids to define and use ``float`` keys. |
| 1918 | +
|
| 1919 | + Reasoning: |
| 1920 | + ``float`` is a very ugly data type. |
| 1921 | + It has a lot of "precision" errors. |
| 1922 | + When we use ``float`` as keys we can hit this wall. |
| 1923 | + We also cannot use ``float`` keys with lists by design. |
| 1924 | +
|
| 1925 | + Solution: |
| 1926 | + Use other data types: integers, decimals, or use fuzzy logic. |
| 1927 | +
|
| 1928 | + Example:: |
| 1929 | +
|
| 1930 | + # Correct: |
| 1931 | + some = {1: 'a'} |
| 1932 | + some[1] |
| 1933 | +
|
| 1934 | + # Wrong: |
| 1935 | + some = {1.0: 'a'} |
| 1936 | + some[1.0] |
| 1937 | +
|
| 1938 | + .. versionadded:: 0.13.0 |
| 1939 | +
|
| 1940 | + """ |
| 1941 | + |
| 1942 | + error_template = 'Found float used as a key' |
| 1943 | + code = 449 |
0 commit comments