@@ -1855,12 +1855,44 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
18551855 )
18561856 }
18571857 }
1858- Type :: ClassDef ( cls) => Type :: type_form ( self . specialize (
1859- & cls,
1860- xs. map ( |x| self . expr_untype ( x, TypeFormContext :: TypeArgument , errors) ) ,
1861- range,
1862- errors,
1863- ) ) ,
1858+ Type :: ClassDef ( cls) => {
1859+ let metadata = self . get_metadata_for_class ( & cls) ;
1860+ let class_getitem_result = if self . get_class_tparams ( & cls) . is_empty ( )
1861+ && !metadata. has_base_any ( )
1862+ && !metadata. is_new_type ( )
1863+ {
1864+ let class_ty = Type :: ClassDef ( cls. dupe ( ) ) ;
1865+ // TODO(stroxler): Add a new API, similar to `type_of_attr_get` but returning a
1866+ // LookupResult or an Optional type, that we could use here to avoid the double lookup.
1867+ if self . has_attr ( & class_ty, & dunder:: CLASS_GETITEM ) {
1868+ let cls_value = self . promote_silently ( & cls) ;
1869+ let call_args = [ CallArg :: ty ( & cls_value, range) , CallArg :: expr ( slice) ] ;
1870+ Some ( self . call_method_or_error (
1871+ & class_ty,
1872+ & dunder:: CLASS_GETITEM ,
1873+ range,
1874+ & call_args,
1875+ & [ ] ,
1876+ errors,
1877+ Some ( & || ErrorContext :: Index ( self . for_display ( class_ty. clone ( ) ) ) ) ,
1878+ ) )
1879+ } else {
1880+ None
1881+ }
1882+ } else {
1883+ None
1884+ } ;
1885+ if let Some ( result) = class_getitem_result {
1886+ result
1887+ } else {
1888+ Type :: type_form ( self . specialize (
1889+ & cls,
1890+ xs. map ( |x| self . expr_untype ( x, TypeFormContext :: TypeArgument , errors) ) ,
1891+ range,
1892+ errors,
1893+ ) )
1894+ }
1895+ }
18641896 Type :: Type ( box Type :: SpecialForm ( special) ) => {
18651897 self . apply_special_form ( special, slice, range, errors)
18661898 }
0 commit comments