@@ -4821,7 +4821,7 @@ impl<'db> Type<'db> {
48214821 pub fn in_type_expression (
48224822 & self ,
48234823 db : & ' db dyn Db ,
4824- scope_id : ScopeId ,
4824+ scope_id : ScopeId < ' db > ,
48254825 ) -> Result < Type < ' db > , InvalidTypeExpressionError < ' db > > {
48264826 match self {
48274827 // Special cases for `float` and `complex`
@@ -4872,7 +4872,9 @@ impl<'db> Type<'db> {
48724872 | Type :: BoundSuper ( _)
48734873 | Type :: ProtocolInstance ( _)
48744874 | Type :: PropertyInstance ( _) => Err ( InvalidTypeExpressionError {
4875- invalid_expressions : smallvec:: smallvec![ InvalidTypeExpression :: InvalidType ( * self ) ] ,
4875+ invalid_expressions : smallvec:: smallvec![ InvalidTypeExpression :: InvalidType (
4876+ * self , scope_id
4877+ ) ] ,
48764878 fallback_type : Type :: unknown ( ) ,
48774879 } ) ,
48784880
@@ -4910,7 +4912,7 @@ impl<'db> Type<'db> {
49104912 return Err ( InvalidTypeExpressionError {
49114913 fallback_type : Type :: unknown ( ) ,
49124914 invalid_expressions : smallvec:: smallvec![
4913- InvalidTypeExpression :: InvalidType ( * self )
4915+ InvalidTypeExpression :: InvalidType ( * self , scope_id )
49144916 ] ,
49154917 } ) ;
49164918 } ;
@@ -5037,7 +5039,7 @@ impl<'db> Type<'db> {
50375039 ) ) ,
50385040 _ => Err ( InvalidTypeExpressionError {
50395041 invalid_expressions : smallvec:: smallvec![ InvalidTypeExpression :: InvalidType (
5040- * self
5042+ * self , scope_id
50415043 ) ] ,
50425044 fallback_type : Type :: unknown ( ) ,
50435045 } ) ,
@@ -5751,7 +5753,8 @@ impl<'db> InvalidTypeExpressionError<'db> {
57515753 let Some ( builder) = context. report_lint ( & INVALID_TYPE_FORM , node) else {
57525754 continue ;
57535755 } ;
5754- builder. into_diagnostic ( error. reason ( context. db ( ) ) ) ;
5756+ let diagnostic = builder. into_diagnostic ( error. reason ( context. db ( ) ) ) ;
5757+ error. add_subdiagnostics ( context. db ( ) , diagnostic) ;
57555758 }
57565759 }
57575760 fallback_type
@@ -5778,7 +5781,7 @@ enum InvalidTypeExpression<'db> {
57785781 /// and which would require exactly one argument even if they appeared in an annotation expression
57795782 TypeQualifierRequiresOneArgument ( KnownInstanceType < ' db > ) ,
57805783 /// Some types are always invalid in type expressions
5781- InvalidType ( Type < ' db > ) ,
5784+ InvalidType ( Type < ' db > , ScopeId < ' db > ) ,
57825785}
57835786
57845787impl < ' db > InvalidTypeExpression < ' db > {
@@ -5822,7 +5825,7 @@ impl<'db> InvalidTypeExpression<'db> {
58225825 "Type qualifier `{q}` is not allowed in type expressions (only in annotation expressions, and only with exactly one argument)" ,
58235826 q = qualifier. repr( self . db)
58245827 ) ,
5825- InvalidTypeExpression :: InvalidType ( ty) => write ! (
5828+ InvalidTypeExpression :: InvalidType ( ty, _ ) => write ! (
58265829 f,
58275830 "Variable of type `{ty}` is not allowed in a type expression" ,
58285831 ty = ty. display( self . db)
@@ -5833,6 +5836,38 @@ impl<'db> InvalidTypeExpression<'db> {
58335836
58345837 Display { error : self , db }
58355838 }
5839+
5840+ fn add_subdiagnostics ( self , db : & ' db dyn Db , mut diagnostic : LintDiagnosticGuard ) {
5841+ let InvalidTypeExpression :: InvalidType ( ty, scope) = self else {
5842+ return ;
5843+ } ;
5844+ let Type :: ModuleLiteral ( module_type) = ty else {
5845+ return ;
5846+ } ;
5847+ let module = module_type. module ( db) ;
5848+ let Some ( module_name_final_part) = module. name ( ) . components ( ) . next_back ( ) else {
5849+ return ;
5850+ } ;
5851+ let Some ( module_member_with_same_name) = ty
5852+ . member ( db, module_name_final_part)
5853+ . symbol
5854+ . ignore_possibly_unbound ( )
5855+ else {
5856+ return ;
5857+ } ;
5858+ if module_member_with_same_name
5859+ . in_type_expression ( db, scope)
5860+ . is_err ( )
5861+ {
5862+ return ;
5863+ }
5864+
5865+ // TODO: showing a diff (and even having an autofix) would be even better
5866+ diagnostic. info ( format_args ! (
5867+ "Did you mean to use the module's member \
5868+ `{module_name_final_part}.{module_name_final_part}` instead?"
5869+ ) ) ;
5870+ }
58365871}
58375872
58385873/// Whether this typecar was created via the legacy `TypeVar` constructor, or using PEP 695 syntax.
0 commit comments