Skip to content

possibly-used-before-assignment false negative when variable has type annotation #10847

@stenbein

Description

@stenbein

Bug description

We're looking to solve all the cases of possibly-used-before-assignment in our codebase. An example was caught by an LLM tool and I was surprised to find pylint didn't catch it.

If err in the below example has a type annotation then pylint will not complain about it possibly being used before assignment. If the type annotation is removed then it works.

result: str | None = None
err: CustomErrReportingClass

try:
    result = fetch_result()
except SpecialException1:
    err = CustomErrOne()
except SpecialException2:
    err = CustomErrTwo()

if not result:
    report_error(err)  # expecting pylint to complain here

Configuration

I'm using pylint with pre-commit. with this config.

repo: https://github.com/pycqa/pylint
rev: v4.0.4
hooks:
  - id: pylint
    name: pylint
    types: [python]
    require_serial: true
    args:
      [
        "-rn",
        "-sn",
        "--disable=all",
        "--enable=E0601,E0606",
      ]
    files: src/

Command used

`pre-commit run --all-files`

Though invoking pylint directly also gives the same result.

Pylint output

Nothing about this code is returned.

Expected behavior

Expecting this to be objected to as possibly-used-before-assignment

Pylint version

pylint 4.0.4
python 3.10.19

OS / Environment

Ubuntu in WSL2 or in a docker container also running ubuntu

Additional dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Negative 🦋No message is emitted but something is wrong with the code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions