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-underthreshold.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 810lazyimport is used where Python forbids it: inside a function, a class body or atrystatement, or as a wildcard import. Python only rejects those when compiling the module, andastparses them without complaining, sopylinthas to report them itself.Add
using-lazy-import-in-unsupported-version(W2608), emitted when the code uses a PEP 810lazyimport whilepy-versionstill includes a Python version that cannot compile it.Refs #10982
False Positives Fixed¶
Fix
unused-importfalse positives for names used only in themetaclass=argument of a class defined inside another class or inside a block such as anif, 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 asundefined-variablewhen 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-variableis now reported for them, as for the bases.Closes #1630
Fix a false positive for
using-constant-testwhen testing a method or function whose decorator is unknown or is a class, such as a third-partycached_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-methodsfor classes decorated withattr.s,attr.dataclass,attr.define,attrs.mutableand the otherattrsclass decorators when they are used throughimport 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 calledsormutableis still reported.Closes #3732
Avoid
dict-init-mutatewhen 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-assignmentwhen 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-defaultno longer flags a non-string default when the result is passed straight toint(),float()orbool(), as inint(os.getenv("PORT", 8000)).Refs #10092
A version guard no longer has to spell out
sys.version_infoliterally to be recognized as one.Refs #11293
Fixed a false positive for
arguments-differwhen 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-withis no longer reported for a resource used in awithafter a nested function definition: leaving the nested function was reporting the resources of the function around it.Refs #11722
False Negatives Fixed¶
invalid-envvar-valueandinvalid-envvar-defaultnow also checkos.environ.get, howeverosorenvironis imported.Closes #10092
Report
used-before-assignmentwhen a bare type annotation masks assignments that may not execute intry,except, orfinallycontrol flow.Closes #10847
More checks now also run on
async forandasync def:too-many-branchescountsasync forloops.method-cache-max-size-none,singledispatch-methodandsingledispatchmethod-functioncheckasync def.bad-dunder-namechecksasync defmethods, and reports a dunder method that cannot beasync defbecause 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) orasync def __enter__instead of__aenter__.looping-through-iteratorchecksasync for, so an async generator consumed again in a nested loop is reported.useless-return,inconsistent-return-statementsandconsider-using-withcheckasync def, andtoo-many-nested-blocksis reported for anasync defeven when no other function follows it.inconsistent-return-statementsanduseless-returnare 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
--classnames 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
pyreversenow 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-packagesor 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
ForandWithnow also run onasync forandasync with. The AST walker dispatches on the node's exact class name, sovisit_fornever received anAsyncForeven thoughAsyncForis a subclass ofFor. This makesuseless-else-on-loop,confusing-with-statement,redeclared-assigned-name,redefined-argument-from-local,too-many-nested-blocksand theredefined-loop-nameextension fire on the async forms. Imports used only in a type comment onasync fororasync withare no longer reported asunused-import.unnecessary-dict-index-lookup,unnecessary-list-index-lookupandunbalanced-dict-unpackingreach theirasync forhandler now as well, but all three first have to infer a dict, a list orenumerate()as the iterated object, and none of those is an async iterator, so in practice they only apply to code that already raisesTypeErrorat runtime.Closes #11417
Fixed an
AssertionErrorcrash in the typecheck checker when accessing an attribute on anEnumexception instance.Closes #11638
Fixed a crash in
use-maxsplit-arg(C0207) when astr.split()orstr.rsplit()call indexed by a variable is used inside a loop that assigns to a subscript, an attribute or an unpacked tuple (for exampleobj[0] = 1,obj.attr = 1ora, b = 1, 2). Such targets have nonameattribute and previously raisedAttributeError.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-codenow 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-configand--generate-rcfilenow include the plugins given with--load-pluginsor--enable-all-extensions.Closes #11699
Checkers loaded with
--load-pluginsorinit-hookare no longer registered twice in every worker process, which used to emit each of their messages twice when--jobswas greater than 1.Closes #11701
Other Changes¶
wrong-spelling-in-commentandwrong-spelling-in-docstringno longer end with an emptyDid you mean: ''''?when there is no suggestion to show, for example withmax-spelling-suggestions=0.Closes #6420
Document TOML escaping and the reserved backslashes in the ignore-paths option.
Closes #6963
Fix the programmatic
run_pylintexample 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, souse "\b" insteadno longer renders asuse "b" instead.Closes #10737
pylint.checkers.utils.is_typing_memberis deprecated: it isis_module_member(node, "typing.<name>")with the module filled in. Callis_module_memberdirectly instead.is_typing_memberwill be removed in pylint 5.0.Refs #11294
Document the
no-headeroutput 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.10setting.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_memberpermits checking whether a node is a member of a module, through the various import syntaxes and aliases. Thetypingchecks that each rolled their own version of that question now share it.Refs #11294