Skip to content

False positive used-before-assignment for filtered comprehensions in try blocks #5586

Closed
@jacobtylerwalls

Description

@jacobtylerwalls

Bug description

def func():
    try:
        print(value for value in range(1 / 0) if isinstance(value, int))  # BUG: emits used-before-assignment
    except ZeroDivisionError:
        value = 1

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
/Users/.../a.py:3:60: E0601: Using variable 'value' before assignment (used-before-assignment)

Expected behavior

No message.

Here, found_nodes for the node inside the generator expression includes the assignment of value in the except block.
https://github.com/PyCQA/pylint/blob/c0fbbb7adbc39cbd67bbd0e0b1ca4e9421c12643/pylint/checkers/variables.py#L611-L620

I'm not sure if the responsibility for filtering that out lies in to_consume(); if not, I suppose the new used-before-assignment logic will have to deal with it.

Pylint version

Caused in bd55b27d41542e3ca1f031f986b6151f6cac457f (2.13.0 unreleased)

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions