[ty] Add an instance of an Any subclass to the property tests (#19180)

This commit is contained in:
Alex Waygood 2025-07-08 10:53:50 +01:00 committed by GitHub
parent 1ddda241f6
commit 220a584c11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View file

@ -156,6 +156,9 @@ pub enum KnownModule {
TyExtensions, TyExtensions,
#[strum(serialize = "importlib")] #[strum(serialize = "importlib")]
ImportLib, ImportLib,
#[cfg(test)]
#[strum(serialize = "unittest.mock")]
UnittestMock,
} }
impl KnownModule { impl KnownModule {
@ -175,6 +178,8 @@ impl KnownModule {
Self::TypeCheckerInternals => "_typeshed._type_checker_internals", Self::TypeCheckerInternals => "_typeshed._type_checker_internals",
Self::TyExtensions => "ty_extensions", Self::TyExtensions => "ty_extensions",
Self::ImportLib => "importlib", Self::ImportLib => "importlib",
#[cfg(test)]
Self::UnittestMock => "unittest.mock",
} }
} }

View file

@ -55,6 +55,10 @@ pub(crate) enum Ty {
params: CallableParams, params: CallableParams,
returns: Option<Box<Ty>>, returns: Option<Box<Ty>>,
}, },
/// `unittest.mock.Mock` is interesting because it is a nominal instance type
/// where the class has `Any` in its MRO
UnittestMockInstance,
UnittestMockLiteral,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -144,6 +148,13 @@ impl Ty {
Ty::AbcClassLiteral(s) => known_module_symbol(db, KnownModule::Abc, s) Ty::AbcClassLiteral(s) => known_module_symbol(db, KnownModule::Abc, s)
.place .place
.expect_type(), .expect_type(),
Ty::UnittestMockLiteral => known_module_symbol(db, KnownModule::UnittestMock, "Mock")
.place
.expect_type(),
Ty::UnittestMockInstance => Ty::UnittestMockLiteral
.into_type(db)
.to_instance(db)
.unwrap(),
Ty::TypingLiteral => Type::SpecialForm(SpecialFormType::Literal), Ty::TypingLiteral => Type::SpecialForm(SpecialFormType::Literal),
Ty::BuiltinClassLiteral(s) => builtins_symbol(db, s).place.expect_type(), Ty::BuiltinClassLiteral(s) => builtins_symbol(db, s).place.expect_type(),
Ty::KnownClassInstance(known_class) => known_class.to_instance(db), Ty::KnownClassInstance(known_class) => known_class.to_instance(db),
@ -223,11 +234,13 @@ fn arbitrary_core_type(g: &mut Gen, fully_static: bool) -> Ty {
let bool_lit = Ty::BooleanLiteral(bool::arbitrary(g)); let bool_lit = Ty::BooleanLiteral(bool::arbitrary(g));
// Update this if new non-fully-static types are added below. // Update this if new non-fully-static types are added below.
let fully_static_index = 3; let fully_static_index = 5;
let types = &[ let types = &[
Ty::Any, Ty::Any,
Ty::Unknown, Ty::Unknown,
Ty::SubclassOfAny, Ty::SubclassOfAny,
Ty::UnittestMockLiteral,
Ty::UnittestMockInstance,
// Add fully static types below, dynamic types above. // Add fully static types below, dynamic types above.
// Update `fully_static_index` above if adding new dynamic types! // Update `fully_static_index` above if adding new dynamic types!
Ty::Never, Ty::Never,