@@ -1327,12 +1327,13 @@ impl<'db> TypeInferenceBuilder<'db> {
1327
1327
& mut self ,
1328
1328
node : AnyNodeRef ,
1329
1329
non_subscriptable_ty : Type < ' db > ,
1330
+ method : & str ,
1330
1331
) {
1331
1332
self . add_diagnostic (
1332
1333
node,
1333
1334
"non-subscriptable" ,
1334
1335
format_args ! (
1335
- "Cannot subscript object of type '{}' with no `__getitem__ ` method." ,
1336
+ "Cannot subscript object of type '{}' with no `{method} ` method." ,
1336
1337
non_subscriptable_ty. display( self . db)
1337
1338
) ,
1338
1339
) ;
@@ -2627,10 +2628,16 @@ impl<'db> TypeInferenceBuilder<'db> {
2627
2628
. call ( self . db , & [ slice_ty] )
2628
2629
. unwrap_with_diagnostic ( self . db , value. as_ref ( ) . into ( ) , self ) ;
2629
2630
}
2631
+
2632
+ self . non_subscriptable_diagnostic (
2633
+ ( & * * value) . into ( ) ,
2634
+ value_ty,
2635
+ "__class_getitem__" ,
2636
+ ) ;
2637
+ } else {
2638
+ self . non_subscriptable_diagnostic ( ( & * * value) . into ( ) , value_ty, "__getitem__" ) ;
2630
2639
}
2631
2640
2632
- // Otherwise, emit a diagnostic.
2633
- self . non_subscriptable_diagnostic ( ( & * * value) . into ( ) , value_ty) ;
2634
2641
Type :: Unknown
2635
2642
}
2636
2643
}
@@ -6791,6 +6798,30 @@ mod tests {
6791
6798
Ok ( ( ) )
6792
6799
}
6793
6800
6801
+ #[ test]
6802
+ fn subscript_class_getitem_unbound ( ) -> anyhow:: Result < ( ) > {
6803
+ let mut db = setup_db ( ) ;
6804
+
6805
+ db. write_dedented (
6806
+ "/src/a.py" ,
6807
+ "
6808
+ class NotSubscriptable:
6809
+ pass
6810
+
6811
+ a = NotSubscriptable[0]
6812
+ " ,
6813
+ ) ?;
6814
+
6815
+ assert_public_ty ( & db, "/src/a.py" , "a" , "Unknown" ) ;
6816
+ assert_file_diagnostics (
6817
+ & db,
6818
+ "/src/a.py" ,
6819
+ & [ "Cannot subscript object of type 'Literal[NotSubscriptable]' with no `__class_getitem__` method." ] ,
6820
+ ) ;
6821
+
6822
+ Ok ( ( ) )
6823
+ }
6824
+
6794
6825
#[ test]
6795
6826
fn subscript_not_callable_getitem ( ) -> anyhow:: Result < ( ) > {
6796
6827
let mut db = setup_db ( ) ;
0 commit comments