| 1 |
:Release: 1.7 |
| 2 |
:Date: 2017-04-13 |
| 3 |
|
| 4 |
Summary -- Release highlights |
| 5 |
============================= |
| 6 |
|
| 7 |
New checkers |
| 8 |
============ |
| 9 |
|
| 10 |
* ``single-string-used-for-slots`` check was added, which is used |
| 11 |
whenever a class is using a single string as a slot value. While this |
| 12 |
is technically not a problem per se, it might trip users when manipulating |
| 13 |
the slots value as an iterable, which would in turn iterate over characters |
| 14 |
of the slot value. In order to be more straight-forward, always try to use |
| 15 |
a container such as a list or a tuple for defining slot values. |
| 16 |
|
| 17 |
* We added a new check, ``literal-comparison``, which is used |
| 18 |
whenever **pylint** can detect a comparison to a literal. This is usually |
| 19 |
not what we want and, potentially, error prone. For instance, in the given example, |
| 20 |
the first string comparison returns true, since smaller strings are interned |
| 21 |
by the interpreter, while for larger ones, it will return False: |
| 22 |
|
| 23 |
.. rstcheck: ignore-next-code-block |
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
mystring = "ok" |
| 27 |
if mystring is "ok": # Returns true |
| 28 |
... |
| 29 |
|
| 30 |
mystring = "a" * 1000 |
| 31 |
if mystring is ("a" * 1000): # This will return False |
| 32 |
... |
| 33 |
|
| 34 |
Instead of using the ``is`` operator, you should use the ``==`` operator for |
| 35 |
this use case. |
| 36 |
|
| 37 |
|
| 38 |
* We added a new refactoring message, ``consider-merging-isinstance``, which is |
| 39 |
emitted whenever we can detect that consecutive *isinstance* calls can be merged |
| 40 |
together. |
| 41 |
For instance, in this example, we can merge the first two *isinstance* calls: |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
# $ cat a.py |
| 46 |
if isinstance(x, int) or isinstance(x, float): |
| 47 |
pass |
| 48 |
if isinstance(x, (int, float)) or isinstance(x, str): |
| 49 |
pass |
| 50 |
# $ pylint a.py |
| 51 |
# R: 1, 0: Consider merging these isinstance calls to isinstance(x, (float, int)) (consider-merging-isinstance) |
| 52 |
# R: 3, 0: Consider merging these isinstance calls to isinstance(x, (int, float, str)) (consider-merging-isinstance) |
| 53 |
|
| 54 |
* A new error check was added, ``invalid-metaclass``, which is used whenever *pylint* |
| 55 |
can detect that a given class is using a metaclass which is invalid for the purpose |
| 56 |
of the class. This usually might indicate a problem in the code, rather than |
| 57 |
something done on purpose. |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
# Needs to inherit from *type* in order to be valid |
| 62 |
class SomeClass(object): |
| 63 |
... |
| 64 |
|
| 65 |
class MyClass(metaclass=SomeClass): |
| 66 |
pass |
| 67 |
|
| 68 |
* A new warning was added, ``useless-super-delegation``, which is used whenever |
| 69 |
we can detect that an overridden method is useless, relying on *super()* delegation |
| 70 |
to do the same thing as another method from the MRO. |
| 71 |
|
| 72 |
For instance, in this example, the first two methods are useless, since they |
| 73 |
do the exact same thing as the methods from the base classes, while the next |
| 74 |
two methods are not, since they do some extra operations with the passed |
| 75 |
arguments. |
| 76 |
|
| 77 |
.. code-block:: python |
| 78 |
|
| 79 |
class Impl(Base): |
| 80 |
|
| 81 |
def __init__(self, param1, param2): |
| 82 |
super(Impl, self).__init__(param1, param2) |
| 83 |
|
| 84 |
def useless(self, first, second): |
| 85 |
return super(Impl, self).useless(first, second) |
| 86 |
|
| 87 |
def not_useless(self, first, **kwargs): |
| 88 |
debug = kwargs.pop('debug', False) |
| 89 |
if debug: |
| 90 |
... |
| 91 |
return super(Impl, self).not_useless(first, **kwargs) |
| 92 |
|
| 93 |
def not_useless_1(self, first, *args): |
| 94 |
return super(Impl, self).not_useless_1(first + some_value, *args) |
| 95 |
|
| 96 |
* A new warning was added, ``len-as-condition``, which is used whenever |
| 97 |
we detect that a condition uses ``len(SEQUENCE)`` incorrectly. Instead |
| 98 |
one could use ``if SEQUENCE`` or ``if not SEQUENCE``. |
| 99 |
|
| 100 |
For instance, all of the examples below: |
| 101 |
|
| 102 |
.. code-block:: python |
| 103 |
|
| 104 |
if len(S): |
| 105 |
pass |
| 106 |
|
| 107 |
if not len(S): |
| 108 |
pass |
| 109 |
|
| 110 |
if len(S) > 0: |
| 111 |
pass |
| 112 |
|
| 113 |
if len(S) != 0: |
| 114 |
pass |
| 115 |
|
| 116 |
if len(S) == 0: |
| 117 |
pass |
| 118 |
|
| 119 |
can be written in a more natural way: |
| 120 |
|
| 121 |
.. code-block:: python |
| 122 |
|
| 123 |
if S: |
| 124 |
pass |
| 125 |
|
| 126 |
if not S: |
| 127 |
pass |
| 128 |
|
| 129 |
See https://peps.python.org/pep-0008/#programming-recommendations |
| 130 |
for more information. |
| 131 |
|
| 132 |
* A new extension was added, ``emptystring.py`` which detects whenever |
| 133 |
we detect comparisons to empty string constants. This extension is disabled |
| 134 |
by default. For instance, the examples below: |
| 135 |
|
| 136 |
.. code-block:: python |
| 137 |
|
| 138 |
if S != "": |
| 139 |
pass |
| 140 |
|
| 141 |
if S == '': |
| 142 |
pass |
| 143 |
|
| 144 |
can be written in a more natural way: |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
if S: |
| 149 |
pass |
| 150 |
|
| 151 |
if not S: |
| 152 |
pass |
| 153 |
|
| 154 |
An exception to this is when empty string is an allowed value whose meaning |
| 155 |
is treated differently than ``None``. For example the meaning could be |
| 156 |
user selected no additional options vs. user has not made their selection yet! |
| 157 |
|
| 158 |
You can activate this checker by adding the line:: |
| 159 |
|
| 160 |
load-plugins=pylint.extensions.emptystring |
| 161 |
|
| 162 |
to the ``MAIN`` section of your ``.pylintrc`` or using the command:: |
| 163 |
|
| 164 |
$ pylint a.py --load-plugins=pylint.extensions.emptystring |
| 165 |
|
| 166 |
* A new extension was added, ``comparetozero.py`` which detects whenever |
| 167 |
we compare integers to zero. This extension is disabled by default. |
| 168 |
For instance, the examples below: |
| 169 |
|
| 170 |
.. code-block:: python |
| 171 |
|
| 172 |
if X != 0: |
| 173 |
pass |
| 174 |
|
| 175 |
if X == 0: |
| 176 |
pass |
| 177 |
|
| 178 |
can be written in a more natural way: |
| 179 |
|
| 180 |
.. code-block:: python |
| 181 |
|
| 182 |
if X: |
| 183 |
pass |
| 184 |
|
| 185 |
if not X: |
| 186 |
pass |
| 187 |
|
| 188 |
An exception to this is when zero is an allowed value whose meaning |
| 189 |
is treated differently than ``None``. For example the meaning could be |
| 190 |
``None`` means no limit, while ``0`` means the limit it zero! |
| 191 |
|
| 192 |
You can activate this checker by adding the line:: |
| 193 |
|
| 194 |
load-plugins=pylint.extensions.comparetozero |
| 195 |
|
| 196 |
to the ``MAIN`` section of your ``.pylintrc`` or using the command:: |
| 197 |
|
| 198 |
$ pylint a.py --load-plugins=pylint.extensions.comparetozero |
| 199 |
|
| 200 |
* We've added new error conditions for ``bad-super-call`` which now detect |
| 201 |
the usage of ``super(type(self), self)`` and ``super(self.__class__, self)`` |
| 202 |
patterns. These can lead to recursion loop in derived classes. The problem |
| 203 |
is visible only if you override a class that uses these incorrect invocations |
| 204 |
of ``super()``. |
| 205 |
|
| 206 |
For instance, ``Derived.__init__()`` will correctly call ``Base.__init__``. |
| 207 |
At this point ``type(self)`` will be equal to ``Derived`` and the call again |
| 208 |
goes to ``Base.__init__`` and we enter a recursion loop. |
| 209 |
|
| 210 |
.. code-block:: python |
| 211 |
|
| 212 |
class Base(object): |
| 213 |
def __init__(self, param1, param2): |
| 214 |
super(type(self), self).__init__(param1, param2) |
| 215 |
|
| 216 |
class Derived(Base): |
| 217 |
def __init__(self, param1, param2): |
| 218 |
super(Derived, self).__init__(param1, param2) |
| 219 |
|
| 220 |
* The warnings ``missing-returns-doc`` and ``missing-yields-doc`` have each |
| 221 |
been replaced with two new warnings - ``missing-[return|yield]-doc`` and |
| 222 |
``missing-[return|yield]-type-doc``. Having these as separate warnings |
| 223 |
allows the user to choose whether their documentation style requires |
| 224 |
text descriptions of function return/yield, specification of return/yield |
| 225 |
types, or both. |
| 226 |
|
| 227 |
.. code-block:: python |
| 228 |
|
| 229 |
# This will raise missing-return-type-doc but not missing-return-doc |
| 230 |
def my_sphinx_style_func(self): |
| 231 |
"""This is a Sphinx-style docstring. |
| 232 |
|
| 233 |
:returns: Always False |
| 234 |
""" |
| 235 |
return False |
| 236 |
|
| 237 |
# This will raise missing-return-doc but not missing-return-type-doc |
| 238 |
def my_google_style_func(self): |
| 239 |
"""This is a Google-style docstring. |
| 240 |
|
| 241 |
Returns: |
| 242 |
bool: |
| 243 |
""" |
| 244 |
return False |
| 245 |
|
| 246 |
* A new refactoring check was added, ``redefined-argument-from-local``, which is |
| 247 |
emitted when **pylint** can detect than a function argument is redefined locally |
| 248 |
in some potential error prone cases. For instance, in the following piece of code, |
| 249 |
we have a bug, since the check will never return ``True``, given the fact that we |
| 250 |
are comparing the same object to its attributes. |
| 251 |
|
| 252 |
.. code-block:: python |
| 253 |
|
| 254 |
def test(resource): |
| 255 |
for resource in resources: |
| 256 |
# The ``for`` is reusing ``resource``, which means that the following |
| 257 |
# ``resource`` is not what we wanted to check against. |
| 258 |
if resource.resource_type == resource: |
| 259 |
call_resource(resource) |
| 260 |
|
| 261 |
Other places where this check looks are *with* statement name bindings and |
| 262 |
except handler's name binding. |
| 263 |
|
| 264 |
* A new refactoring check was added, ``no-else-return``, which is |
| 265 |
emitted when pylint encounters an else following a chain of ifs, |
| 266 |
all of them containing a return statement. |
| 267 |
|
| 268 |
.. code-block:: python |
| 269 |
|
| 270 |
def foo1(x, y, z): |
| 271 |
if x: |
| 272 |
return y |
| 273 |
else: # This is unnecessary here. |
| 274 |
return z |
| 275 |
|
| 276 |
|
| 277 |
We could fix it deleting the ``else`` statement. |
| 278 |
|
| 279 |
.. code-block:: python |
| 280 |
|
| 281 |
def foo1(x, y, z): |
| 282 |
if x: |
| 283 |
return y |
| 284 |
return z |
| 285 |
|
| 286 |
* A new Python 3 check was added, ``eq-without-hash``, which enforces classes that implement |
| 287 |
``__eq__`` *also* implement ``__hash__``. The behavior around classes which implement ``__eq__`` |
| 288 |
but not ``__hash__`` changed in Python 3; in Python 2 such classes would get ``object.__hash__`` |
| 289 |
as their default implementation. In Python 3, aforementioned classes get ``None`` as their |
| 290 |
implementation thus making them unhashable. |
| 291 |
|
| 292 |
.. code-block:: python |
| 293 |
|
| 294 |
class JustEq(object): |
| 295 |
def __init__(self, x): |
| 296 |
self.x = x |
| 297 |
|
| 298 |
def __eq__(self, other): |
| 299 |
return self.x == other.x |
| 300 |
|
| 301 |
class Neither(object): |
| 302 |
def __init__(self, x): |
| 303 |
self.x = x |
| 304 |
|
| 305 |
class HashAndEq(object): |
| 306 |
def __init__(self, x): |
| 307 |
self.x = x |
| 308 |
|
| 309 |
def __eq__(self, other): |
| 310 |
return self.x == other.x |
| 311 |
|
| 312 |
def __hash__(self): |
| 313 |
return hash(self.x) |
| 314 |
|
| 315 |
{Neither(1), Neither(2)} # OK in Python 2 and Python 3 |
| 316 |
{HashAndEq(1), HashAndEq(2)} # OK in Python 2 and Python 3 |
| 317 |
{JustEq(1), JustEq(2)} # Works in Python 2, throws in Python 3 |
| 318 |
|
| 319 |
|
| 320 |
In general, this is a poor practice which motivated the behavior change. |
| 321 |
|
| 322 |
.. code-block:: python |
| 323 |
|
| 324 |
as_set = {JustEq(1), JustEq(2)} |
| 325 |
print(JustEq(1) in as_set) # prints False |
| 326 |
print(JustEq(1) in list(as_set)) # prints True |
| 327 |
|
| 328 |
|
| 329 |
In order to fix this error and avoid behavior differences between Python 2 and Python 3, classes |
| 330 |
should either explicitly set ``__hash__`` to ``None`` or implement a hashing function. |
| 331 |
|
| 332 |
.. code-block:: python |
| 333 |
|
| 334 |
class JustEq(object): |
| 335 |
def __init__(self, x): |
| 336 |
self.x = x |
| 337 |
|
| 338 |
def __eq__(self, other): |
| 339 |
return self.x == other.x |
| 340 |
|
| 341 |
__hash__ = None |
| 342 |
|
| 343 |
{JustEq(1), JustEq(2)} # Now throws an exception in both Python 2 and Python 3. |
| 344 |
|
| 345 |
* 3 new Python 3 checkers were added, ``div-method``, ``idiv-method`` and ``rdiv-method``. |
| 346 |
The magic methods ``__div__`` and ``__idiv__`` have been phased out in Python 3 in favor |
| 347 |
of ``__truediv__``. Classes implementing ``__div__`` that still need to be used from Python |
| 348 |
2 code not using ``from __future__ import division`` should implement ``__truediv__`` and |
| 349 |
alias ``__div__`` to that implementation. |
| 350 |
|
| 351 |
.. code-block:: python |
| 352 |
|
| 353 |
from __future__ import division |
| 354 |
|
| 355 |
class DivisibleThing(object): |
| 356 |
def __init__(self, x): |
| 357 |
self.x = x |
| 358 |
|
| 359 |
def __truediv__(self, other): |
| 360 |
return DivisibleThing(self.x / other.x) |
| 361 |
|
| 362 |
__div__ = __truediv__ |
| 363 |
|
| 364 |
* A new Python 3 checker was added to warn about accessing the ``message`` attribute on |
| 365 |
Exceptions. The message attribute was deprecated in Python 2.7 and was removed in Python 3. |
| 366 |
See https://peps.python.org/pep-0352/#retracted-ideas for more information. |
| 367 |
|
| 368 |
.. code-block:: python |
| 369 |
|
| 370 |
try: |
| 371 |
raise Exception("Oh No!!") |
| 372 |
except Exception as e: |
| 373 |
print(e.message) |
| 374 |
|
| 375 |
Instead of relying on the ``message`` attribute, you should explicitly cast the exception to a |
| 376 |
string: |
| 377 |
|
| 378 |
.. code-block:: python |
| 379 |
|
| 380 |
try: |
| 381 |
raise Exception("Oh No!!") |
| 382 |
except Exception as e: |
| 383 |
print(str(e)) |
| 384 |
|
| 385 |
|
| 386 |
* A new Python 3 checker was added to warn about using ``encode`` or ``decode`` on strings |
| 387 |
with non-text codecs. This check also checks calls to ``open`` with the keyword argument |
| 388 |
``encoding``. See https://docs.python.org/3/whatsnew/3.4.html#improvements-to-codec-handling |
| 389 |
for more information. |
| 390 |
|
| 391 |
.. code-block:: python |
| 392 |
|
| 393 |
'hello world'.encode('hex') |
| 394 |
|
| 395 |
Instead of using the ``encode`` method for non-text codecs use the ``codecs`` module. |
| 396 |
|
| 397 |
.. code-block:: python |
| 398 |
|
| 399 |
import codecs |
| 400 |
codecs.encode('hello world', 'hex') |
| 401 |
|
| 402 |
|
| 403 |
* A new warning was added, ``overlapping-except``, which is emitted |
| 404 |
when an except handler treats two exceptions which are *overlapping*. |
| 405 |
This means that one exception is an ancestor of the other one or it is |
| 406 |
just an alias. |
| 407 |
|
| 408 |
For example, in Python 3.3+, IOError is an alias for OSError. In addition, socket.error is |
| 409 |
an alias for OSError. The intention is to find cases like the following: |
| 410 |
|
| 411 |
.. code-block:: python |
| 412 |
|
| 413 |
import socket |
| 414 |
try: |
| 415 |
pass |
| 416 |
except (ConnectionError, IOError, OSError, socket.error): |
| 417 |
pass |
| 418 |
|
| 419 |
* A new Python 3 checker was added to warn about accessing ``sys.maxint``. This attribute was |
| 420 |
removed in Python 3 in favor of ``sys.maxsize``. |
| 421 |
|
| 422 |
.. code-block:: python |
| 423 |
|
| 424 |
import sys |
| 425 |
print(sys.maxint) |
| 426 |
|
| 427 |
Instead of using ``sys.maxint``, use ``sys.maxsize`` |
| 428 |
|
| 429 |
.. code-block:: python |
| 430 |
|
| 431 |
import sys |
| 432 |
print(sys.maxsize) |
| 433 |
|
| 434 |
* A new Python 3 checker was added to warn about importing modules that have either moved or been |
| 435 |
removed from the standard library. |
| 436 |
|
| 437 |
One of the major undertakings with Python 3 was a reorganization of the standard library to |
| 438 |
remove old or supplanted modules and reorganize some of the existing modules. As a result, |
| 439 |
roughly 100 modules that exist in Python 2 no longer exist in Python 3. See |
| 440 |
https://peps.python.org/pep-3108/ and https://peps.python.org/pep-0004/ for more |
| 441 |
information. There were suggestions on how to handle this, at |
| 442 |
pythonhosted.org/six/#module-six.moves (dead link) or python3porting.com/stdlib.html (dead link). |
| 443 |
|
| 444 |
.. code-block:: python |
| 445 |
|
| 446 |
from cStringIO import StringIO |
| 447 |
|
| 448 |
Instead of directly importing the deprecated module, either use ``six.moves`` or a conditional |
| 449 |
import. |
| 450 |
|
| 451 |
.. code-block:: python |
| 452 |
|
| 453 |
from six.moves import cStringIO as StringIO |
| 454 |
|
| 455 |
if sys.version_info[0] >= 3: |
| 456 |
from io import StringIO |
| 457 |
else: |
| 458 |
from cStringIO import StringIO |
| 459 |
|
| 460 |
This checker will assume any imports that happen within a conditional or a ``try/except`` block |
| 461 |
are valid. |
| 462 |
|
| 463 |
* A new Python 3 checker was added to warn about accessing deprecated functions on the string |
| 464 |
module. Python 3 removed functions that were duplicated from the builtin ``str`` class. See |
| 465 |
https://docs.python.org/2/library/string.html#deprecated-string-functions for more information. |
| 466 |
|
| 467 |
.. code-block:: python |
| 468 |
|
| 469 |
import string |
| 470 |
print(string.upper('hello world!')) |
| 471 |
|
| 472 |
Instead of using ``string.upper``, call the ``upper`` method directly on the string object. |
| 473 |
|
| 474 |
.. code-block:: python |
| 475 |
|
| 476 |
"hello world!".upper() |
| 477 |
|
| 478 |
|
| 479 |
* A new Python 3 checker was added to warn about calling ``str.translate`` with the removed |
| 480 |
``deletechars`` parameter. ``str.translate`` is frequently used as a way to remove characters |
| 481 |
from a string. |
| 482 |
|
| 483 |
.. code-block:: python |
| 484 |
|
| 485 |
'hello world'.translate(None, 'low') |
| 486 |
|
| 487 |
Unfortunately, there is not an idiomatic way of writing this call in a 2and3 compatible way. If |
| 488 |
this code is not in the critical path for your application and the use of ``translate`` was a |
| 489 |
premature optimization, consider using ``re.sub`` instead: |
| 490 |
|
| 491 |
.. code-block:: python |
| 492 |
|
| 493 |
import re |
| 494 |
chars_to_remove = re.compile('[low]') |
| 495 |
chars_to_remove.sub('', 'hello world') |
| 496 |
|
| 497 |
If this code is in your critical path and must be as fast as possible, consider declaring a |
| 498 |
helper method that varies based upon Python version. |
| 499 |
|
| 500 |
.. code-block:: python |
| 501 |
|
| 502 |
if six.PY3: |
| 503 |
def _remove_characters(text, deletechars): |
| 504 |
return text.translate({ord(x): None for x in deletechars}) |
| 505 |
else: |
| 506 |
def _remove_characters(text, deletechars): |
| 507 |
return text.translate(None, deletechars) |
| 508 |
|
| 509 |
* A new refactoring check was added, ``consider-using-ternary``, which is |
| 510 |
emitted when pylint encounters constructs which were used to emulate |
| 511 |
ternary statement before it was introduced in Python 2.5. |
| 512 |
|
| 513 |
.. code-block:: python |
| 514 |
|
| 515 |
value = condition and truth_value or false_value |
| 516 |
|
| 517 |
|
| 518 |
Warning can be fixed by using standard ternary construct: |
| 519 |
|
| 520 |
.. code-block:: python |
| 521 |
|
| 522 |
value = truth_value if condition else false_value |
| 523 |
|
| 524 |
|
| 525 |
* A new refactoring check was added, ``trailing-comma-tuple``, which is emitted |
| 526 |
when pylint finds an one-element tuple, created by a stray comma. This can |
| 527 |
suggest a potential problem in the code and it is recommended to use parentheses |
| 528 |
in order to emphasise the creation of a tuple, rather than relying on the comma |
| 529 |
itself. |
| 530 |
|
| 531 |
The warning is emitted for such a construct: |
| 532 |
|
| 533 |
.. code-block:: python |
| 534 |
|
| 535 |
a = 1, |
| 536 |
|
| 537 |
The warning can be fixed by adding parentheses: |
| 538 |
|
| 539 |
.. code-block:: python |
| 540 |
|
| 541 |
a = (1, ) |
| 542 |
|
| 543 |
|
| 544 |
* Two new check were added for detecting an unsupported operation |
| 545 |
over an instance, ``unsupported-assignment-operation`` and ``unsupported-delete-operation``. |
| 546 |
The first one is emitted whenever an object does not support item assignment, while |
| 547 |
the second is emitted when an object does not support item deletion: |
| 548 |
|
| 549 |
.. code-block:: python |
| 550 |
|
| 551 |
class A: |
| 552 |
pass |
| 553 |
instance = A() |
| 554 |
instance[4] = 4 # unsupported-assignment-operation |
| 555 |
del instance[4] # unsupported-delete-operation |
| 556 |
|
| 557 |
* A new check was added, ``relative-beyond-top-level``, which is emitted |
| 558 |
when a relative import tries to access too many levels in the current package. |
| 559 |
|
| 560 |
* A new check was added, ``trailing-newlines``, which is emitted when a file |
| 561 |
has trailing new lines. |
| 562 |
|
| 563 |
* ``invalid-length-returned`` check was added, which is emitted when a ``__len__`` |
| 564 |
implementation does not return a non-negative integer. |
| 565 |
|
| 566 |
* There is a new extension, ``pylint.extensions.mccabe``, which can be used for |
| 567 |
computing the McCabe complexity of classes and functions. |
| 568 |
|
| 569 |
You can enable this extension through ``--load-plugins=pylint.extensions.mccabe`` |
| 570 |
|
| 571 |
* A new check was added, ``used-prior-global-declaration``. This is emitted when |
| 572 |
a name is used prior a global declaration, resulting in a SyntaxError in Python 3.6. |
| 573 |
|
| 574 |
* A new message was added, ``assign-to-new-keyword``. This is emitted when used name |
| 575 |
is known to become a keyword in future Python release. Assignments to keywords |
| 576 |
would result in ``SyntaxError`` after switching to newer interpreter version. |
| 577 |
|
| 578 |
.. rstcheck: ignore-next-code-block |
| 579 |
.. code-block:: python |
| 580 |
|
| 581 |
# While it's correct in Python 2.x, it raises a SyntaxError in Python 3.x |
| 582 |
True = 1 |
| 583 |
False = 0 |
| 584 |
|
| 585 |
# Same as above, but it'll be a SyntaxError starting from Python 3.7 |
| 586 |
async = "async" |
| 587 |
await = "await" |
| 588 |
|
| 589 |
|
| 590 |
Other Changes |
| 591 |
============= |
| 592 |
|
| 593 |
* We don't emit by default ``no-member`` if we have opaque inference objects in the inference results |
| 594 |
|
| 595 |
This is controlled through the new flag ``--ignore-on-opaque-inference``, which is by |
| 596 |
default True. The inference can return multiple potential results while |
| 597 |
evaluating a Python object, but some branches might not be evaluated, which |
| 598 |
results in partial inference. In that case, it might be useful to still emit |
| 599 |
:ref:`no-member` and other checks for the rest of the inferred objects. |
| 600 |
|
| 601 |
* Namespace packages are now supported by pylint. This includes both explicit namespace |
| 602 |
packages and implicit namespace packages, supported in Python 3 through PEP 420. |
| 603 |
|
| 604 |
* A new option was added, ``--analyse-fallback-block``. |
| 605 |
|
| 606 |
This can be used to support both Python 2 and 3 compatible import block code, |
| 607 |
which means that the import block might have code that exists only in one or another |
| 608 |
interpreter, leading to false positives when analysed. By default, this is false, you |
| 609 |
can enable the analysis for both branches using this flag. |
| 610 |
|
| 611 |
* ``ignored-argument-names`` option is now used for ignoring arguments |
| 612 |
for :ref:`unused-variable` check. |
| 613 |
|
| 614 |
This option was used for ignoring arguments when computing the correct number of arguments |
| 615 |
a function should have, but for handling the arguments with regard |
| 616 |
to :ref:`unused-variable` check, dummy-variables-rgx was used instead. Now, ignored-argument-names |
| 617 |
is used for its original purpose and also for ignoring the matched arguments for |
| 618 |
the :ref:`unused-variable` check. This offers a better control of what should be ignored |
| 619 |
and how. |
| 620 |
Also, the same option was moved from the design checker to the variables checker, |
| 621 |
which means that the option now appears under the ``[VARIABLES]`` section inside |
| 622 |
the configuration file. |
| 623 |
|
| 624 |
* A new option was added, ``redefining-builtins-modules``, for controlling the modules |
| 625 |
which can redefine builtins, such as six.moves and future.builtins. |
| 626 |
|
| 627 |
* A new option was added, ``ignore-patterns``, which is used for building a |
| 628 |
ignore list of directories and files matching the regex patterns, similar to the |
| 629 |
``ignore`` option. |
| 630 |
|
| 631 |
|
| 632 |
* The reports are now disabled by default, as well as the information category |
| 633 |
warnings. |
| 634 |
|
| 635 |
* ``arguments-differ`` check was rewritten to take in consideration |
| 636 |
keyword only parameters and variadics. |
| 637 |
|
| 638 |
Now it also complains about losing or adding capabilities to a method, |
| 639 |
by introducing positional or keyword variadics. For instance, *pylint* |
| 640 |
now complains about these cases: |
| 641 |
|
| 642 |
.. code-block:: python |
| 643 |
|
| 644 |
class Parent(object): |
| 645 |
|
| 646 |
def foo(self, first, second): |
| 647 |
... |
| 648 |
|
| 649 |
def bar(self, **kwargs): |
| 650 |
... |
| 651 |
|
| 652 |
def baz(self, *, first): |
| 653 |
... |
| 654 |
|
| 655 |
class Child(Parent): |
| 656 |
|
| 657 |
# Why subclassing in the first place? |
| 658 |
def foo(self, *args, **kwargs): |
| 659 |
# mutate args or kwargs. |
| 660 |
super(Child, self).foo(*args, **kwargs) |
| 661 |
|
| 662 |
def bar(self, first=None, second=None, **kwargs): |
| 663 |
... |
| 664 |
# The overridden method adds two new parameters, |
| 665 |
# which can also be passed as positional arguments, |
| 666 |
# breaking the contract of the parent's method. |
| 667 |
|
| 668 |
def baz(self, first): |
| 669 |
... |
| 670 |
# Not keyword-only |
| 671 |
|
| 672 |
* ``redefined-outer-name`` is now also emitted when a |
| 673 |
nested loop's target variable is the same as an outer loop. |
| 674 |
|
| 675 |
.. code-block:: python |
| 676 |
|
| 677 |
for i, j in [(1, 2), (3, 4)]: |
| 678 |
for j in range(i): |
| 679 |
print(j) |
| 680 |
|
| 681 |
* relax character limit for method and function names that starts with ``_``. |
| 682 |
This will let people to use longer descriptive names for methods and |
| 683 |
functions with a shorter scope (considered as private). The same idea |
| 684 |
applies to variable names, only with an inverse rule: you want long |
| 685 |
descriptive names for variables with bigger scope, like globals. |
| 686 |
|
| 687 |
* Add ``InvalidMessageError`` exception class and replace ``assert`` in |
| 688 |
pylint.utils with ``raise InvalidMessageError``. |
| 689 |
|
| 690 |
* ``UnknownMessageError`` (formerly ``UnknownMessage``) and |
| 691 |
``EmptyReportError`` (formerly ``EmptyReport``) are now provided by the new |
| 692 |
``pylint.exceptions`` submodule instead of ``pylint.utils`` as before. |
| 693 |
|
| 694 |
* We now support inline comments for comma separated values in the configurations |
| 695 |
|
| 696 |
For instance, you can now use the **#** sign for having comments inside |
| 697 |
comma separated values, as seen below:: |
| 698 |
|
| 699 |
disable=no-member, # Don't care about it for now |
| 700 |
bad-indentation, # No need for this |
| 701 |
import-error |
| 702 |
|
| 703 |
Of course, interweaving comments with values is also working:: |
| 704 |
|
| 705 |
disable=no-member, |
| 706 |
# Don't care about it for now |
| 707 |
bad-indentation # No need for this |
| 708 |
|
| 709 |
|
| 710 |
This works by setting the `inline comment prefixes`_ accordingly. |
| 711 |
|
| 712 |
* Added epytext docstring support to the docparams extension. |
| 713 |
|
| 714 |
* We added support for providing hints when not finding a missing member. |
| 715 |
|
| 716 |
For example, given the following code, it should be obvious that |
| 717 |
the programmer intended to use the ``mail`` attribute, rather than |
| 718 |
``email``. |
| 719 |
|
| 720 |
.. code-block:: python |
| 721 |
|
| 722 |
class Contribution: |
| 723 |
def __init__(self, name, email, date): |
| 724 |
self.name = name |
| 725 |
self.mail = mail |
| 726 |
self.date = date |
| 727 |
|
| 728 |
for c in contributions: |
| 729 |
print(c.email) # Oups |
| 730 |
|
| 731 |
**pylint** will now warn that there is a chance of having a typo, |
| 732 |
suggesting new names that could be used instead. |
| 733 |
|
| 734 |
.. code-block:: sh |
| 735 |
|
| 736 |
$ pylint a.py |
| 737 |
E: 8,10: Instance of 'Contribution' has no 'email' member; maybe 'mail'? |
| 738 |
|
| 739 |
The behaviour is controlled through the ``--missing-member-hint`` option. |
| 740 |
Other options that come with this change are ``--missing-member-max-choices`` |
| 741 |
for choosing the total number of choices that should be picked in this |
| 742 |
situation and ``--missing-member-hint-distance``, which specifies a metric |
| 743 |
for computing the distance between the names (this is based on Levenshtein |
| 744 |
distance, which means the lower the number, the more pickier the algorithm |
| 745 |
will be). |
| 746 |
|
| 747 |
* ``PyLinter.should_analyze_file`` has a new parameter, ``is_argument``, |
| 748 |
which specifies if the given path is a **pylint** argument or not. |
| 749 |
|
| 750 |
``should_analyze_file`` is called whenever **pylint** tries to determine |
| 751 |
if a file should be analyzed, defaulting to files with the ``.py`` |
| 752 |
extension, but this function gets called only in the case where the said |
| 753 |
file is not passed as a command line argument to **pylint**. This usually |
| 754 |
means that pylint will analyze a file, even if that file has a different |
| 755 |
extension, as long as the file was explicitly passed at command line. |
| 756 |
Since ``should_analyze_file`` cannot be overridden to handle all the cases, |
| 757 |
the check for the provenience of files was moved into ``should_analyze_file``. |
| 758 |
This means we now can write something similar with this example, for ignoring |
| 759 |
every file respecting the desired property, disregarding the provenience of the |
| 760 |
file, being it a file passed as CLI argument or part of a package. |
| 761 |
|
| 762 |
.. code-block:: python |
| 763 |
|
| 764 |
from pylint.lint import Run, PyLinter |
| 765 |
|
| 766 |
class CustomPyLinter(PyLinter): |
| 767 |
|
| 768 |
def should_analyze_file(self, modname, path, is_argument=False): |
| 769 |
if respect_condition(path): |
| 770 |
return False |
| 771 |
return super().should_analyze_file(modname, path, is_argument=is_argument) |
| 772 |
|
| 773 |
|
| 774 |
class CustomRun(Run): |
| 775 |
LinterClass = CustomPyLinter |
| 776 |
|
| 777 |
CustomRun(sys.argv[1:]) |
| 778 |
|
| 779 |
* Imports aliased with underscore are skipped when checking for unused imports. |
| 780 |
|
| 781 |
* ``bad-builtin`` and ``redefined-variable-type`` are now extensions, |
| 782 |
being disabled by default. They can be enabled through: |
| 783 |
``--load-plugins=pylint.extensions.redefined_variable_type,pylint.extensions.bad_builtin`` |
| 784 |
|
| 785 |
* Imports checker supports new switch ``allow-wildcard-with-all`` which disables |
| 786 |
warning on wildcard import when imported module defines ``__all__`` variable. |
| 787 |
|
| 788 |
* ``differing-param-doc`` is now used for the differing part of the old ``missing-param-doc``, |
| 789 |
and ``differing-type-doc`` for the differing part of the old ``missing-type-doc``. |
| 790 |
|
| 791 |
|
| 792 |
Bug fixes |
| 793 |
========= |
| 794 |
|
| 795 |
* Fix a false positive of ``redundant-returns-doc``, occurred when the documented |
| 796 |
function was using *yield* instead of *return*. |
| 797 |
|
| 798 |
* Fix a false positive of ``missing-param-doc`` and ``missing-type-doc``, |
| 799 |
occurred when a class docstring uses the ``For the parameters, see`` |
| 800 |
magic string but the class ``__init__`` docstring does not, or vice versa. |
| 801 |
|
| 802 |
* Added proper exception type inference for ``missing-raises-doc``. Now: |
| 803 |
|
| 804 |
.. code-block:: python |
| 805 |
|
| 806 |
def my_func(): |
| 807 |
""""My function.""" |
| 808 |
ex = ValueError('foo') |
| 809 |
raise ex |
| 810 |
|
| 811 |
will properly be flagged for missing documentation of |
| 812 |
``:raises ValueError:`` instead of ``:raises ex:``, among other scenarios. |
| 813 |
|
| 814 |
* Fix false positives of ``missing-[raises|params|type]-doc`` due to not |
| 815 |
recognizing valid keyword synonyms supported by Sphinx. |
| 816 |
|
| 817 |
* More thorough validation in ``MessagesStore.register_messages()`` to detect |
| 818 |
conflicts between a new message and any existing message id, symbol, |
| 819 |
or ``old_names``. |
| 820 |
|
| 821 |
* We now support having plugins that shares the same name and with each one |
| 822 |
providing options. |
| 823 |
|
| 824 |
A plugin can be logically split into multiple classes, each class providing |
| 825 |
certain capabilities, all of them being tied under the same name. But when |
| 826 |
two or more such classes are also adding options, then **pylint** crashed, |
| 827 |
since it already added the first encountered section. Now, these should |
| 828 |
work as expected. |
| 829 |
|
| 830 |
.. code-block:: python |
| 831 |
|
| 832 |
from pylint.checkers import BaseChecker |
| 833 |
|
| 834 |
|
| 835 |
class DummyPlugin1(BaseChecker): |
| 836 |
name = 'dummy_plugin' |
| 837 |
msgs = {'I9061': ('Dummy short desc 01', 'dummy-message-01', 'Dummy long desc')} |
| 838 |
options = ( |
| 839 |
('dummy_option_1', { |
| 840 |
'type': 'string', |
| 841 |
'metavar': '<string>', |
| 842 |
'help': 'Dummy option 1', |
| 843 |
}), |
| 844 |
) |
| 845 |
|
| 846 |
|
| 847 |
class DummyPlugin2(BaseChecker): |
| 848 |
name = 'dummy_plugin' |
| 849 |
msgs = {'I9060': ('Dummy short desc 02', 'dummy-message-02', 'Dummy long desc')} |
| 850 |
options = ( |
| 851 |
('dummy_option_2', { |
| 852 |
'type': 'string', |
| 853 |
'metavar': '<string>', |
| 854 |
'help': 'Dummy option 2', |
| 855 |
}), |
| 856 |
) |
| 857 |
|
| 858 |
|
| 859 |
def register(linter): |
| 860 |
linter.register_checker(DummyPlugin1(linter)) |
| 861 |
linter.register_checker(DummyPlugin2(linter)) |
| 862 |
|
| 863 |
* We do not yield ``unused-argument`` for singledispatch implementations and |
| 864 |
do not warn about ``function-redefined`` for multiple implementations with same name. |
| 865 |
|
| 866 |
.. code-block:: python |
| 867 |
|
| 868 |
from functools import singledispatch |
| 869 |
|
| 870 |
@singledispatch |
| 871 |
def f(x): |
| 872 |
return 2*x |
| 873 |
|
| 874 |
@f.register(str) |
| 875 |
def _(x): |
| 876 |
return -1 |
| 877 |
|
| 878 |
@f.register(int) |
| 879 |
@f.register(float) |
| 880 |
def _(x): |
| 881 |
return -x |
| 882 |
|
| 883 |
* ``unused-variable`` checker has new functionality of warning about unused |
| 884 |
variables in global module namespace. Since globals in module namespace |
| 885 |
may be a part of exposed API, this check is disabled by default. For |
| 886 |
enabling it, set ``allow-global-unused-variables`` option to false. |
| 887 |
|
| 888 |
* Fix a false-positive ``logging-format-interpolation`` message, when format |
| 889 |
specifications are used in formatted string. In general, these operations |
| 890 |
are not always convertible to old-style formatting used by logging module. |
| 891 |
|
| 892 |
* Added a new switch ``single-line-class-stmt`` to allow single-line declaration |
| 893 |
of empty class bodies (as seen in the example below). Pylint won't emit a |
| 894 |
``multiple-statements`` message when this option is enabled. |
| 895 |
|
| 896 |
.. code-block:: python |
| 897 |
|
| 898 |
class MyError(Exception): pass |
| 899 |
|
| 900 |
* ``too-many-format-args`` and ``too-few-format-args`` are emitted correctly |
| 901 |
(or not emitted at all, when exact count of elements in RHS cannot be |
| 902 |
inferred) when starred expressions are used in RHS tuple. For example, |
| 903 |
code block as shown below detects correctly that the used tuple has in |
| 904 |
fact three elements, not two. |
| 905 |
|
| 906 |
.. code-block:: python |
| 907 |
|
| 908 |
meat = ['spam', 'ham'] |
| 909 |
print('%s%s%s' % ('eggs', *meat)) |
| 910 |
|
| 911 |
* ``cyclic-import`` checker supports local disable clauses. When one |
| 912 |
of cycle imports was done in scope where disable clause was active, |
| 913 |
cycle is not reported as violation. |
| 914 |
|
| 915 |
Removed Changes |
| 916 |
=============== |
| 917 |
|
| 918 |
* ``pylint-gui`` was removed, because it was deemed unfit for being included |
| 919 |
in *pylint*. It had a couple of bugs and misfeatures, its usability was subpar |
| 920 |
and since its development was neglected, we decided it is best to move on without it. |
| 921 |
|
| 922 |
|
| 923 |
* The HTML reporter was removed, including the ``--output-format=html`` option. |
| 924 |
It was lately a second class citizen in Pylint, being mostly neglected. |
| 925 |
Since we now have the JSON reporter, it can be used as a basis for building |
| 926 |
more prettier HTML reports than what Pylint can currently generate. This is |
| 927 |
part of the effort of removing cruft from Pylint, by removing less used |
| 928 |
features. |
| 929 |
|
| 930 |
* The ``--files-output`` option was removed. While the same functionality cannot |
| 931 |
be easily replicated, the JSON reporter, for instance, can be used as a basis |
| 932 |
for generating the messages per each file. |
| 933 |
|
| 934 |
* ``--required-attributes`` option was removed. |
| 935 |
|
| 936 |
* ``--ignore-iface-methods`` option was removed. |
| 937 |
|
| 938 |
* The ``--optimize-ast`` flag was removed. |
| 939 |
|
| 940 |
The option was initially added for handling pathological cases, |
| 941 |
such as joining too many strings using the addition operator, which |
| 942 |
was leading pylint to have a recursion error when trying to figure |
| 943 |
out what the string was. Unfortunately, we decided to ignore the |
| 944 |
issue, since the pathological case would have happen when the |
| 945 |
code was parsed by Python as well, without actually reaching the |
| 946 |
runtime step and as such, we decided to remove the error altogether. |
| 947 |
|
| 948 |
* ``epylint.py_run``'s *script* parameter was removed. |
| 949 |
|
| 950 |
Now ``epylint.py_run`` is always using the underlying ``epylint.lint`` |
| 951 |
method from the current interpreter. This avoids some issues when multiple |
| 952 |
instances of **pylint** are installed, which means that ``epylint.py_run`` |
| 953 |
might have ran a different ``epylint`` script than what was intended. |
| 954 |
|
| 955 |
.. _`inline comment prefixes`: https://docs.python.org/3/library/configparser.html#customizing-parser-behaviour |