| 1 |
:Release: 1.8 |
| 2 |
:Date: 2017-12-15 |
| 3 |
|
| 4 |
|
| 5 |
Summary -- Release highlights |
| 6 |
============================= |
| 7 |
|
| 8 |
* None so far |
| 9 |
|
| 10 |
New checkers |
| 11 |
============ |
| 12 |
|
| 13 |
* A new check was added, ``shallow-copy-environ``. |
| 14 |
|
| 15 |
This warning message is emitted when shallow copy of os.environ is created. |
| 16 |
Shallow copy of os.environ doesn't work as people may expect. os.environ |
| 17 |
is not a dict object but rather a proxy object, so any changes made |
| 18 |
on copy may have unexpected effects on os.environ |
| 19 |
|
| 20 |
Instead of copy.copy(os.environ) method os.environ.copy() should be used. |
| 21 |
|
| 22 |
See https://bugs.python.org/issue15373 for details. |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
import copy |
| 27 |
import os |
| 28 |
wrong_env_copy = copy.copy(os.environ) # will emit pylint warning |
| 29 |
wrong_env_copy['ENV_VAR'] = 'new_value' # changes os.environ |
| 30 |
assert os.environ['ENV_VAR'] == 'new_value' |
| 31 |
|
| 32 |
good_env_copy = dict(os.environ) # the right way |
| 33 |
good_env_copy['ENV_VAR'] = 'different_value' # doesn't change os.environ |
| 34 |
assert os.environ['ENV_VAR'] == 'new_value' |
| 35 |
|
| 36 |
* A new check was added, ``keyword-arg-before-vararg``. |
| 37 |
|
| 38 |
This warning message is emitted when a function is defined with a keyword |
| 39 |
argument appearing before variable-length positional arguments (\*args). |
| 40 |
This may lead to args list getting modified if keyword argument's value is |
| 41 |
not provided in the function call assuming it will take default value provided |
| 42 |
in the definition. |
| 43 |
|
| 44 |
.. rstcheck: ignore-next-code-block |
| 45 |
.. code-block:: python |
| 46 |
|
| 47 |
def foo(a, b=3, *args): |
| 48 |
print(a, b, args) |
| 49 |
|
| 50 |
# Case1: a=0, b=2, args=(4,5) |
| 51 |
foo(0,2,4,5) # 0 2 (4,5) ==> Observed values are same as expected values |
| 52 |
|
| 53 |
# Case2: a=0, b=<default_value>, args=(4,5) |
| 54 |
foo(0,4,5) # 0 4 (5,) ==> args list got modified as well as the observed value of b |
| 55 |
|
| 56 |
# Case3: Syntax Error if tried as follows |
| 57 |
foo(0,b=2,4,5) # syntax error |
| 58 |
|
| 59 |
* A new check was added, ``simplify-boolean-expression``. |
| 60 |
|
| 61 |
This message is emitted when ``consider-using-ternary`` check would emit |
| 62 |
not equivalent code, due to truthy element being falsy in boolean context. |
| 63 |
|
| 64 |
.. code-block:: python |
| 65 |
|
| 66 |
value = condition and False or other_value |
| 67 |
|
| 68 |
This flawed construct may be simplified to: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
value = other_value |
| 73 |
|
| 74 |
* A new check was added, ``bad-thread-instantiation``. |
| 75 |
|
| 76 |
This message is emitted when the threading.Thread class does not |
| 77 |
receive the target argument, but receives just one argument, which |
| 78 |
is by default the group parameter. |
| 79 |
|
| 80 |
In the following example, the instantiation will fail, which is definitely |
| 81 |
not desired: |
| 82 |
|
| 83 |
.. code-block:: python |
| 84 |
|
| 85 |
import threading |
| 86 |
threading.Thread(lambda: print(1)) # Oups, this is the group parameter |
| 87 |
|
| 88 |
* A new Python 3 checker was added to warn about accessing functions that have been |
| 89 |
removed from the itertools module ``izip``, ``imap``, ``iflter``, ``izip_longest``, and ``ifilterfalse``. |
| 90 |
|
| 91 |
.. code-block:: python |
| 92 |
|
| 93 |
from itertools import izip |
| 94 |
print(list(izip([1, 2], [3]))) |
| 95 |
|
| 96 |
Instead use ``six.moves`` to import a Python 2 and Python 3 compatible function: |
| 97 |
|
| 98 |
.. code-block:: python |
| 99 |
|
| 100 |
from six.moves import zip |
| 101 |
print(list(zip([1, 2], [3]))) |
| 102 |
|
| 103 |
* A new Python 3 checker was added to warn about accessing deprecated fields from |
| 104 |
the types module like ``ListType`` or ``IntType`` |
| 105 |
|
| 106 |
.. code-block:: python |
| 107 |
|
| 108 |
from types import ListType |
| 109 |
print(isinstance([], ListType)) |
| 110 |
|
| 111 |
Instead use the declarations in the builtin namespace: |
| 112 |
|
| 113 |
.. code-block:: python |
| 114 |
|
| 115 |
print(isinstance([], list)) |
| 116 |
|
| 117 |
* A new Python 3 checker was added to warn about declaring a ``next`` method that |
| 118 |
would have implemented the ``Iterator`` protocol in Python 2 but is now a normal |
| 119 |
method in Python 3. |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
class Foo(object): |
| 124 |
def next(self): |
| 125 |
return 42 |
| 126 |
|
| 127 |
Instead implement a ``__next__`` method and use ``six.Iterator`` as a base class |
| 128 |
or alias ``next`` to ``__next__``: |
| 129 |
|
| 130 |
.. code-block:: python |
| 131 |
|
| 132 |
class Foo(object): |
| 133 |
def __next__(self): |
| 134 |
return 42 |
| 135 |
next = __next__ |
| 136 |
|
| 137 |
* Three new Python 3 checkers were added to warn about using dictionary methods |
| 138 |
in non-iterating contexts. |
| 139 |
|
| 140 |
For example, the following are returning iterators in Python 3:: |
| 141 |
|
| 142 |
.. code-block:: python |
| 143 |
|
| 144 |
d = {} |
| 145 |
d.keys()[0] |
| 146 |
d.items()[0] |
| 147 |
d.values() + d.keys() |
| 148 |
|
| 149 |
* A new Python 3 porting check was added, ``non-ascii-bytes-literals`` |
| 150 |
|
| 151 |
This message is emitted whenever we detect that a bytes string contain |
| 152 |
non-ASCII characters, which results in a SyntaxError on Python 3. |
| 153 |
|
| 154 |
* A new warning, ``raising-format-tuple``, will catch situations where the |
| 155 |
intent was likely raising an exception with a formatted message string, |
| 156 |
but the actual code did omit the formatting and instead passes template |
| 157 |
string and value parameters as separate arguments to the exception |
| 158 |
constructor. So it detects things like |
| 159 |
|
| 160 |
.. code-block:: python |
| 161 |
|
| 162 |
raise SomeError('message about %s', foo) |
| 163 |
raise SomeError('message about {}', foo) |
| 164 |
|
| 165 |
which likely were meant instead as |
| 166 |
|
| 167 |
.. code-block:: python |
| 168 |
|
| 169 |
raise SomeError('message about %s' % foo) |
| 170 |
raise SomeError('message about {}'.format(foo)) |
| 171 |
|
| 172 |
This warning can be ignored on projects which deliberately use lazy |
| 173 |
formatting of messages in all user-facing exception handlers. |
| 174 |
|
| 175 |
* Following the recommendations of PEP479_ ,a new Python 3.0 checker was added to warn about raising a ``StopIteration`` inside |
| 176 |
a generator. Raising a ``StopIteration`` inside a generator may be due a direct call |
| 177 |
to ``raise StopIteration``: |
| 178 |
|
| 179 |
.. code-block:: python |
| 180 |
|
| 181 |
def gen_stopiter(): |
| 182 |
yield 1 |
| 183 |
yield 2 |
| 184 |
yield 3 |
| 185 |
raise StopIteration |
| 186 |
|
| 187 |
Instead use a simple ``return`` statement |
| 188 |
|
| 189 |
.. code-block:: python |
| 190 |
|
| 191 |
def gen_stopiter(): |
| 192 |
yield 1 |
| 193 |
yield 2 |
| 194 |
yield 3 |
| 195 |
return |
| 196 |
|
| 197 |
Raising a ``StopIteration`` may also be due to the call to ``next`` function with a generator |
| 198 |
as argument: |
| 199 |
|
| 200 |
.. code-block:: python |
| 201 |
|
| 202 |
def gen_next_raises_stopiter(): |
| 203 |
g = gen_ok() |
| 204 |
while True: |
| 205 |
yield next(g) |
| 206 |
|
| 207 |
In this case, surround the call to ``next`` with a try/except block: |
| 208 |
|
| 209 |
.. code-block:: python |
| 210 |
|
| 211 |
def gen_next_raises_stopiter(): |
| 212 |
g = gen_ok() |
| 213 |
while True: |
| 214 |
try: |
| 215 |
yield next(g) |
| 216 |
except StopIteration: |
| 217 |
return |
| 218 |
|
| 219 |
* The check about raising a StopIteration inside a generator is also valid if the exception |
| 220 |
raised inherit from StopIteration. |
| 221 |
|
| 222 |
Closes #1385 |
| 223 |
|
| 224 |
.. _PEP479: https://peps.python.org/pep-0479 |
| 225 |
|
| 226 |
* A new Python checker was added to warn about using a ``+`` operator inside call of logging methods |
| 227 |
when one of the operands is a literal string: |
| 228 |
|
| 229 |
.. code-block:: python |
| 230 |
|
| 231 |
import logging |
| 232 |
var = "123" |
| 233 |
logging.log(logging.INFO, "Var: " + var) |
| 234 |
|
| 235 |
Instead use formatted string and positional arguments : |
| 236 |
|
| 237 |
.. code-block:: python |
| 238 |
|
| 239 |
import logging |
| 240 |
var = "123" |
| 241 |
logging.log(logging.INFO, "Var: %s", var) |
| 242 |
|
| 243 |
* A new Python checker was added to warn about ``inconsistent-return-statements``. A function or a method |
| 244 |
has inconsistent return statements if it returns both explicit and implicit values : |
| 245 |
|
| 246 |
.. code-block:: python |
| 247 |
|
| 248 |
def mix_implicit_explicit_returns(arg): |
| 249 |
if arg < 10: |
| 250 |
return True |
| 251 |
elif arg < 20: |
| 252 |
return |
| 253 |
|
| 254 |
According to PEP8_, if any return statement returns an expression, |
| 255 |
any return statements where no value is returned should explicitly state this as return None, |
| 256 |
and an explicit return statement should be present at the end of the function (if reachable). |
| 257 |
Thus, the previous function should be written: |
| 258 |
|
| 259 |
.. code-block:: python |
| 260 |
|
| 261 |
def mix_implicit_explicit_returns(arg): |
| 262 |
if arg < 10: |
| 263 |
return True |
| 264 |
elif arg < 20: |
| 265 |
return None |
| 266 |
|
| 267 |
Closes #1267 |
| 268 |
|
| 269 |
.. _PEP8: https://peps.python.org/pep-0008 |
| 270 |
|
| 271 |
Other Changes |
| 272 |
============= |
| 273 |
|
| 274 |
* Fixing u'' string in :ref:`superfluous-parens` message. |
| 275 |
|
| 276 |
* Configuration options of invalid name checker are significantly redesigned. |
| 277 |
Predefined rules for common naming styles were introduced. For typical |
| 278 |
setups, user friendly options like ``--function-naming-style=camelCase`` may |
| 279 |
be used in place of hand-written regular expressions. Default linter config |
| 280 |
enforce PEP8-compatible naming style. See documentation for details. |
| 281 |
|
| 282 |
* Raise meaningful exception in case of invalid reporter class (output format) |
| 283 |
being selected. |
| 284 |
|
| 285 |
* The docparams extension now allows a property docstring to document both |
| 286 |
the property and the setter. Therefore setters can also have no docstring. |
| 287 |
|
| 288 |
* The docparams extension now understands property type syntax. |
| 289 |
|
| 290 |
.. code-block:: python |
| 291 |
|
| 292 |
class Foo(object): |
| 293 |
@property |
| 294 |
def foo(self): |
| 295 |
"""My Sphinx style docstring description. |
| 296 |
|
| 297 |
:type: int |
| 298 |
""" |
| 299 |
return 10 |
| 300 |
|
| 301 |
.. code-block:: python |
| 302 |
|
| 303 |
class Foo(object): |
| 304 |
@property |
| 305 |
def foo(self): |
| 306 |
"""int: My Numpy and Google docstring style description.""" |
| 307 |
return 10 |
| 308 |
|
| 309 |
* In case of ``--output-format=json``, the dictionary returned holds a new key-value pair. |
| 310 |
The key is ``message-id`` and the value the message id. |
| 311 |
|
| 312 |
* Spelling checker has a new configuration parameter ``max-spelling-suggestions``, which |
| 313 |
affects maximum count of suggestions included in emitted message. |
| 314 |
|
| 315 |
* The **:ref:`invalid-name`** check contains the name of the template that caused the failure. |
| 316 |
|
| 317 |
For the given code, **pylint** used to emit ``invalid-name`` in the form ``Invalid constant name var``, |
| 318 |
without offering any context why ``var`` is not such a good name. |
| 319 |
|
| 320 |
With this change, it is now more clear what should be improved for a name to be accepted according to |
| 321 |
its corresponding template. |
| 322 |
|
| 323 |
* New configuration flag, ``suggestion-mode`` was introduced. When enabled, pylint would |
| 324 |
attempt to emit user-friendly suggestions instead of spurious errors for some known |
| 325 |
false-positive scenarios. Flag is enabled by default. |
| 326 |
|
| 327 |
* ``superfluous-parens`` is no longer wrongly emitted for logical statements involving ``in`` operator |
| 328 |
(see example below for what used to be false-positive). |
| 329 |
|
| 330 |
.. code-block:: python |
| 331 |
|
| 332 |
foo = None |
| 333 |
if 'bar' in (foo or {}): |
| 334 |
pass |
| 335 |
|
| 336 |
* Redefinition of dummy function is now possible. ``function-redefined`` message won't be emitted anymore when |
| 337 |
dummy functions are redefined. |
| 338 |
|
| 339 |
* ``missing-param-doc`` and ``missing-type-doc`` are no longer emitted when |
| 340 |
``Args`` and ``Keyword Args`` are mixed in Google docstring. |
| 341 |
|
| 342 |
* Fix of false positive ``useless-super-delegation`` message when |
| 343 |
parameters default values are different from those used in the base class. |
| 344 |
|
| 345 |
* Fix of false positive ``useless-else-on-loop`` message when break statements |
| 346 |
are deeply nested inside loop. |
| 347 |
|
| 348 |
* The Python 3 porting checker no longer emits multiple ``no-absolute-import`` per file. |
| 349 |
|
| 350 |
* The Python 3 porting checker respects disabled checkers found in the config file. |
| 351 |
|
| 352 |
* Modules, classes, or methods consist of compound statements that exceed the ``docstring-min-length`` |
| 353 |
are now correctly emitting ``missing-docstring`` |
| 354 |
|
| 355 |
* Fix no ``wrong-import-order`` message emitted on ordering of first and third party libraries. |
| 356 |
With this fix, pylint distinguishes first and third party modules when checking |
| 357 |
import order. |
| 358 |
|
| 359 |
* Fix the ignored ``pylint disable=fixme`` directives for comments following |
| 360 |
the last statement in a file. |
| 361 |
|
| 362 |
* Fix ``line-too-long`` message deactivated by wrong disable directive. |
| 363 |
The directive ``disable=fixme`` doesn't deactivate anymore the emission |
| 364 |
of ``line-too-long`` message for long commented lines. |
| 365 |
|
| 366 |
* If the rcfile specified on the command line doesn't exist, then an |
| 367 |
IOError exception is raised. |
| 368 |
|
| 369 |
* Fix the wrong scope of ``disable=`` directive after a commented line. |
| 370 |
For example when a ``disable=line-too-long`` directive is at the end of a |
| 371 |
long commented line, it no longer disables the emission of ``line-too-long`` |
| 372 |
message for lines that follow. |