What's New in Pylint 4.2

:Release:4.2 :Date: TBA

Summary -- Release highlights

What's new in Pylint 4.2.0?

Release date: TBA

New Features

  • Add visual indicator to score output when score is below the --fail-under threshold.

    Closes #8503

  • Message documentation can now be opened using a message ID or symbol shortcut.

    Closes #9320

New Checks

  • Add invalid-lazy-import (E0403), emitted when a PEP 810 lazy import is used where Python forbids it: inside a function, a class body or a try statement, or as a wildcard import. Python only rejects those when compiling the module, and ast parses them without complaining, so pylint has to report them itself.

    Add using-lazy-import-in-unsupported-version (W2608), emitted when the code uses a PEP 810 lazy import while py-version still includes a Python version that cannot compile it.

    Refs #10982

False Positives Fixed

  • Fix unused-import false positives for names used only in the metaclass= argument of a class defined inside another class or inside a block such as an if, or within a more complex expression like a conditional expression, a call argument, a lambda or a comprehension. A name already used before such a class is also no longer reported as undefined-variable when the metaclass cannot be inferred. The names of a class body are no longer resolved from the metaclass of a class defined in one of its methods, where they are not visible: undefined-variable is now reported for them, as for the bases.

    Closes #1630

  • Fix a false positive for using-constant-test when testing a method or function whose decorator is unknown or is a class, such as a third-party cached_property. astroid still infers such a method as a method, but the decorator may have turned it into any other value.

    Closes #3484

  • Fixed false positive too-few-public-methods for classes decorated with attr.s, attr.dataclass, attr.define, attrs.mutable and the other attrs class decorators when they are used through import attr, through an alias, or imported by name. The decorator is now matched by the name it was imported from, so a local function that happens to be called s or mutable is still reported.

    Closes #3732

  • Avoid dict-init-mutate when an assignment's key or value references the dictionary being initialized. Stop the suggested dictionary literal before later assignments that reference the dictionary.

    Closes #7997

  • Fix a false positive for used-before-assignment when a class body uses a module-level or builtin name that a method or nested class defined later in the same class body shadows.

    Closes #9134

  • invalid-envvar-default no longer flags a non-string default when the result is passed straight to int(), float() or bool(), as in int(os.getenv("PORT", 8000)).

    Refs #10092

  • A version guard no longer has to spell out sys.version_info literally to be recognized as one.

    Refs #11293

  • Fixed a false positive for arguments-differ when an overriding method declares a parameter as positional-only (/) that is a regular parameter in the overridden method. Positional-only parameters are now counted as positional parameters in the comparison.

    Closes #11567

  • Fixed a false positive undefined-variable (E0602) when a name annotated without a value (x: int) is assigned with the walrus operator (:=) in the condition of a comprehension spanning several lines, and used in its element.

    Closes #11647

  • consider-using-with is no longer reported for a resource used in a with after a nested function definition: leaving the nested function was reporting the resources of the function around it.

    Refs #11722

False Negatives Fixed

  • invalid-envvar-value and invalid-envvar-default now also check os.environ.get, however os or environ is imported.

    Closes #10092

  • Report used-before-assignment when a bare type annotation masks assignments that may not execute in try, except, or finally control flow.

    Closes #10847

  • More checks now also run on async for and async def:

    • too-many-branches counts async for loops.

    • method-cache-max-size-none, singledispatch-method and singledispatchmethod-function check async def.

    • bad-dunder-name checks async def methods, and reports a dunder method that cannot be async def because Python checks the type of its return value, uses it as a bool, or ignores it: async def __init__, async def __exit__ (which would swallow every exception) or async def __enter__ instead of __aenter__.

    • looping-through-iterator checks async for, so an async generator consumed again in a nested loop is reported.

    • useless-return, inconsistent-return-statements and consider-using-with check async def, and too-many-nested-blocks is reported for an async def even when no other function follows it.

    • inconsistent-return-statements and useless-return are no longer skipped for a function containing a nested function with the same name.

    Refs #11722

