[ty] Use Type::string_literal() more (#19352)

This commit is contained in:
Alex Waygood 2025-07-15 12:09:07 +01:00 committed by GitHub
parent 8e61da740a
commit 2c9da80985
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 21 deletions

View file

@ -3134,9 +3134,7 @@ impl<'db> Type<'db> {
self.try_call_dunder( self.try_call_dunder(
db, db,
"__getattr__", "__getattr__",
CallArgumentTypes::positional([Type::StringLiteral( CallArgumentTypes::positional([Type::string_literal(db, &name)]),
StringLiteralType::new(db, name.as_str()),
)]),
) )
.map(|outcome| Place::bound(outcome.return_type(db))) .map(|outcome| Place::bound(outcome.return_type(db)))
// TODO: Handle call errors here. // TODO: Handle call errors here.
@ -3155,9 +3153,7 @@ impl<'db> Type<'db> {
self.try_call_dunder_with_policy( self.try_call_dunder_with_policy(
db, db,
"__getattribute__", "__getattribute__",
&mut CallArgumentTypes::positional([Type::StringLiteral( &mut CallArgumentTypes::positional([Type::string_literal(db, &name)]),
StringLiteralType::new(db, name.as_str()),
)]),
MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK, MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK,
) )
.map(|outcome| Place::bound(outcome.return_type(db))) .map(|outcome| Place::bound(outcome.return_type(db)))

View file

@ -1009,28 +1009,22 @@ mod tests {
use crate::Db; use crate::Db;
use crate::db::tests::setup_db; use crate::db::tests::setup_db;
use crate::place::typing_extensions_symbol; use crate::place::typing_extensions_symbol;
use crate::types::{KnownClass, Parameter, Parameters, Signature, StringLiteralType, Type}; use crate::types::{KnownClass, Parameter, Parameters, Signature, Type};
#[test] #[test]
fn string_literal_display() { fn string_literal_display() {
let db = setup_db(); let db = setup_db();
assert_eq!( assert_eq!(
Type::StringLiteral(StringLiteralType::new(&db, r"\n")) Type::string_literal(&db, r"\n").display(&db).to_string(),
.display(&db)
.to_string(),
r#"Literal["\\n"]"# r#"Literal["\\n"]"#
); );
assert_eq!( assert_eq!(
Type::StringLiteral(StringLiteralType::new(&db, "'")) Type::string_literal(&db, "'").display(&db).to_string(),
.display(&db)
.to_string(),
r#"Literal["'"]"# r#"Literal["'"]"#
); );
assert_eq!( assert_eq!(
Type::StringLiteral(StringLiteralType::new(&db, r#"""#)) Type::string_literal(&db, r#"""#).display(&db).to_string(),
.display(&db)
.to_string(),
r#"Literal["\""]"# r#"Literal["\""]"#
); );
} }

View file

@ -113,10 +113,9 @@ use crate::types::{
CallDunderError, CallableType, ClassLiteral, ClassType, DataclassParams, DynamicType, CallDunderError, CallableType, ClassLiteral, ClassType, DataclassParams, DynamicType,
IntersectionBuilder, IntersectionType, KnownClass, KnownInstanceType, LintDiagnosticGuard, IntersectionBuilder, IntersectionType, KnownClass, KnownInstanceType, LintDiagnosticGuard,
MemberLookupPolicy, MetaclassCandidate, PEP695TypeAliasType, Parameter, ParameterForm, MemberLookupPolicy, MetaclassCandidate, PEP695TypeAliasType, Parameter, ParameterForm,
Parameters, SpecialFormType, StringLiteralType, SubclassOfType, Truthiness, Type, Parameters, SpecialFormType, SubclassOfType, Truthiness, Type, TypeAliasType,
TypeAliasType, TypeAndQualifiers, TypeIsType, TypeQualifiers, TypeVarBoundOrConstraints, TypeAndQualifiers, TypeIsType, TypeQualifiers, TypeVarBoundOrConstraints, TypeVarInstance,
TypeVarInstance, TypeVarKind, TypeVarVariance, UnionBuilder, UnionType, binding_type, TypeVarKind, TypeVarVariance, UnionBuilder, UnionType, binding_type, todo_type,
todo_type,
}; };
use crate::unpack::{Unpack, UnpackPosition}; use crate::unpack::{Unpack, UnpackPosition};
use crate::util::diagnostics::format_enumeration; use crate::util::diagnostics::format_enumeration;
@ -3462,7 +3461,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
db, db,
"__setattr__", "__setattr__",
&mut CallArgumentTypes::positional([ &mut CallArgumentTypes::positional([
Type::StringLiteral(StringLiteralType::new(db, attribute)), Type::string_literal(db, attribute),
value_ty, value_ty,
]), ]),
MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK, MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK,