Avoid enforcing native-literals rule within nested f-strings (#4488)

This commit is contained in:
Charlie Marsh 2023-06-02 00:00:31 -04:00 committed by GitHub
parent b8f45c93b4
commit ea3cbcc362
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 149 additions and 122 deletions

View file

@ -734,6 +734,11 @@ impl<'a> SemanticModel<'a> {
self.flags.contains(SemanticModelFlags::F_STRING)
}
/// Return `true` if the context is in a nested f-string.
pub const fn in_nested_f_string(&self) -> bool {
self.flags.contains(SemanticModelFlags::NESTED_F_STRING)
}
/// Return `true` if the context is in boolean test.
pub const fn in_boolean_test(&self) -> bool {
self.flags.contains(SemanticModelFlags::BOOLEAN_TEST)
@ -850,6 +855,14 @@ bitflags! {
/// ```
const F_STRING = 1 << 6;
/// The context is in a nested f-string.
///
/// For example, the context could be visiting `x` in:
/// ```python
/// f'{f"{x}"}'
/// ```
const NESTED_F_STRING = 1 << 7;
/// The context is in a boolean test.
///
/// For example, the context could be visiting `x` in:
@ -860,7 +873,7 @@ bitflags! {
///
/// The implication is that the actual value returned by the current expression is
/// not used, only its truthiness.
const BOOLEAN_TEST = 1 << 7;
const BOOLEAN_TEST = 1 << 8;
/// The context is in a `typing::Literal` annotation.
///
@ -869,7 +882,7 @@ bitflags! {
/// def f(x: Literal["A", "B", "C"]):
/// ...
/// ```
const LITERAL = 1 << 8;
const LITERAL = 1 << 9;
/// The context is in a subscript expression.
///
@ -877,7 +890,7 @@ bitflags! {
/// ```python
/// x["a"]["b"]
/// ```
const SUBSCRIPT = 1 << 9;
const SUBSCRIPT = 1 << 10;
/// The context is in a type-checking block.
///
@ -889,7 +902,7 @@ bitflags! {
/// if TYPE_CHECKING:
/// x: int = 1
/// ```
const TYPE_CHECKING_BLOCK = 1 << 10;
const TYPE_CHECKING_BLOCK = 1 << 11;
/// The context has traversed past the "top-of-file" import boundary.
@ -903,7 +916,7 @@ bitflags! {
///
/// x: int = 1
/// ```
const IMPORT_BOUNDARY = 1 << 11;
const IMPORT_BOUNDARY = 1 << 12;
/// The context has traversed past the `__future__` import boundary.
///
@ -918,7 +931,7 @@ bitflags! {
///
/// Python considers it a syntax error to import from `__future__` after
/// any other non-`__future__`-importing statements.
const FUTURES_BOUNDARY = 1 << 12;
const FUTURES_BOUNDARY = 1 << 13;
/// `__future__`-style type annotations are enabled in this context.
///
@ -930,7 +943,7 @@ bitflags! {
/// def f(x: int) -> int:
/// ...
/// ```
const FUTURE_ANNOTATIONS = 1 << 13;
const FUTURE_ANNOTATIONS = 1 << 14;
}
}