mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
[ty] rename BareTypeAliasType to ManualPEP695TypeAliasType (#20037)
## Summary Rename `TypeAliasType::Bare` to `TypeAliasType::ManualPEP695`, and `BareTypeAliasType` to `ManualPEP695TypeAliasType`. Why? Both existing variants of `TypeAliasType` are specific to features added in PEP 695 (which introduced both the `type` statement and `types.TypeAliasType`), so it doesn't make sense to name one with the name `PEP695` and not the other. A "bare" type alias, in my mind, is a legacy type alias like `IntOrStr = int | str`, which is "bare" in that there is nothing at all distinguishing it as a type alias. I will want to use the "bare" name for this variant, in a future PR. The renamed variant here describes a type alias created with `IntOrStr = types.TypeAliasType("IntOrStr", int | str)`, which is not "bare", it's just "manually" instantiated instead of using the `type` statement syntax sugar. (This is useful when using the `typing_extensions` backport of `TypeAliasType` on older Python versions.) ## Test Plan Pure rename, existing tests pass.
This commit is contained in:
parent
c22395dbc6
commit
8b827c3c6c
2 changed files with 22 additions and 19 deletions
|
@ -9231,12 +9231,14 @@ fn value_type_cycle_initial<'db>(_db: &'db dyn Db, _self: PEP695TypeAliasType<'d
|
||||||
Type::Never
|
Type::Never
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A PEP 695 `types.TypeAliasType` created by manually calling the constructor.
|
||||||
|
///
|
||||||
/// # Ordering
|
/// # Ordering
|
||||||
/// Ordering is based on the type alias's salsa-assigned id and not on its values.
|
/// Ordering is based on the type alias's salsa-assigned id and not on its values.
|
||||||
/// The id may change between runs, or when the alias was garbage collected and recreated.
|
/// The id may change between runs, or when the alias was garbage collected and recreated.
|
||||||
#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)]
|
#[salsa::interned(debug, heap_size=ruff_memory_usage::heap_size)]
|
||||||
#[derive(PartialOrd, Ord)]
|
#[derive(PartialOrd, Ord)]
|
||||||
pub struct BareTypeAliasType<'db> {
|
pub struct ManualPEP695TypeAliasType<'db> {
|
||||||
#[returns(ref)]
|
#[returns(ref)]
|
||||||
pub name: ast::name::Name,
|
pub name: ast::name::Name,
|
||||||
pub definition: Option<Definition<'db>>,
|
pub definition: Option<Definition<'db>>,
|
||||||
|
@ -9244,17 +9246,17 @@ pub struct BareTypeAliasType<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Salsa heap is tracked separately.
|
// The Salsa heap is tracked separately.
|
||||||
impl get_size2::GetSize for BareTypeAliasType<'_> {}
|
impl get_size2::GetSize for ManualPEP695TypeAliasType<'_> {}
|
||||||
|
|
||||||
fn walk_bare_type_alias<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
|
fn walk_manual_pep_695_type_alias<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
|
||||||
db: &'db dyn Db,
|
db: &'db dyn Db,
|
||||||
type_alias: BareTypeAliasType<'db>,
|
type_alias: ManualPEP695TypeAliasType<'db>,
|
||||||
visitor: &V,
|
visitor: &V,
|
||||||
) {
|
) {
|
||||||
visitor.visit_type(db, type_alias.value(db));
|
visitor.visit_type(db, type_alias.value(db));
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'db> BareTypeAliasType<'db> {
|
impl<'db> ManualPEP695TypeAliasType<'db> {
|
||||||
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self {
|
fn normalized_impl(self, db: &'db dyn Db, visitor: &NormalizedVisitor<'db>) -> Self {
|
||||||
Self::new(
|
Self::new(
|
||||||
db,
|
db,
|
||||||
|
@ -9272,7 +9274,7 @@ pub enum TypeAliasType<'db> {
|
||||||
/// A type alias defined using the PEP 695 `type` statement.
|
/// A type alias defined using the PEP 695 `type` statement.
|
||||||
PEP695(PEP695TypeAliasType<'db>),
|
PEP695(PEP695TypeAliasType<'db>),
|
||||||
/// A type alias defined by manually instantiating the PEP 695 `types.TypeAliasType`.
|
/// A type alias defined by manually instantiating the PEP 695 `types.TypeAliasType`.
|
||||||
Bare(BareTypeAliasType<'db>),
|
ManualPEP695(ManualPEP695TypeAliasType<'db>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn walk_type_alias_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
|
fn walk_type_alias_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
|
||||||
|
@ -9284,8 +9286,8 @@ fn walk_type_alias_type<'db, V: visitor::TypeVisitor<'db> + ?Sized>(
|
||||||
TypeAliasType::PEP695(type_alias) => {
|
TypeAliasType::PEP695(type_alias) => {
|
||||||
walk_pep_695_type_alias(db, type_alias, visitor);
|
walk_pep_695_type_alias(db, type_alias, visitor);
|
||||||
}
|
}
|
||||||
TypeAliasType::Bare(type_alias) => {
|
TypeAliasType::ManualPEP695(type_alias) => {
|
||||||
walk_bare_type_alias(db, type_alias, visitor);
|
walk_manual_pep_695_type_alias(db, type_alias, visitor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9296,8 +9298,8 @@ impl<'db> TypeAliasType<'db> {
|
||||||
TypeAliasType::PEP695(type_alias) => {
|
TypeAliasType::PEP695(type_alias) => {
|
||||||
TypeAliasType::PEP695(type_alias.normalized_impl(db, visitor))
|
TypeAliasType::PEP695(type_alias.normalized_impl(db, visitor))
|
||||||
}
|
}
|
||||||
TypeAliasType::Bare(type_alias) => {
|
TypeAliasType::ManualPEP695(type_alias) => {
|
||||||
TypeAliasType::Bare(type_alias.normalized_impl(db, visitor))
|
TypeAliasType::ManualPEP695(type_alias.normalized_impl(db, visitor))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9305,21 +9307,21 @@ impl<'db> TypeAliasType<'db> {
|
||||||
pub(crate) fn name(self, db: &'db dyn Db) -> &'db str {
|
pub(crate) fn name(self, db: &'db dyn Db) -> &'db str {
|
||||||
match self {
|
match self {
|
||||||
TypeAliasType::PEP695(type_alias) => type_alias.name(db),
|
TypeAliasType::PEP695(type_alias) => type_alias.name(db),
|
||||||
TypeAliasType::Bare(type_alias) => type_alias.name(db),
|
TypeAliasType::ManualPEP695(type_alias) => type_alias.name(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn definition(self, db: &'db dyn Db) -> Option<Definition<'db>> {
|
pub(crate) fn definition(self, db: &'db dyn Db) -> Option<Definition<'db>> {
|
||||||
match self {
|
match self {
|
||||||
TypeAliasType::PEP695(type_alias) => Some(type_alias.definition(db)),
|
TypeAliasType::PEP695(type_alias) => Some(type_alias.definition(db)),
|
||||||
TypeAliasType::Bare(type_alias) => type_alias.definition(db),
|
TypeAliasType::ManualPEP695(type_alias) => type_alias.definition(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn value_type(self, db: &'db dyn Db) -> Type<'db> {
|
pub(crate) fn value_type(self, db: &'db dyn Db) -> Type<'db> {
|
||||||
match self {
|
match self {
|
||||||
TypeAliasType::PEP695(type_alias) => type_alias.value_type(db),
|
TypeAliasType::PEP695(type_alias) => type_alias.value_type(db),
|
||||||
TypeAliasType::Bare(type_alias) => type_alias.value(db),
|
TypeAliasType::ManualPEP695(type_alias) => type_alias.value(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,11 +28,12 @@ use crate::types::infer::nearest_enclosing_class;
|
||||||
use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signature};
|
use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signature};
|
||||||
use crate::types::tuple::{TupleSpec, TupleType};
|
use crate::types::tuple::{TupleSpec, TupleType};
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
ApplyTypeMappingVisitor, BareTypeAliasType, Binding, BoundSuperError, BoundSuperType,
|
ApplyTypeMappingVisitor, Binding, BoundSuperError, BoundSuperType, CallableType,
|
||||||
CallableType, DataclassParams, DeprecatedInstance, HasRelationToVisitor, IsEquivalentVisitor,
|
DataclassParams, DeprecatedInstance, HasRelationToVisitor, IsEquivalentVisitor,
|
||||||
KnownInstanceType, NormalizedVisitor, PropertyInstanceType, StringLiteralType, TypeAliasType,
|
KnownInstanceType, ManualPEP695TypeAliasType, NormalizedVisitor, PropertyInstanceType,
|
||||||
TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance, TypeVarKind,
|
StringLiteralType, TypeAliasType, TypeMapping, TypeRelation, TypeVarBoundOrConstraints,
|
||||||
VarianceInferable, declaration_type, infer_definition_types, todo_type,
|
TypeVarInstance, TypeVarKind, VarianceInferable, declaration_type, infer_definition_types,
|
||||||
|
todo_type,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
Db, FxIndexMap, FxOrderSet, Program,
|
Db, FxIndexMap, FxOrderSet, Program,
|
||||||
|
@ -4935,7 +4936,7 @@ impl KnownClass {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
overload.set_return_type(Type::KnownInstance(KnownInstanceType::TypeAliasType(
|
overload.set_return_type(Type::KnownInstance(KnownInstanceType::TypeAliasType(
|
||||||
TypeAliasType::Bare(BareTypeAliasType::new(
|
TypeAliasType::ManualPEP695(ManualPEP695TypeAliasType::new(
|
||||||
db,
|
db,
|
||||||
ast::name::Name::new(name.value(db)),
|
ast::name::Name::new(name.value(db)),
|
||||||
containing_assignment,
|
containing_assignment,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue