From 6a1d7d8a1cb194aaf8cd06e84a56c76d1b08fca3 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 9 Oct 2022 18:28:29 -0400 Subject: [PATCH] Defer string annotations even when futures annotations are enabled (#378) --- .../test/fixtures/{F401.py => F401_0.py} | 0 resources/test/fixtures/F401_1.py | 5 + resources/test/fixtures/F401_2.py | 12 ++ resources/test/fixtures/F401_3.py | 14 ++ resources/test/fixtures/F401_4.py | 14 ++ src/check_ast.rs | 20 ++- src/linter.rs | 52 ++++++- .../ruff__linter__tests__f401_0.snap | 131 ++++++++++++++++++ .../ruff__linter__tests__f401_1.snap | 6 + .../ruff__linter__tests__f401_2.snap | 6 + .../ruff__linter__tests__f401_3.snap | 6 + .../ruff__linter__tests__f401_4.snap | 6 + 12 files changed, 264 insertions(+), 8 deletions(-) rename resources/test/fixtures/{F401.py => F401_0.py} (100%) create mode 100644 resources/test/fixtures/F401_1.py create mode 100644 resources/test/fixtures/F401_2.py create mode 100644 resources/test/fixtures/F401_3.py create mode 100644 resources/test/fixtures/F401_4.py create mode 100644 src/snapshots/ruff__linter__tests__f401_0.snap create mode 100644 src/snapshots/ruff__linter__tests__f401_1.snap create mode 100644 src/snapshots/ruff__linter__tests__f401_2.snap create mode 100644 src/snapshots/ruff__linter__tests__f401_3.snap create mode 100644 src/snapshots/ruff__linter__tests__f401_4.snap diff --git a/resources/test/fixtures/F401.py b/resources/test/fixtures/F401_0.py similarity index 100% rename from resources/test/fixtures/F401.py rename to resources/test/fixtures/F401_0.py diff --git a/resources/test/fixtures/F401_1.py b/resources/test/fixtures/F401_1.py new file mode 100644 index 0000000000..ba6860f277 --- /dev/null +++ b/resources/test/fixtures/F401_1.py @@ -0,0 +1,5 @@ +"""Access a sub-importation via an alias.""" +import pyarrow as pa +import pyarrow.csv + +print(pa.csv.read_csv("test.csv")) diff --git a/resources/test/fixtures/F401_2.py b/resources/test/fixtures/F401_2.py new file mode 100644 index 0000000000..a982490bc5 --- /dev/null +++ b/resources/test/fixtures/F401_2.py @@ -0,0 +1,12 @@ +"""Test: referencing an import via TypeAlias.""" +import sys + +import numpy as np + +if sys.version_info >= (3, 10): + from typing import TypeAlias +else: + from typing_extensions import TypeAlias + + +CustomInt: TypeAlias = "np.int8 | np.int16" diff --git a/resources/test/fixtures/F401_3.py b/resources/test/fixtures/F401_3.py new file mode 100644 index 0000000000..bd0e101f0e --- /dev/null +++ b/resources/test/fixtures/F401_3.py @@ -0,0 +1,14 @@ +"""Test: referencing an import via TypeAlias (with future annotations).""" +from __future__ import annotations + +import sys + +import numpy as np + +if sys.version_info >= (3, 10): + from typing import TypeAlias +else: + from typing_extensions import TypeAlias + + +CustomInt: TypeAlias = np.int8 | np.int16 diff --git a/resources/test/fixtures/F401_4.py b/resources/test/fixtures/F401_4.py new file mode 100644 index 0000000000..a313fb2710 --- /dev/null +++ b/resources/test/fixtures/F401_4.py @@ -0,0 +1,14 @@ +"""Test: referencing an import via TypeAlias (with future annotations and quotes).""" +from __future__ import annotations + +import sys + +import numpy as np + +if sys.version_info >= (3, 10): + from typing import TypeAlias +else: + from typing_extensions import TypeAlias + + +CustomInt: TypeAlias = "np.int8 | np.int16" diff --git a/src/check_ast.rs b/src/check_ast.rs index e861b57ce1..926b385034 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -633,12 +633,20 @@ where let prev_in_annotation = self.in_annotation; if self.in_annotation && self.annotations_future_enabled { - self.deferred_annotations.push(( - expr, - self.scope_stack.clone(), - self.parent_stack.clone(), - )); - visitor::walk_expr(self, expr); + if let ExprKind::Constant { + value: Constant::Str(value), + .. + } = &expr.node + { + self.deferred_string_annotations + .push((Range::from_located(expr), value)); + } else { + self.deferred_annotations.push(( + expr, + self.scope_stack.clone(), + self.parent_stack.clone(), + )); + } return; } diff --git a/src/linter.rs b/src/linter.rs index 67ba8aeeca..bb159a9dce 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -376,9 +376,57 @@ mod tests { } #[test] - fn f401() -> Result<()> { + fn f401_0() -> Result<()> { let mut checks = check_path( - Path::new("./resources/test/fixtures/F401.py"), + Path::new("./resources/test/fixtures/F401_0.py"), + &settings::Settings::for_rule(CheckCode::F401), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn f401_1() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/F401_1.py"), + &settings::Settings::for_rule(CheckCode::F401), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn f401_2() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/F401_2.py"), + &settings::Settings::for_rule(CheckCode::F401), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn f401_3() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/F401_3.py"), + &settings::Settings::for_rule(CheckCode::F401), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + + #[test] + fn f401_4() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/F401_4.py"), &settings::Settings::for_rule(CheckCode::F401), &fixer::Mode::Generate, )?; diff --git a/src/snapshots/ruff__linter__tests__f401_0.snap b/src/snapshots/ruff__linter__tests__f401_0.snap new file mode 100644 index 0000000000..04b0b2a61d --- /dev/null +++ b/src/snapshots/ruff__linter__tests__f401_0.snap @@ -0,0 +1,131 @@ +--- +source: src/linter.rs +expression: checks +--- +- kind: + UnusedImport: + - functools + location: + row: 2 + column: 1 + end_location: + row: 2 + column: 21 + fix: + content: import os + location: + row: 2 + column: 1 + end_location: + row: 2 + column: 21 + applied: false +- kind: + UnusedImport: + - collections.OrderedDict + location: + row: 4 + column: 1 + end_location: + row: 8 + column: 2 + fix: + content: "from collections import (\n Counter,\n namedtuple,\n)" + location: + row: 4 + column: 1 + end_location: + row: 8 + column: 2 + applied: false +- kind: + UnusedImport: + - logging.handlers + location: + row: 12 + column: 1 + end_location: + row: 12 + column: 24 + fix: + content: import logging.handlers + location: + row: 12 + column: 1 + end_location: + row: 12 + column: 24 + applied: false +- kind: + UnusedImport: + - shelve + location: + row: 33 + column: 5 + end_location: + row: 33 + column: 18 + fix: + content: "" + location: + row: 33 + column: 1 + end_location: + row: 34 + column: 1 + applied: false +- kind: + UnusedImport: + - importlib + location: + row: 34 + column: 5 + end_location: + row: 34 + column: 21 + fix: + content: "" + location: + row: 34 + column: 1 + end_location: + row: 35 + column: 1 + applied: false +- kind: + UnusedImport: + - pathlib + location: + row: 38 + column: 5 + end_location: + row: 38 + column: 19 + fix: + content: "" + location: + row: 38 + column: 1 + end_location: + row: 39 + column: 1 + applied: false +- kind: + UnusedImport: + - pickle + location: + row: 53 + column: 9 + end_location: + row: 53 + column: 22 + fix: + content: pass + location: + row: 53 + column: 9 + end_location: + row: 53 + column: 22 + applied: false + diff --git a/src/snapshots/ruff__linter__tests__f401_1.snap b/src/snapshots/ruff__linter__tests__f401_1.snap new file mode 100644 index 0000000000..60c615f917 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__f401_1.snap @@ -0,0 +1,6 @@ +--- +source: src/linter.rs +expression: checks +--- +[] + diff --git a/src/snapshots/ruff__linter__tests__f401_2.snap b/src/snapshots/ruff__linter__tests__f401_2.snap new file mode 100644 index 0000000000..60c615f917 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__f401_2.snap @@ -0,0 +1,6 @@ +--- +source: src/linter.rs +expression: checks +--- +[] + diff --git a/src/snapshots/ruff__linter__tests__f401_3.snap b/src/snapshots/ruff__linter__tests__f401_3.snap new file mode 100644 index 0000000000..60c615f917 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__f401_3.snap @@ -0,0 +1,6 @@ +--- +source: src/linter.rs +expression: checks +--- +[] + diff --git a/src/snapshots/ruff__linter__tests__f401_4.snap b/src/snapshots/ruff__linter__tests__f401_4.snap new file mode 100644 index 0000000000..60c615f917 --- /dev/null +++ b/src/snapshots/ruff__linter__tests__f401_4.snap @@ -0,0 +1,6 @@ +--- +source: src/linter.rs +expression: checks +--- +[] +