Other Bug Fixes

  • Pyreverse now reports a clear error instead of a traceback when --class names an unknown class or module, or omits the module name when analyzing multiple modules. Targets whose inferred values are all known not to be classes are also rejected. If a target has several inferred values, Pyreverse uses the first class among them.

    Closes #22

  • The MermaidJS output of pyreverse now honours --module-names=y: each class is labelled with its module-qualified name, as the dot and PlantUML outputs already were.

    Closes #10164

  • Fix a crash when linting a package whose parent directory is an implicit namespace package that also exists in site-packages or the standard library: the module paths of the files inside it could not be resolved, and the given package was reported without its package prefix.

    Closes #10278

  • Checkers registered for For and With now also run on async for and async with. The AST walker dispatches on the node's exact class name, so visit_for never received an AsyncFor even though AsyncFor is a subclass of For. This makes useless-else-on-loop, confusing-with-statement, redeclared-assigned-name, redefined-argument-from-local, too-many-nested-blocks and the redefined-loop-name extension fire on the async forms. Imports used only in a type comment on async for or async with are no longer reported as unused-import.

    unnecessary-dict-index-lookup, unnecessary-list-index-lookup and unbalanced-dict-unpacking reach their async for handler now as well, but all three first have to infer a dict, a list or enumerate() as the iterated object, and none of those is an async iterator, so in practice they only apply to code that already raises TypeError at runtime.

    Closes #11417

  • Fixed an AssertionError crash in the typecheck checker when accessing an attribute on an Enum exception instance.

    Closes #11638

  • Fixed a crash in use-maxsplit-arg (C0207) when a str.split() or str.rsplit() call indexed by a variable is used inside a loop that assigns to a subscript, an attribute or an unpacked tuple (for example obj[0] = 1, obj.attr = 1 or a, b = 1, 2). Such targets have no name attribute and previously raised AttributeError.

    The index variable is now also recognized as changed in the loop when it is reassigned through tuple unpacking or an assignment expression (:=), which removes a false positive.

    Refs #11676

  • duplicate-code now reports every copy of a duplicated block instead of only the first two. Each message lists all the copies, so there are fewer messages and the number of duplicated lines in the statistics goes up. The message counts distinct files, so a group can list several locations in one file.

    Closes #11679

  • --generate-toml-config and --generate-rcfile now include the plugins given with --load-plugins or --enable-all-extensions.

    Closes #11699

  • Checkers loaded with --load-plugins or init-hook are no longer registered twice in every worker process, which used to emit each of their messages twice when --jobs was greater than 1.

    Closes #11701

Other Changes

  • wrong-spelling-in-comment and wrong-spelling-in-docstring no longer end with an empty Did you mean: ''''? when there is no suggestion to show, for example with max-spelling-suggestions=0.

    Closes #6420

  • Document TOML escaping and the reserved backslashes in the ignore-paths option.

    Closes #6963

  • Fix the programmatic run_pylint example to pass command-line arguments as a sequence.

    Closes #8766

  • Clarify the distinction between the built-in design checker and the optional McCabe extension, including how to disable their messages individually.

    Fixes part of #9174

  • Backslashes in message and option descriptions are now escaped in the generated documentation and in --full-documentation, so use "\b" instead no longer renders as use "b" instead.

    Closes #10737

  • pylint.checkers.utils.is_typing_member is deprecated: it is is_module_member(node, "typing.<name>") with the module filled in. Call is_module_member directly instead. is_typing_member will be removed in pylint 5.0.

    Refs #11294

  • Document the no-header output format and the use of a custom reporter class with --output-format.

    Closes #11595

  • Remove support for launching pylint with Python 3.10. Code that supports Python 3.10 can still be linted with the --py-version=3.10 setting.

    Refs #11643

Internal Changes

  • Updating the functional tests output no longer removes an empty expected output specific to recent interpreters, like name.314.txt, which made the tests of these interpreters use the expected output of older ones.

    Refs #7362

  • New function pylint.checkers.utils.is_module_member permits checking whether a node is a member of a module, through the various import syntaxes and aliases. The typing checks that each rolled their own version of that question now share it.

    Refs #11294