@@ -11,7 +11,6 @@ use crate::registry::AsRule;
11
11
#[ violation]
12
12
pub struct TypedArgumentDefaultInStub ;
13
13
14
- /// PYI011
15
14
impl AlwaysAutofixableViolation for TypedArgumentDefaultInStub {
16
15
#[ derive_message_formats]
17
16
fn message ( & self ) -> String {
@@ -26,7 +25,6 @@ impl AlwaysAutofixableViolation for TypedArgumentDefaultInStub {
26
25
#[ violation]
27
26
pub struct ArgumentDefaultInStub ;
28
27
29
- /// PYI014
30
28
impl AlwaysAutofixableViolation for ArgumentDefaultInStub {
31
29
#[ derive_message_formats]
32
30
fn message ( & self ) -> String {
@@ -41,7 +39,6 @@ impl AlwaysAutofixableViolation for ArgumentDefaultInStub {
41
39
#[ violation]
42
40
pub struct AssignmentDefaultInStub ;
43
41
44
- /// PYI015
45
42
impl AlwaysAutofixableViolation for AssignmentDefaultInStub {
46
43
#[ derive_message_formats]
47
44
fn message ( & self ) -> String {
@@ -225,7 +222,7 @@ fn is_valid_default_value_with_annotation(
225
222
/// Returns `true` if an [`Expr`] appears to be `TypeVar`, `TypeVarTuple`, `NewType`, or `ParamSpec`
226
223
/// call.
227
224
fn is_type_var_like_call ( context : & Context , expr : & Expr ) -> bool {
228
- let ExprKind :: Call { func, ..} = & expr. node else {
225
+ let ExprKind :: Call { func, .. } = & expr. node else {
229
226
return false ;
230
227
} ;
231
228
context. resolve_call_path ( func) . map_or ( false , |call_path| {
@@ -239,6 +236,20 @@ fn is_type_var_like_call(context: &Context, expr: &Expr) -> bool {
239
236
} )
240
237
}
241
238
239
+ /// Returns `true` if this is a "special" assignment which must have a value (e.g., an assignment to
240
+ /// `__all__`).
241
+ fn is_special_assignment ( context : & Context , target : & Expr ) -> bool {
242
+ if let ExprKind :: Name { id, .. } = & target. node {
243
+ match id. as_str ( ) {
244
+ "__all__" => context. scope ( ) . kind . is_module ( ) ,
245
+ "__match_args__" | "__slots__" => context. scope ( ) . kind . is_class ( ) ,
246
+ _ => false ,
247
+ }
248
+ } else {
249
+ false
250
+ }
251
+ }
252
+
242
253
/// PYI011
243
254
pub fn typed_argument_simple_defaults ( checker : & mut Checker , args : & Arguments ) {
244
255
if !args. defaults . is_empty ( ) {
@@ -354,26 +365,55 @@ pub fn argument_simple_defaults(checker: &mut Checker, args: &Arguments) {
354
365
}
355
366
356
367
/// PYI015
357
- pub fn assignment_default_in_stub ( checker : & mut Checker , value : & Expr , annotation : Option < & Expr > ) {
358
- if annotation. map_or ( false , |annotation| {
359
- checker. ctx . match_typing_expr ( annotation, "TypeAlias" )
360
- } ) {
368
+ pub fn assignment_default_in_stub ( checker : & mut Checker , targets : & [ Expr ] , value : & Expr ) {
369
+ if targets. len ( ) == 1 && is_special_assignment ( & checker. ctx , & targets[ 0 ] ) {
361
370
return ;
362
371
}
363
372
if is_type_var_like_call ( & checker. ctx , value) {
364
373
return ;
365
374
}
366
- if !is_valid_default_value_with_annotation ( value, checker, true ) {
367
- let mut diagnostic = Diagnostic :: new ( AssignmentDefaultInStub , Range :: from ( value) ) ;
368
-
369
- if checker. patch ( diagnostic. kind . rule ( ) ) {
370
- diagnostic. set_fix ( Edit :: replacement (
371
- "..." . to_string ( ) ,
372
- value. location ,
373
- value. end_location . unwrap ( ) ,
374
- ) ) ;
375
- }
375
+ if is_valid_default_value_with_annotation ( value, checker, true ) {
376
+ return ;
377
+ }
378
+
379
+ let mut diagnostic = Diagnostic :: new ( AssignmentDefaultInStub , Range :: from ( value) ) ;
380
+ if checker. patch ( diagnostic. kind . rule ( ) ) {
381
+ diagnostic. set_fix ( Edit :: replacement (
382
+ "..." . to_string ( ) ,
383
+ value. location ,
384
+ value. end_location . unwrap ( ) ,
385
+ ) ) ;
386
+ }
387
+ checker. diagnostics . push ( diagnostic) ;
388
+ }
389
+
390
+ /// PYI015
391
+ pub fn annotated_assignment_default_in_stub (
392
+ checker : & mut Checker ,
393
+ target : & Expr ,
394
+ value : & Expr ,
395
+ annotation : & Expr ,
396
+ ) {
397
+ if checker. ctx . match_typing_expr ( annotation, "TypeAlias" ) {
398
+ return ;
399
+ }
400
+ if is_special_assignment ( & checker. ctx , target) {
401
+ return ;
402
+ }
403
+ if is_type_var_like_call ( & checker. ctx , value) {
404
+ return ;
405
+ }
406
+ if is_valid_default_value_with_annotation ( value, checker, true ) {
407
+ return ;
408
+ }
376
409
377
- checker. diagnostics . push ( diagnostic) ;
410
+ let mut diagnostic = Diagnostic :: new ( AssignmentDefaultInStub , Range :: from ( value) ) ;
411
+ if checker. patch ( diagnostic. kind . rule ( ) ) {
412
+ diagnostic. set_fix ( Edit :: replacement (
413
+ "..." . to_string ( ) ,
414
+ value. location ,
415
+ value. end_location . unwrap ( ) ,
416
+ ) ) ;
378
417
}
418
+ checker. diagnostics . push ( diagnostic) ;
379
419
}
0 commit comments