From 42d969f19f15db368dfbe23b2cb0e12ff076c0fb Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 24 Jul 2023 11:49:48 -0400 Subject: [PATCH] Add additional test cases for `F823` (#6036) Making some behavior explicit / codified. See: https://github.com/astral-sh/ruff/issues/6029. --- .../resources/test/fixtures/pyflakes/F823.py | 24 +++++++++++++++++++ .../rules/pyflakes/rules/undefined_local.rs | 2 +- ..._rules__pyflakes__tests__F823_F823.py.snap | 18 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/pyflakes/F823.py b/crates/ruff/resources/test/fixtures/pyflakes/F823.py index 8821883f31..a17d91f0c7 100644 --- a/crates/ruff/resources/test/fixtures/pyflakes/F823.py +++ b/crates/ruff/resources/test/fixtures/pyflakes/F823.py @@ -39,3 +39,27 @@ class Class: def f(self): print(my_var) my_var = 1 + + +import sys + + +def main(): + print(sys.argv) + + try: + 3 / 0 + except ZeroDivisionError: + import sys + + sys.exit(1) + + +import sys + + +def main(): + print(sys.argv) + + for sys in range(5): + pass diff --git a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs index 0763d32db4..43c4eda595 100644 --- a/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs +++ b/crates/ruff/src/rules/pyflakes/rules/undefined_local.rs @@ -33,7 +33,7 @@ use crate::checkers::ast::Checker; /// ``` #[violation] pub struct UndefinedLocal { - pub name: String, + name: String, } impl Violation for UndefinedLocal { diff --git a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F823_F823.py.snap b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F823_F823.py.snap index 45bab8ac35..8139a06590 100644 --- a/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F823_F823.py.snap +++ b/crates/ruff/src/rules/pyflakes/snapshots/ruff__rules__pyflakes__tests__F823_F823.py.snap @@ -25,4 +25,22 @@ F823.py:40:15: F823 Local variable `my_var` referenced before assignment 41 | my_var = 1 | +F823.py:48:11: F823 Local variable `sys` referenced before assignment + | +47 | def main(): +48 | print(sys.argv) + | ^^^ F823 +49 | +50 | try: + | + +F823.py:62:11: F823 Local variable `sys` referenced before assignment + | +61 | def main(): +62 | print(sys.argv) + | ^^^ F823 +63 | +64 | for sys in range(5): + | +