Avoid treating Literal members as expressions with __future__ (#6032)

Closes https://github.com/astral-sh/ruff/issues/6030.
This commit is contained in:
Charlie Marsh 2023-07-24 11:09:37 -04:00 committed by GitHub
parent 6feb3fcc1b
commit 62ffc773de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 1 deletions

View file

@ -0,0 +1,11 @@
"""Test case: `Literal` with `__future__` annotations."""
from __future__ import annotations
from typing import Literal, Final
from typing_extensions import assert_type
CONSTANT: Final = "ns"
assert_type(CONSTANT, Literal["ns"])

View file

@ -2172,6 +2172,7 @@ where
fn visit_expr(&mut self, expr: &'b Expr) {
// Step 0: Pre-processing
if !self.semantic.in_f_string()
&& !self.semantic.in_literal()
&& !self.semantic.in_deferred_type_definition()
&& self.semantic.in_type_definition()
&& self.semantic.future_annotations()
@ -3921,7 +3922,7 @@ where
Some(typing::SubscriptKind::Literal) => {
self.semantic.flags |= SemanticModelFlags::LITERAL;
self.visit_type_definition(slice);
self.visit_expr(slice);
self.visit_expr_context(ctx);
}
// Ex) Optional[int]

View file

@ -125,6 +125,7 @@ mod tests {
#[test_case(Rule::UndefinedName, Path::new("F821_13.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_14.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_15.py"))]
#[test_case(Rule::UndefinedName, Path::new("F821_16.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_0.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_1.py"))]
#[test_case(Rule::UndefinedExport, Path::new("F822_2.py"))]

View file

@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pyflakes/mod.rs
---