Skip to content

False negative used-before-assignment after name defined and used in except #5780

Open
@jacobtylerwalls

Description

@jacobtylerwalls

Bug description

This doesn't warn as it should:

try:
  pass
except ValueError:
  msg = 'ha'
  print(msg)
print(msg)  # should raise used-before-assignment

But this warns just fine:

try:
  pass
except ValueError:
  msg = 'ha'
print(msg)  # [used-before-assignment]

So it's an issue with "consuming" the name too early in the variables checker when used by the first "print(msg)".

Configuration

No response

Command used

pylint a.py

Pylint output

-------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 1.67/10, +8.33)

Expected behavior

************* Module a
a.py:6:6: E0601: Using variable 'msg' before assignment (used-before-assignment)

Pylint version

pylint 2.13.0-dev0
astroid 2.9.2
Python 3.10.1 (v3.10.1:2cd268a3a9, Dec  6 2021, 14:28:59) [Clang 13.0.0 (clang-1300.0.29.3)]

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: used-before-assignmentIssues related to 'used-before-assignment' checkFalse Negative 🦋No message is emitted but something is wrong with the codeNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions