Skip to content

False positive: possibly-used-before-assignment and pytest.skip (and other NoReturn functions) #9674

Closed

Description

Bug description

import pytest
import sys

def test_something():
    if sys.platform == "linux":
        cmd = "ls"
    elif sys.platform == "win32":
        cmd = "dir"
    else:
        pytest.skip("only runs on Linux/Windows")

    print(cmd)

Configuration

No response

Command used

pylint -E test_x.py

Pylint output

************* Module test_x
test_x.py:12:10: E0606: Possibly using variable 'cmd' before assignment (possibly-used-before-assignment)

Expected behavior

No error: The pytest module has a couple of functions that never return:

  • pytest.exit(...)
  • pytest.skip(...)
  • pytest.fail(...)
  • pytest.xfail(...)

Related:


Instead of maintaining a list of functions like this, could pylint maybe instead check whether they have a NoReturn return type, if type information is available? That would cover assert_never(), all the pytest functions, possibly sys.exit() and friends (if stdlib typing information is available via typeshed), and lots and lots of other third-party cases like this.

Pylint version

pylint 3.2.1
astroid 3.2.2
Python 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417]

OS / Environment

Archlinux

Additional dependencies

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Labels

C: used-before-assignmentIssues related to 'used-before-assignment' checkFalse Positive 🦟A message is emitted but nothing is wrong with the codeLib specific 💅This affect the code from a particular libraryNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions