Add semantic model flag when inside f-string replacement field (#10766)

## Summary

This PR adds a new semantic model flag to indicate that the checker is
inside an f-string replacement field. This will be used to ignore
certain checks if the target version doesn't support a specific feature
like PEP 701.

fixes: #10761 

## Test Plan

Add a test case from the raised issue.
This commit is contained in:
Dhruv Manilawala 2024-04-04 09:08:48 +05:30 committed by GitHub
parent 6b4fa17097
commit d02b1069b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 9 deletions

View file

@ -1525,6 +1525,12 @@ impl<'a> SemanticModel<'a> {
self.flags.intersects(SemanticModelFlags::F_STRING)
}
/// Return `true` if the model is in an f-string replacement field.
pub const fn in_f_string_replacement_field(&self) -> bool {
self.flags
.intersects(SemanticModelFlags::F_STRING_REPLACEMENT_FIELD)
}
/// Return `true` if the model is in boolean test.
pub const fn in_boolean_test(&self) -> bool {
self.flags.intersects(SemanticModelFlags::BOOLEAN_TEST)
@ -1960,6 +1966,15 @@ bitflags! {
/// ```
const DUNDER_ALL_DEFINITION = 1 << 22;
/// The model is in an f-string replacement field.
///
/// For example, the model could be visiting `x` or `y` in:
///
/// ```python
/// f"first {x} second {y}"
/// ```
const F_STRING_REPLACEMENT_FIELD = 1 << 23;
/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();