[ty] Always pass NO_INSTANCE_FALLBACK in try_call_dunder_with_policy (#18315)

## Summary

The previous `try_call_dunder_with_policy` API was a bit of a footgun
since you needed to pass `NO_INSTANCE_FALLBACK` in *addition* to other
policies that you wanted for the member lookup. Implicit calls to dunder
methods never access instance members though, so we can do this
implicitly in `try_call_dunder_with_policy`.

No functional changes.
This commit is contained in:
David Peter 2025-05-26 13:20:27 +02:00 committed by GitHub
parent f885cb8a2f
commit e1b662bf5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View file

@ -3214,8 +3214,7 @@ impl<'db> Type<'db> {
&mut CallArgumentTypes::positional([Type::StringLiteral( &mut CallArgumentTypes::positional([Type::StringLiteral(
StringLiteralType::new(db, Box::from(name.as_str())), StringLiteralType::new(db, Box::from(name.as_str())),
)]), )]),
MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK,
| MemberLookupPolicy::NO_INSTANCE_FALLBACK,
) )
.map(|outcome| Symbol::bound(outcome.return_type(db))) .map(|outcome| Symbol::bound(outcome.return_type(db)))
// TODO: Handle call errors here. // TODO: Handle call errors here.
@ -4415,7 +4414,7 @@ impl<'db> Type<'db> {
db, db,
name, name,
&mut argument_types, &mut argument_types,
MemberLookupPolicy::NO_INSTANCE_FALLBACK, MemberLookupPolicy::default(),
) )
} }
@ -4423,6 +4422,9 @@ impl<'db> Type<'db> {
/// particular, this allows to specify `MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK` to avoid /// particular, this allows to specify `MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK` to avoid
/// looking up dunder methods on `object`, which is needed for functions like `__init__`, /// looking up dunder methods on `object`, which is needed for functions like `__init__`,
/// `__new__`, or `__setattr__`. /// `__new__`, or `__setattr__`.
///
/// Note that `NO_INSTANCE_FALLBACK` is always added to the policy, since implicit calls to
/// dunder methods never access instance members.
fn try_call_dunder_with_policy( fn try_call_dunder_with_policy(
self, self,
db: &'db dyn Db, db: &'db dyn Db,
@ -4430,8 +4432,14 @@ impl<'db> Type<'db> {
argument_types: &mut CallArgumentTypes<'_, 'db>, argument_types: &mut CallArgumentTypes<'_, 'db>,
policy: MemberLookupPolicy, policy: MemberLookupPolicy,
) -> Result<Bindings<'db>, CallDunderError<'db>> { ) -> Result<Bindings<'db>, CallDunderError<'db>> {
// Implicit calls to dunder methods never access instance members, so we pass
// `NO_INSTANCE_FALLBACK` here in addition to other policies:
match self match self
.member_lookup_with_policy(db, name.into(), policy) .member_lookup_with_policy(
db,
name.into(),
policy | MemberLookupPolicy::NO_INSTANCE_FALLBACK,
)
.symbol .symbol
{ {
Symbol::Type(dunder_callable, boundness) => { Symbol::Type(dunder_callable, boundness) => {

View file

@ -3231,8 +3231,7 @@ impl<'db> TypeInferenceBuilder<'db> {
)), )),
value_ty, value_ty,
]), ]),
MemberLookupPolicy::NO_INSTANCE_FALLBACK MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK,
| MemberLookupPolicy::MRO_NO_OBJECT_FALLBACK,
); );
match result { match result {