Skip to content

False positive used-before-assignment for homonym between subscript in comprehension and assignment in except block #6069

Closed
@jacobtylerwalls

Description

@jacobtylerwalls
Member

False positive for used-before-assignment from subscript notation in comprehension followed by definition of that same variable later in an except block.

# test.py
def select_dict(data, keys):
    try:
        d = {key: data[key] for key in keys}
    except KeyError as e:
        key, *_ = e.args
        raise Exception(f"{key} not found")

    return d
$ pip install -U pylint==2.13.4 -q && pylint test.py
************* Module test
test.py:3:23: E0601: Using variable 'key' before assignment (used-before-assignment)

------------------------------------------------------------------
Your code has been rated at 2.86/10 (previous run: 2.86/10, +0.00)

Originally posted by @skshetry in #6035 (comment)

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @jacobtylerwalls

    Issue actions

      False positive `used-before-assignment` for homonym between subscript in comprehension and assignment in except block · Issue #6069 · pylint-dev/pylint