|
1 | 1 | use crate::Db; |
2 | 2 | use crate::types::generics::Specialization; |
3 | 3 | use crate::types::{ |
4 | | - ClassType, DynamicType, KnownClass, MroError, MroIterator, SpecialForm, Type, TypeMapping, |
5 | | - todo_type, |
| 4 | + ClassType, DynamicType, KnownClass, KnownInstanceType, MroError, MroIterator, SpecialFormType, |
| 5 | + Type, TypeMapping, todo_type, |
6 | 6 | }; |
7 | 7 |
|
8 | 8 | /// Enumeration of the possible kinds of types we allow in class bases. |
@@ -147,66 +147,78 @@ impl<'db> ClassBase<'db> { |
147 | 147 | | Type::ProtocolInstance(_) |
148 | 148 | | Type::AlwaysFalsy |
149 | 149 | | Type::AlwaysTruthy => None, |
| 150 | + |
150 | 151 | Type::KnownInstance(known_instance) => match known_instance { |
151 | | - SpecialForm::TypeVar(_) |
152 | | - | SpecialForm::TypeAliasType(_) |
153 | | - | SpecialForm::Annotated |
154 | | - | SpecialForm::Literal |
155 | | - | SpecialForm::LiteralString |
156 | | - | SpecialForm::Union |
157 | | - | SpecialForm::NoReturn |
158 | | - | SpecialForm::Never |
159 | | - | SpecialForm::Final |
160 | | - | SpecialForm::NotRequired |
161 | | - | SpecialForm::TypeGuard |
162 | | - | SpecialForm::TypeIs |
163 | | - | SpecialForm::TypingSelf |
164 | | - | SpecialForm::Unpack |
165 | | - | SpecialForm::ClassVar |
166 | | - | SpecialForm::Concatenate |
167 | | - | SpecialForm::Required |
168 | | - | SpecialForm::TypeAlias |
169 | | - | SpecialForm::ReadOnly |
170 | | - | SpecialForm::Optional |
171 | | - | SpecialForm::Not |
172 | | - | SpecialForm::Intersection |
173 | | - | SpecialForm::TypeOf |
174 | | - | SpecialForm::CallableTypeOf |
175 | | - | SpecialForm::AlwaysTruthy |
176 | | - | SpecialForm::AlwaysFalsy => None, |
177 | | - SpecialForm::Unknown => Some(Self::unknown()), |
| 152 | + KnownInstanceType::Generic(_) => Some(Self::Generic), |
| 153 | + KnownInstanceType::Protocol(_) => Some(Self::Protocol), |
| 154 | + KnownInstanceType::TypeAliasType(_) | KnownInstanceType::TypeVar(_) => None, |
| 155 | + }, |
| 156 | + |
| 157 | + Type::SpecialForm(special_form) => match special_form { |
| 158 | + SpecialFormType::Annotated |
| 159 | + | SpecialFormType::Literal |
| 160 | + | SpecialFormType::LiteralString |
| 161 | + | SpecialFormType::Union |
| 162 | + | SpecialFormType::NoReturn |
| 163 | + | SpecialFormType::Never |
| 164 | + | SpecialFormType::Final |
| 165 | + | SpecialFormType::NotRequired |
| 166 | + | SpecialFormType::TypeGuard |
| 167 | + | SpecialFormType::TypeIs |
| 168 | + | SpecialFormType::TypingSelf |
| 169 | + | SpecialFormType::Unpack |
| 170 | + | SpecialFormType::ClassVar |
| 171 | + | SpecialFormType::Concatenate |
| 172 | + | SpecialFormType::Required |
| 173 | + | SpecialFormType::TypeAlias |
| 174 | + | SpecialFormType::ReadOnly |
| 175 | + | SpecialFormType::Optional |
| 176 | + | SpecialFormType::Not |
| 177 | + | SpecialFormType::Intersection |
| 178 | + | SpecialFormType::TypeOf |
| 179 | + | SpecialFormType::CallableTypeOf |
| 180 | + | SpecialFormType::AlwaysTruthy |
| 181 | + | SpecialFormType::AlwaysFalsy => None, |
| 182 | + |
| 183 | + SpecialFormType::Unknown => Some(Self::unknown()), |
178 | 184 | // TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO |
179 | | - SpecialForm::Dict => Self::try_from_type(db, KnownClass::Dict.to_class_literal(db)), |
180 | | - SpecialForm::List => Self::try_from_type(db, KnownClass::List.to_class_literal(db)), |
181 | | - SpecialForm::Type => Self::try_from_type(db, KnownClass::Type.to_class_literal(db)), |
182 | | - SpecialForm::Tuple => { |
| 185 | + SpecialFormType::Dict => { |
| 186 | + Self::try_from_type(db, KnownClass::Dict.to_class_literal(db)) |
| 187 | + } |
| 188 | + SpecialFormType::List => { |
| 189 | + Self::try_from_type(db, KnownClass::List.to_class_literal(db)) |
| 190 | + } |
| 191 | + SpecialFormType::Type => { |
| 192 | + Self::try_from_type(db, KnownClass::Type.to_class_literal(db)) |
| 193 | + } |
| 194 | + SpecialFormType::Tuple => { |
183 | 195 | Self::try_from_type(db, KnownClass::Tuple.to_class_literal(db)) |
184 | 196 | } |
185 | | - SpecialForm::Set => Self::try_from_type(db, KnownClass::Set.to_class_literal(db)), |
186 | | - SpecialForm::FrozenSet => { |
| 197 | + SpecialFormType::Set => { |
| 198 | + Self::try_from_type(db, KnownClass::Set.to_class_literal(db)) |
| 199 | + } |
| 200 | + SpecialFormType::FrozenSet => { |
187 | 201 | Self::try_from_type(db, KnownClass::FrozenSet.to_class_literal(db)) |
188 | 202 | } |
189 | | - SpecialForm::ChainMap => { |
| 203 | + SpecialFormType::ChainMap => { |
190 | 204 | Self::try_from_type(db, KnownClass::ChainMap.to_class_literal(db)) |
191 | 205 | } |
192 | | - SpecialForm::Counter => { |
| 206 | + SpecialFormType::Counter => { |
193 | 207 | Self::try_from_type(db, KnownClass::Counter.to_class_literal(db)) |
194 | 208 | } |
195 | | - SpecialForm::DefaultDict => { |
| 209 | + SpecialFormType::DefaultDict => { |
196 | 210 | Self::try_from_type(db, KnownClass::DefaultDict.to_class_literal(db)) |
197 | 211 | } |
198 | | - SpecialForm::Deque => { |
| 212 | + SpecialFormType::Deque => { |
199 | 213 | Self::try_from_type(db, KnownClass::Deque.to_class_literal(db)) |
200 | 214 | } |
201 | | - SpecialForm::OrderedDict => { |
| 215 | + SpecialFormType::OrderedDict => { |
202 | 216 | Self::try_from_type(db, KnownClass::OrderedDict.to_class_literal(db)) |
203 | 217 | } |
204 | | - SpecialForm::TypedDict => Self::try_from_type(db, todo_type!("TypedDict")), |
205 | | - SpecialForm::Callable => { |
| 218 | + SpecialFormType::TypedDict => Self::try_from_type(db, todo_type!("TypedDict")), |
| 219 | + SpecialFormType::Callable => { |
206 | 220 | Self::try_from_type(db, todo_type!("Support for Callable as a base class")) |
207 | 221 | } |
208 | | - SpecialForm::Protocol(_) => Some(ClassBase::Protocol), |
209 | | - SpecialForm::Generic(_) => Some(ClassBase::Generic), |
210 | 222 | }, |
211 | 223 | } |
212 | 224 | } |
@@ -280,8 +292,8 @@ impl<'db> From<ClassBase<'db>> for Type<'db> { |
280 | 292 | match value { |
281 | 293 | ClassBase::Dynamic(dynamic) => Type::Dynamic(dynamic), |
282 | 294 | ClassBase::Class(class) => class.into(), |
283 | | - ClassBase::Protocol => Type::KnownInstance(SpecialForm::Protocol(None)), |
284 | | - ClassBase::Generic => Type::KnownInstance(SpecialForm::Generic(None)), |
| 295 | + ClassBase::Protocol => Type::KnownInstance(KnownInstanceType::Protocol(None)), |
| 296 | + ClassBase::Generic => Type::KnownInstance(KnownInstanceType::Generic(None)), |
285 | 297 | } |
286 | 298 | } |
287 | 299 | } |
|
0 commit comments