[pyflakes] Avoid false positives in @no_type_check contexts (F821, F722) (#14615)

This commit is contained in:
Brent Westbrook 2024-11-26 14:13:43 -05:00 committed by GitHub
parent b94d6cf567
commit 9f446faa6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 136 additions and 3 deletions

View file

@ -1584,6 +1584,11 @@ impl<'a> SemanticModel<'a> {
self.flags.intersects(SemanticModelFlags::ANNOTATION)
}
/// Return `true` if the model is in a `@no_type_check` context.
pub const fn in_no_type_check(&self) -> bool {
self.flags.intersects(SemanticModelFlags::NO_TYPE_CHECK)
}
/// Return `true` if the model is in a typing-only type annotation.
pub const fn in_typing_only_annotation(&self) -> bool {
self.flags
@ -2222,6 +2227,23 @@ bitflags! {
/// [PEP 257]: https://peps.python.org/pep-0257/#what-is-a-docstring
const ATTRIBUTE_DOCSTRING = 1 << 25;
/// The model is in a [no_type_check] context.
///
/// This is used to skip type checking when the `@no_type_check` decorator is found.
///
/// For example (adapted from [#13824]):
/// ```python
/// from typing import no_type_check
///
/// @no_type_check
/// def fn(arg: "A") -> "R":
/// pass
/// ```
///
/// [no_type_check]: https://docs.python.org/3/library/typing.html#typing.no_type_check
/// [#13824]: https://github.com/astral-sh/ruff/issues/13824
const NO_TYPE_CHECK = 1 << 26;
/// The context is in any type annotation.
const ANNOTATION = Self::TYPING_ONLY_ANNOTATION.bits() | Self::RUNTIME_EVALUATED_ANNOTATION.bits() | Self::RUNTIME_REQUIRED_ANNOTATION.bits();