Skip to content

F821: Incorrectly reports use before assignment for conditional import #22467

@k4lizen

Description

@k4lizen

Summary

I do this in my code:

from __future__ import annotations

import pwndbg


def myfunc() -> None:
    # note that dbg is an object in the pwndbg module
    if pwndbg.dbg.is_gdblib_available():
        import pwndbg.gdblib.functions
        _ = pwndbg.gdblib.functions.functions
        print("yes!")
    else:
        print("no!")

myfunc()

And I get the following:

~❯ uv run ruff check repro.py
F823 Local variable `pwndbg` referenced before assignment
 --> repro.py:7:8
  |
6 | def myfunc() -> None:
7 |     if pwndbg.dbg.is_gdblib_available():
  |        ^^^^^^
8 |         import pwndbg.gdblib.functions
9 |         _ = pwndbg.gdblib.functions.functions
  |

Found 1 error.

What is going on?

Repro

git clone https://github.com/k4lizen/pwndbg/
cd pwndbg
git checkout 4588a455f5e511b567458ef618eb627788b539d5
uv run --all-groups --all-extras ruff check repro.py

Version

ruff 0.14.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnosticsRelated to reporting of diagnostics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions