@@ -3201,16 +3201,16 @@ impl KnownClass {
32013201 }
32023202 }
32033203
3204- /// Evaluate a call to this known class, and emit any diagnostics that are necessary
3205- /// as a result of the call.
3204+ /// Evaluate a call to this known class, emit any diagnostics that are necessary
3205+ /// as a result of the call, and return the type that results from the call .
32063206 pub ( super ) fn check_call < ' db > (
32073207 self ,
32083208 context : & InferContext < ' db , ' _ > ,
32093209 index : & SemanticIndex < ' db > ,
3210- overload_binding : & mut Binding < ' db > ,
3210+ overload_binding : & Binding < ' db > ,
32113211 call_argument_types : & CallArgumentTypes < ' _ , ' db > ,
32123212 call_expression : & ast:: ExprCall ,
3213- ) {
3213+ ) -> Option < Type < ' db > > {
32143214 let db = context. db ( ) ;
32153215 let scope = context. scope ( ) ;
32163216 let module = context. module ( ) ;
@@ -3226,10 +3226,9 @@ impl KnownClass {
32263226 let Some ( enclosing_class) =
32273227 nearest_enclosing_class ( db, index, scope, module)
32283228 else {
3229- overload_binding. set_return_type ( Type :: unknown ( ) ) ;
32303229 BoundSuperError :: UnavailableImplicitArguments
32313230 . report_diagnostic ( context, call_expression. into ( ) ) ;
3232- return ;
3231+ return Some ( Type :: unknown ( ) ) ;
32333232 } ;
32343233
32353234 // The type of the first parameter if the given scope is function-like (i.e. function or lambda).
@@ -3249,10 +3248,9 @@ impl KnownClass {
32493248 } ;
32503249
32513250 let Some ( first_param) = first_param else {
3252- overload_binding. set_return_type ( Type :: unknown ( ) ) ;
32533251 BoundSuperError :: UnavailableImplicitArguments
32543252 . report_diagnostic ( context, call_expression. into ( ) ) ;
3255- return ;
3253+ return Some ( Type :: unknown ( ) ) ;
32563254 } ;
32573255
32583256 let definition = index. expect_single_definition ( first_param) ;
@@ -3269,7 +3267,7 @@ impl KnownClass {
32693267 Type :: unknown ( )
32703268 } ) ;
32713269
3272- overload_binding . set_return_type ( bound_super) ;
3270+ Some ( bound_super)
32733271 }
32743272 [ Some ( pivot_class_type) , Some ( owner_type) ] => {
32753273 let bound_super = BoundSuperType :: build ( db, * pivot_class_type, * owner_type)
@@ -3278,9 +3276,9 @@ impl KnownClass {
32783276 Type :: unknown ( )
32793277 } ) ;
32803278
3281- overload_binding . set_return_type ( bound_super) ;
3279+ Some ( bound_super)
32823280 }
3283- _ => { }
3281+ _ => None ,
32843282 }
32853283 }
32863284
@@ -3295,14 +3293,12 @@ impl KnownClass {
32953293 _ => None ,
32963294 }
32973295 } ) else {
3298- if let Some ( builder) =
3299- context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression)
3300- {
3301- builder. into_diagnostic (
3302- "A legacy `typing.TypeVar` must be immediately assigned to a variable" ,
3303- ) ;
3304- }
3305- return ;
3296+ let builder =
3297+ context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression) ?;
3298+ builder. into_diagnostic (
3299+ "A legacy `typing.TypeVar` must be immediately assigned to a variable" ,
3300+ ) ;
3301+ return None ;
33063302 } ;
33073303
33083304 let [
@@ -3315,7 +3311,7 @@ impl KnownClass {
33153311 _infer_variance,
33163312 ] = overload_binding. parameter_types ( )
33173313 else {
3318- return ;
3314+ return None ;
33193315 } ;
33203316
33213317 let covariant = covariant
@@ -3328,39 +3324,30 @@ impl KnownClass {
33283324
33293325 let variance = match ( contravariant, covariant) {
33303326 ( Truthiness :: Ambiguous , _) => {
3331- let Some ( builder) =
3332- context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression)
3333- else {
3334- return ;
3335- } ;
3327+ let builder =
3328+ context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression) ?;
33363329 builder. into_diagnostic (
33373330 "The `contravariant` parameter of a legacy `typing.TypeVar` \
33383331 cannot have an ambiguous value",
33393332 ) ;
3340- return ;
3333+ return None ;
33413334 }
33423335 ( _, Truthiness :: Ambiguous ) => {
3343- let Some ( builder) =
3344- context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression)
3345- else {
3346- return ;
3347- } ;
3336+ let builder =
3337+ context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression) ?;
33483338 builder. into_diagnostic (
33493339 "The `covariant` parameter of a legacy `typing.TypeVar` \
33503340 cannot have an ambiguous value",
33513341 ) ;
3352- return ;
3342+ return None ;
33533343 }
33543344 ( Truthiness :: AlwaysTrue , Truthiness :: AlwaysTrue ) => {
3355- let Some ( builder) =
3356- context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression)
3357- else {
3358- return ;
3359- } ;
3345+ let builder =
3346+ context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression) ?;
33603347 builder. into_diagnostic (
33613348 "A legacy `typing.TypeVar` cannot be both covariant and contravariant" ,
33623349 ) ;
3363- return ;
3350+ return None ;
33643351 }
33653352 ( Truthiness :: AlwaysTrue , Truthiness :: AlwaysFalse ) => {
33663353 TypeVarVariance :: Contravariant
@@ -3374,11 +3361,8 @@ impl KnownClass {
33743361 let name_param = name_param. into_string_literal ( ) . map ( |name| name. value ( db) ) ;
33753362
33763363 if name_param. is_none_or ( |name_param| name_param != target. id ) {
3377- let Some ( builder) =
3378- context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression)
3379- else {
3380- return ;
3381- } ;
3364+ let builder =
3365+ context. report_lint ( & INVALID_LEGACY_TYPE_VARIABLE , call_expression) ?;
33823366 builder. into_diagnostic ( format_args ! (
33833367 "The name of a legacy `typing.TypeVar`{} must match \
33843368 the name of the variable it is assigned to (`{}`)",
@@ -3389,7 +3373,7 @@ impl KnownClass {
33893373 } ,
33903374 target. id,
33913375 ) ) ;
3392- return ;
3376+ return None ;
33933377 }
33943378
33953379 let bound_or_constraint = match ( bound, constraints) {
@@ -3414,13 +3398,13 @@ impl KnownClass {
34143398
34153399 // TODO: Emit a diagnostic that TypeVar cannot be both bounded and
34163400 // constrained
3417- ( Some ( _) , Some ( _) ) => return ,
3401+ ( Some ( _) , Some ( _) ) => return None ,
34183402
34193403 ( None , None ) => None ,
34203404 } ;
34213405
34223406 let containing_assignment = index. expect_single_definition ( target) ;
3423- overload_binding . set_return_type ( Type :: KnownInstance ( KnownInstanceType :: TypeVar (
3407+ Some ( Type :: KnownInstance ( KnownInstanceType :: TypeVar (
34243408 TypeVarInstance :: new (
34253409 db,
34263410 target. id . clone ( ) ,
@@ -3430,7 +3414,7 @@ impl KnownClass {
34303414 * default,
34313415 TypeVarKind :: Legacy ,
34323416 ) ,
3433- ) ) ) ;
3417+ ) ) )
34343418 }
34353419
34363420 KnownClass :: TypeAliasType => {
@@ -3446,30 +3430,31 @@ impl KnownClass {
34463430 } ) ;
34473431
34483432 let [ Some ( name) , Some ( value) , ..] = overload_binding. parameter_types ( ) else {
3449- return ;
3433+ return None ;
34503434 } ;
34513435
3452- if let Some ( name) = name . into_string_literal ( ) {
3453- overload_binding . set_return_type ( Type :: KnownInstance (
3454- KnownInstanceType :: TypeAliasType ( TypeAliasType :: Bare (
3436+ name. into_string_literal ( )
3437+ . map ( |name| {
3438+ Type :: KnownInstance ( KnownInstanceType :: TypeAliasType ( TypeAliasType :: Bare (
34553439 BareTypeAliasType :: new (
34563440 db,
34573441 ast:: name:: Name :: new ( name. value ( db) ) ,
34583442 containing_assignment,
34593443 value,
34603444 ) ,
3461- ) ) ,
3462- ) ) ;
3463- } else if let Some ( builder) =
3464- context. report_lint ( & INVALID_TYPE_ALIAS_TYPE , call_expression)
3465- {
3466- builder. into_diagnostic (
3467- "The name of a `typing.TypeAlias` must be a string literal" ,
3468- ) ;
3469- }
3445+ ) ) )
3446+ } )
3447+ . or_else ( || {
3448+ let builder =
3449+ context. report_lint ( & INVALID_TYPE_ALIAS_TYPE , call_expression) ?;
3450+ builder. into_diagnostic (
3451+ "The name of a `typing.TypeAlias` must be a string literal" ,
3452+ ) ;
3453+ None
3454+ } )
34703455 }
34713456
3472- _ => { }
3457+ _ => None ,
34733458 }
34743459 }
34753460}
0 commit comments