Skip to content

Commit

Permalink
Add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Mar 21, 2024
1 parent caa1450 commit 5732931
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pyflakes/F821_29.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Regression test for #10451.
Annotations in a class are allowed to be forward references
if `from __future__ import annotations` is active,
even if they're in a class included in
`lint.flake8-type-checking.runtime-evaluated-base-classes`.
They're not allowed to refer to symbols that cannot be *resolved*
at runtime, however.
"""

from __future__ import annotations

from sqlalchemy.orm import DeclarativeBase, Mapped


class Base(DeclarativeBase):
some_mapping: Mapped[list[Bar]] | None = None # Should not trigger F821 (resolveable forward reference)
simplified: list[Bar] | None = None # Should not trigger F821 (resolveable forward reference)


class Bar:
pass
23 changes: 23 additions & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ mod tests {
Ok(())
}

#[test_case(Rule::UndefinedName, Path::new("F821_29.py"))]
fn rules_with_flake8_type_checking_settings_enabled(
rule_code: Rule,
path: &Path,
) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("pyflakes").join(path).as_path(),
&LinterSettings {
flake8_type_checking: crate::rules::flake8_type_checking::settings::Settings {
runtime_required_base_classes: vec![
"pydantic.BaseModel".to_string(),
"sqlalchemy.orm.DeclarativeBase".to_string(),
],
..Default::default()
},
..LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Rule::UnusedVariable, Path::new("F841_4.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---

18 changes: 18 additions & 0 deletions repro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from sqlalchemy.orm import DeclarativeBase, Mapped

if TYPE_CHECKING:
from whatever import whatever


class Base(DeclarativeBase):
some_mapping: Mapped[list[Bar]] | None = None # F821 Undefined name `Bar`
simplified: list[Bar] | None = None # F821 Undefined name `Bar`
bad: whatever


class Bar:
pass

0 comments on commit 5732931

Please sign in to comment.