[flake8-type-checking] Adds implementation for TC007 and TC008 (#12927)

Co-authored-by: Simon Brugman <sbrugman@users.noreply.github.com>
Co-authored-by: Carl Meyer <carl@oddbird.net>
This commit is contained in:
David Salvisberg 2024-11-27 09:51:20 +01:00 committed by GitHub
parent e0f3eaf1dd
commit 6fd10e2fe7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 1483 additions and 10 deletions

View file

@ -135,6 +135,35 @@ impl<'a> Binding<'a> {
self.flags.contains(BindingFlags::IN_EXCEPT_HANDLER)
}
/// Return `true` if this [`Binding`] represents a [PEP 613] type alias
/// e.g. `OptString` in:
/// ```python
/// from typing import TypeAlias
///
/// OptString: TypeAlias = str | None
/// ```
///
/// [PEP 613]: https://peps.python.org/pep-0613/
pub const fn is_annotated_type_alias(&self) -> bool {
self.flags.intersects(BindingFlags::ANNOTATED_TYPE_ALIAS)
}
/// Return `true` if this [`Binding`] represents a [PEP 695] type alias
/// e.g. `OptString` in:
/// ```python
/// type OptString = str | None
/// ```
///
/// [PEP 695]: https://peps.python.org/pep-0695/#generic-type-alias
pub const fn is_deferred_type_alias(&self) -> bool {
self.flags.intersects(BindingFlags::DEFERRED_TYPE_ALIAS)
}
/// Return `true` if this [`Binding`] represents either kind of type alias
pub const fn is_type_alias(&self) -> bool {
self.flags.intersects(BindingFlags::TYPE_ALIAS)
}
/// Return `true` if this binding "redefines" the given binding, as per Pyflake's definition of
/// redefinition.
pub fn redefines(&self, existing: &Binding) -> bool {
@ -366,6 +395,19 @@ bitflags! {
/// y = 42
/// ```
const IN_EXCEPT_HANDLER = 1 << 10;
/// The binding represents a [PEP 613] explicit type alias.
///
/// [PEP 613]: https://peps.python.org/pep-0613/
const ANNOTATED_TYPE_ALIAS = 1 << 11;
/// The binding represents a [PEP 695] type statement
///
/// [PEP 695]: https://peps.python.org/pep-0695/#generic-type-alias
const DEFERRED_TYPE_ALIAS = 1 << 12;
/// The binding represents any type alias.
const TYPE_ALIAS = Self::ANNOTATED_TYPE_ALIAS.bits() | Self::DEFERRED_TYPE_ALIAS.bits();
}
}