mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 05:20:49 -05:00
[ty] Use Type::string_literal() more (#22184)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use compact_str::{CompactString, ToCompactString};
|
||||
use compact_str::CompactString;
|
||||
use infer::nearest_enclosing_class;
|
||||
use itertools::{Either, Itertools};
|
||||
use ruff_diagnostics::{Edit, Fix};
|
||||
@@ -5224,11 +5224,7 @@ impl<'db> Type<'db> {
|
||||
&& Type::ClassLiteral(enum_literal.enum_class(db))
|
||||
.is_subtype_of(db, KnownClass::Enum.to_subclass_of(db)) =>
|
||||
{
|
||||
Place::bound(Type::StringLiteral(StringLiteralType::new(
|
||||
db,
|
||||
enum_literal.name(db).as_str(),
|
||||
)))
|
||||
.into()
|
||||
Place::bound(Type::string_literal(db, enum_literal.name(db))).into()
|
||||
}
|
||||
|
||||
Type::EnumLiteral(enum_literal)
|
||||
@@ -8388,10 +8384,9 @@ impl<'db> Type<'db> {
|
||||
),
|
||||
),
|
||||
Type::SpecialForm(special_form) => Type::string_literal(db, &special_form.to_string()),
|
||||
Type::KnownInstance(known_instance) => Type::StringLiteral(StringLiteralType::new(
|
||||
db,
|
||||
known_instance.repr(db).to_compact_string(),
|
||||
)),
|
||||
Type::KnownInstance(known_instance) => {
|
||||
Type::string_literal(db, &known_instance.repr(db).to_string())
|
||||
}
|
||||
// TODO: handle more complex types
|
||||
_ => KnownClass::Str.to_instance(db),
|
||||
}
|
||||
@@ -8410,10 +8405,9 @@ impl<'db> Type<'db> {
|
||||
}
|
||||
Type::LiteralString => Type::LiteralString,
|
||||
Type::SpecialForm(special_form) => Type::string_literal(db, &special_form.to_string()),
|
||||
Type::KnownInstance(known_instance) => Type::StringLiteral(StringLiteralType::new(
|
||||
db,
|
||||
known_instance.repr(db).to_compact_string(),
|
||||
)),
|
||||
Type::KnownInstance(known_instance) => {
|
||||
Type::string_literal(db, &known_instance.repr(db).to_string())
|
||||
}
|
||||
// TODO: handle more complex types
|
||||
_ => KnownClass::Str.to_instance(db),
|
||||
}
|
||||
|
||||
@@ -39,9 +39,9 @@ use crate::types::{
|
||||
CallableTypeKind, CallableTypes, DATACLASS_FLAGS, DataclassFlags, DataclassParams,
|
||||
DeprecatedInstance, FindLegacyTypeVarsVisitor, HasRelationToVisitor, IsDisjointVisitor,
|
||||
IsEquivalentVisitor, KnownInstanceType, ManualPEP695TypeAliasType, MaterializationKind,
|
||||
NormalizedVisitor, PropertyInstanceType, StringLiteralType, TypeAliasType, TypeContext,
|
||||
TypeMapping, TypeRelation, TypedDictParams, UnionBuilder, VarianceInferable, binding_type,
|
||||
declaration_type, determine_upper_bound,
|
||||
NormalizedVisitor, PropertyInstanceType, TypeAliasType, TypeContext, TypeMapping, TypeRelation,
|
||||
TypedDictParams, UnionBuilder, VarianceInferable, binding_type, declaration_type,
|
||||
determine_upper_bound,
|
||||
};
|
||||
use crate::{
|
||||
Db, FxIndexMap, FxIndexSet, FxOrderSet, Program,
|
||||
@@ -2773,7 +2773,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
}
|
||||
|
||||
let overloads = writeable_fields.map(|(name, field)| {
|
||||
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
Signature::new(
|
||||
Parameters::new(
|
||||
@@ -2803,7 +2803,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
|
||||
// Add (key -> value type) overloads for all TypedDict items ("fields"):
|
||||
let overloads = fields.iter().map(|(name, field)| {
|
||||
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
Signature::new(
|
||||
Parameters::new(
|
||||
@@ -2861,7 +2861,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
|
||||
// Otherwise, add overloads for all deletable fields.
|
||||
let overloads = deletable_fields.map(|(name, _field)| {
|
||||
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
Signature::new(
|
||||
Parameters::new(
|
||||
@@ -2889,8 +2889,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
.fields(db, specialization, field_policy)
|
||||
.iter()
|
||||
.flat_map(|(name, field)| {
|
||||
let key_type =
|
||||
Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
// For a required key, `.get()` always returns the value type. For a non-required key,
|
||||
// `.get()` returns the union of the value type and the type of the default argument
|
||||
@@ -3002,8 +3001,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
!field.is_required()
|
||||
})
|
||||
.flat_map(|(name, field)| {
|
||||
let key_type =
|
||||
Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
// TODO: Similar to above: consider merging these two overloads into one
|
||||
|
||||
@@ -3057,7 +3055,7 @@ impl<'db> ClassLiteral<'db> {
|
||||
(CodeGeneratorKind::TypedDict, "setdefault") => {
|
||||
let fields = self.fields(db, specialization, field_policy);
|
||||
let overloads = fields.iter().map(|(name, field)| {
|
||||
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
|
||||
let key_type = Type::string_literal(db, name);
|
||||
|
||||
// `setdefault` always returns the field type
|
||||
Signature::new(
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
semantic_index::{place_table, use_def_map},
|
||||
types::{
|
||||
ClassBase, ClassLiteral, DynamicType, EnumLiteralType, KnownClass, MemberLookupPolicy,
|
||||
StringLiteralType, Type, TypeQualifiers,
|
||||
Type, TypeQualifiers,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -139,10 +139,7 @@ pub(crate) fn enum_metadata<'db>(
|
||||
let auto_value_ty = if Type::ClassLiteral(class)
|
||||
.is_subtype_of(db, KnownClass::StrEnum.to_subclass_of(db))
|
||||
{
|
||||
Type::StringLiteral(StringLiteralType::new(
|
||||
db,
|
||||
name.to_lowercase().as_str(),
|
||||
))
|
||||
Type::string_literal(db, &name.to_lowercase())
|
||||
} else {
|
||||
let custom_mixins: smallvec::SmallVec<[Option<KnownClass>; 1]> =
|
||||
class
|
||||
|
||||
@@ -8791,9 +8791,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
}
|
||||
} else {
|
||||
// Key not found, report error with suggestion and return early
|
||||
let key_ty = Type::StringLiteral(
|
||||
crate::types::StringLiteralType::new(self.db(), key),
|
||||
);
|
||||
let key_ty = Type::string_literal(self.db(), key);
|
||||
report_invalid_key_on_typed_dict(
|
||||
&self.context,
|
||||
first_arg.into(),
|
||||
|
||||
Reference in New Issue
Block a user