@@ -108,7 +108,7 @@ use super::diagnostic::{
108108 report_runtime_check_against_non_runtime_checkable_protocol, report_slice_step_size_zero,
109109 report_unresolved_reference,
110110} ;
111- use super :: generics:: { GenericContextOrigin , LegacyGenericBase } ;
111+ use super :: generics:: LegacyGenericBase ;
112112use super :: slots:: check_class_slots;
113113use super :: string_annotation:: {
114114 BYTE_STRING_TYPE_ANNOTATION , FSTRING_TYPE_ANNOTATION , parse_string_annotation,
@@ -856,6 +856,25 @@ impl<'db> TypeInferenceBuilder<'db> {
856856 }
857857 continue ;
858858 }
859+ // Note that unlike several of the other errors caught in this function,
860+ // this does not lead to the class creation failing at runtime,
861+ // but it is semantically invalid.
862+ Type :: KnownInstance ( KnownInstanceType :: Protocol ( Some ( _) ) ) => {
863+ if class_node. type_params . is_none ( ) {
864+ continue ;
865+ }
866+ let Some ( builder) = self
867+ . context
868+ . report_lint ( & INVALID_GENERIC_CLASS , & class_node. bases ( ) [ i] )
869+ else {
870+ continue ;
871+ } ;
872+ builder. into_diagnostic (
873+ "Cannot both inherit from subscripted `Protocol` \
874+ and use PEP 695 type variables",
875+ ) ;
876+ continue ;
877+ }
859878 Type :: ClassLiteral ( class) => class,
860879 // dynamic/unknown bases are never `@final`
861880 _ => continue ,
@@ -912,27 +931,28 @@ impl<'db> TypeInferenceBuilder<'db> {
912931 }
913932 }
914933 MroErrorKind :: UnresolvableMro { bases_list } => {
915- let has_conflicting_generic_contexts = class
916- . has_pep_695_type_params ( self . db ( ) )
917- && class. has_legacy_generic_context ( self . db ( ) ) ;
918-
919- // Skip reporting the error if the class has conflicting generic contexts.
920- // We'll report an error below for that with a nicer error message;
921- // there's not much need to have two diagnostics for the same underlying problem.
922- if !has_conflicting_generic_contexts {
923- if let Some ( builder) =
924- self . context . report_lint ( & INCONSISTENT_MRO , class_node)
925- {
926- builder. into_diagnostic ( format_args ! (
927- "Cannot create a consistent method resolution order (MRO) \
928- for class `{}` with bases list `[{}]`",
929- class. name( self . db( ) ) ,
930- bases_list
931- . iter( )
932- . map( |base| base. display( self . db( ) ) )
933- . join( ", " )
934- ) ) ;
935- }
934+ if let Some ( builder) =
935+ self . context . report_lint ( & INCONSISTENT_MRO , class_node)
936+ {
937+ builder. into_diagnostic ( format_args ! (
938+ "Cannot create a consistent method resolution order (MRO) \
939+ for class `{}` with bases list `[{}]`",
940+ class. name( self . db( ) ) ,
941+ bases_list
942+ . iter( )
943+ . map( |base| base. display( self . db( ) ) )
944+ . join( ", " )
945+ ) ) ;
946+ }
947+ }
948+ MroErrorKind :: Pep695ClassWithGenericInheritance => {
949+ if let Some ( builder) =
950+ self . context . report_lint ( & INVALID_GENERIC_CLASS , class_node)
951+ {
952+ builder. into_diagnostic (
953+ "Cannot both inherit from `typing.Generic` \
954+ and use PEP 695 type variables",
955+ ) ;
936956 }
937957 }
938958 MroErrorKind :: InheritanceCycle => {
@@ -1031,21 +1051,6 @@ impl<'db> TypeInferenceBuilder<'db> {
10311051 }
10321052 }
10331053
1034- // (5) Check that a generic class does not have invalid or conflicting generic
1035- // contexts.
1036- if class. pep695_generic_context ( self . db ( ) ) . is_some ( ) {
1037- if let Some ( legacy_context) = class. legacy_generic_context ( self . db ( ) ) {
1038- if let Some ( builder) =
1039- self . context . report_lint ( & INVALID_GENERIC_CLASS , class_node)
1040- {
1041- builder. into_diagnostic ( format_args ! (
1042- "Cannot both inherit from {} and use PEP 695 type variables" ,
1043- legacy_context. origin( self . db( ) )
1044- ) ) ;
1045- }
1046- }
1047- }
1048-
10491054 if let ( Some ( legacy) , Some ( inherited) ) = (
10501055 class. legacy_generic_context ( self . db ( ) ) ,
10511056 class. inherited_legacy_generic_context ( self . db ( ) ) ,
@@ -7631,17 +7636,15 @@ impl<'db> TypeInferenceBuilder<'db> {
76317636 self . context . report_lint ( & INVALID_ARGUMENT_TYPE , value_node)
76327637 {
76337638 builder. into_diagnostic ( format_args ! (
7634- "`{}` is not a valid argument to {origin}" ,
7639+ "`{}` is not a valid argument to ` {origin}` " ,
76357640 typevar. display( self . db( ) ) ,
76367641 ) ) ;
76377642 }
76387643 None
76397644 }
76407645 } )
76417646 . collect ( ) ;
7642- typevars. map ( |typevars| {
7643- GenericContext :: new ( self . db ( ) , typevars, GenericContextOrigin :: from ( origin) )
7644- } )
7647+ typevars. map ( |typevars| GenericContext :: new ( self . db ( ) , typevars) )
76457648 }
76467649
76477650 fn infer_slice_expression ( & mut self , slice : & ast:: ExprSlice ) -> Type < ' db > {
0 commit comments