@@ -1527,6 +1527,18 @@ impl<'db> ClassLiteral<'db> {
1527
1527
} )
1528
1528
}
1529
1529
1530
+ /// Returns a specialization of this class where each typevar is mapped to itself. The second
1531
+ /// parameter can be `Type::TypeVar` or `Type::NonInferableTypeVar`, depending on the use case.
1532
+ pub ( crate ) fn identity_specialization (
1533
+ self ,
1534
+ db : & ' db dyn Db ,
1535
+ typevar_to_type : impl Fn ( BoundTypeVarInstance < ' db > ) -> Type < ' db > ,
1536
+ ) -> ClassType < ' db > {
1537
+ self . apply_specialization ( db, |generic_context| {
1538
+ generic_context. identity_specialization ( db, typevar_to_type)
1539
+ } )
1540
+ }
1541
+
1530
1542
/// Return an iterator over the inferred types of this class's *explicit* bases.
1531
1543
///
1532
1544
/// Note that any class (except for `object`) that has no explicit
@@ -2625,7 +2637,14 @@ impl<'db> ClassLiteral<'db> {
2625
2637
. map_type ( |ty|
2626
2638
ty. apply_type_mapping (
2627
2639
db,
2628
- & TypeMapping :: ReplaceSelf { new_upper_bound : determine_upper_bound ( db, self , specialization, ClassBase :: is_typed_dict) }
2640
+ & TypeMapping :: ReplaceSelf {
2641
+ new_upper_bound : determine_upper_bound (
2642
+ db,
2643
+ self ,
2644
+ specialization,
2645
+ ClassBase :: is_typed_dict
2646
+ )
2647
+ }
2629
2648
)
2630
2649
)
2631
2650
}
@@ -4167,6 +4186,90 @@ impl KnownClass {
4167
4186
}
4168
4187
}
4169
4188
4189
+ /// Return `true` if this class is a typeshed fallback class which is used to provide attributes and
4190
+ /// methods for another type (e.g. `NamedTupleFallback` for actual `NamedTuple`s). These fallback
4191
+ /// classes need special treatment in some places. For example, implicit usages of `Self` should not
4192
+ /// be eagerly replaced with the fallback class itself. Instead, `Self` should eventually be treated
4193
+ /// as referring to the destination type (e.g. the actual `NamedTuple`).
4194
+ pub ( crate ) const fn is_fallback_class ( self ) -> bool {
4195
+ match self {
4196
+ KnownClass :: Bool
4197
+ | KnownClass :: Object
4198
+ | KnownClass :: Bytes
4199
+ | KnownClass :: Bytearray
4200
+ | KnownClass :: Type
4201
+ | KnownClass :: Int
4202
+ | KnownClass :: Float
4203
+ | KnownClass :: Complex
4204
+ | KnownClass :: Str
4205
+ | KnownClass :: List
4206
+ | KnownClass :: Tuple
4207
+ | KnownClass :: Set
4208
+ | KnownClass :: FrozenSet
4209
+ | KnownClass :: Dict
4210
+ | KnownClass :: Slice
4211
+ | KnownClass :: Property
4212
+ | KnownClass :: BaseException
4213
+ | KnownClass :: Exception
4214
+ | KnownClass :: BaseExceptionGroup
4215
+ | KnownClass :: ExceptionGroup
4216
+ | KnownClass :: Staticmethod
4217
+ | KnownClass :: Classmethod
4218
+ | KnownClass :: Super
4219
+ | KnownClass :: Enum
4220
+ | KnownClass :: EnumType
4221
+ | KnownClass :: Auto
4222
+ | KnownClass :: Member
4223
+ | KnownClass :: Nonmember
4224
+ | KnownClass :: StrEnum
4225
+ | KnownClass :: ABCMeta
4226
+ | KnownClass :: GenericAlias
4227
+ | KnownClass :: ModuleType
4228
+ | KnownClass :: FunctionType
4229
+ | KnownClass :: MethodType
4230
+ | KnownClass :: MethodWrapperType
4231
+ | KnownClass :: WrapperDescriptorType
4232
+ | KnownClass :: UnionType
4233
+ | KnownClass :: GeneratorType
4234
+ | KnownClass :: AsyncGeneratorType
4235
+ | KnownClass :: CoroutineType
4236
+ | KnownClass :: NotImplementedType
4237
+ | KnownClass :: BuiltinFunctionType
4238
+ | KnownClass :: EllipsisType
4239
+ | KnownClass :: NoneType
4240
+ | KnownClass :: Awaitable
4241
+ | KnownClass :: Generator
4242
+ | KnownClass :: Deprecated
4243
+ | KnownClass :: StdlibAlias
4244
+ | KnownClass :: SpecialForm
4245
+ | KnownClass :: TypeVar
4246
+ | KnownClass :: ParamSpec
4247
+ | KnownClass :: ParamSpecArgs
4248
+ | KnownClass :: ParamSpecKwargs
4249
+ | KnownClass :: ProtocolMeta
4250
+ | KnownClass :: TypeVarTuple
4251
+ | KnownClass :: TypeAliasType
4252
+ | KnownClass :: NoDefaultType
4253
+ | KnownClass :: NewType
4254
+ | KnownClass :: SupportsIndex
4255
+ | KnownClass :: Iterable
4256
+ | KnownClass :: Iterator
4257
+ | KnownClass :: ChainMap
4258
+ | KnownClass :: Counter
4259
+ | KnownClass :: DefaultDict
4260
+ | KnownClass :: Deque
4261
+ | KnownClass :: OrderedDict
4262
+ | KnownClass :: VersionInfo
4263
+ | KnownClass :: Field
4264
+ | KnownClass :: KwOnly
4265
+ | KnownClass :: NamedTupleLike
4266
+ | KnownClass :: Template
4267
+ | KnownClass :: ConstraintSet
4268
+ | KnownClass :: InitVar => false ,
4269
+ KnownClass :: NamedTupleFallback | KnownClass :: TypedDictFallback => true ,
4270
+ }
4271
+ }
4272
+
4170
4273
pub ( crate ) fn name ( self , db : & dyn Db ) -> & ' static str {
4171
4274
match self {
4172
4275
Self :: Bool => "bool" ,
0 commit comments