@@ -116,14 +116,11 @@ impl<'db> ScopeId<'db> {
116
116
// Type parameter scopes behave like function scopes in terms of name resolution; CPython
117
117
// symbol table also uses the term "function-like" for these scopes.
118
118
matches ! (
119
- self . node( db) ,
120
- NodeWithScopeKind :: ClassTypeParameters ( _)
121
- | NodeWithScopeKind :: FunctionTypeParameters ( _)
122
- | NodeWithScopeKind :: Function ( _)
123
- | NodeWithScopeKind :: ListComprehension ( _)
124
- | NodeWithScopeKind :: SetComprehension ( _)
125
- | NodeWithScopeKind :: DictComprehension ( _)
126
- | NodeWithScopeKind :: GeneratorExpression ( _)
119
+ self . node( db) . scope_kind( ) ,
120
+ ScopeKind :: Annotation
121
+ | ScopeKind :: Function
122
+ | ScopeKind :: TypeAlias
123
+ | ScopeKind :: Comprehension
127
124
)
128
125
}
129
126
@@ -144,6 +141,12 @@ impl<'db> ScopeId<'db> {
144
141
}
145
142
NodeWithScopeKind :: Function ( function)
146
143
| NodeWithScopeKind :: FunctionTypeParameters ( function) => function. name . as_str ( ) ,
144
+ NodeWithScopeKind :: TypeAlias ( type_alias)
145
+ | NodeWithScopeKind :: TypeAliasTypeParameters ( type_alias) => type_alias
146
+ . name
147
+ . as_name_expr ( )
148
+ . map ( |name| name. id . as_str ( ) )
149
+ . unwrap_or ( "<type alias>" ) ,
147
150
NodeWithScopeKind :: Lambda ( _) => "<lambda>" ,
148
151
NodeWithScopeKind :: ListComprehension ( _) => "<listcomp>" ,
149
152
NodeWithScopeKind :: SetComprehension ( _) => "<setcomp>" ,
@@ -201,6 +204,7 @@ pub enum ScopeKind {
201
204
Class ,
202
205
Function ,
203
206
Comprehension ,
207
+ TypeAlias ,
204
208
}
205
209
206
210
impl ScopeKind {
@@ -326,6 +330,8 @@ pub(crate) enum NodeWithScopeRef<'a> {
326
330
Lambda ( & ' a ast:: ExprLambda ) ,
327
331
FunctionTypeParameters ( & ' a ast:: StmtFunctionDef ) ,
328
332
ClassTypeParameters ( & ' a ast:: StmtClassDef ) ,
333
+ TypeAlias ( & ' a ast:: StmtTypeAlias ) ,
334
+ TypeAliasTypeParameters ( & ' a ast:: StmtTypeAlias ) ,
329
335
ListComprehension ( & ' a ast:: ExprListComp ) ,
330
336
SetComprehension ( & ' a ast:: ExprSetComp ) ,
331
337
DictComprehension ( & ' a ast:: ExprDictComp ) ,
@@ -347,6 +353,12 @@ impl NodeWithScopeRef<'_> {
347
353
NodeWithScopeRef :: Function ( function) => {
348
354
NodeWithScopeKind :: Function ( AstNodeRef :: new ( module, function) )
349
355
}
356
+ NodeWithScopeRef :: TypeAlias ( type_alias) => {
357
+ NodeWithScopeKind :: TypeAlias ( AstNodeRef :: new ( module, type_alias) )
358
+ }
359
+ NodeWithScopeRef :: TypeAliasTypeParameters ( type_alias) => {
360
+ NodeWithScopeKind :: TypeAliasTypeParameters ( AstNodeRef :: new ( module, type_alias) )
361
+ }
350
362
NodeWithScopeRef :: Lambda ( lambda) => {
351
363
NodeWithScopeKind :: Lambda ( AstNodeRef :: new ( module, lambda) )
352
364
}
@@ -387,6 +399,12 @@ impl NodeWithScopeRef<'_> {
387
399
NodeWithScopeRef :: ClassTypeParameters ( class) => {
388
400
NodeWithScopeKey :: ClassTypeParameters ( NodeKey :: from_node ( class) )
389
401
}
402
+ NodeWithScopeRef :: TypeAlias ( type_alias) => {
403
+ NodeWithScopeKey :: TypeAlias ( NodeKey :: from_node ( type_alias) )
404
+ }
405
+ NodeWithScopeRef :: TypeAliasTypeParameters ( type_alias) => {
406
+ NodeWithScopeKey :: TypeAliasTypeParameters ( NodeKey :: from_node ( type_alias) )
407
+ }
390
408
NodeWithScopeRef :: ListComprehension ( comprehension) => {
391
409
NodeWithScopeKey :: ListComprehension ( NodeKey :: from_node ( comprehension) )
392
410
}
@@ -411,6 +429,8 @@ pub enum NodeWithScopeKind {
411
429
ClassTypeParameters ( AstNodeRef < ast:: StmtClassDef > ) ,
412
430
Function ( AstNodeRef < ast:: StmtFunctionDef > ) ,
413
431
FunctionTypeParameters ( AstNodeRef < ast:: StmtFunctionDef > ) ,
432
+ TypeAliasTypeParameters ( AstNodeRef < ast:: StmtTypeAlias > ) ,
433
+ TypeAlias ( AstNodeRef < ast:: StmtTypeAlias > ) ,
414
434
Lambda ( AstNodeRef < ast:: ExprLambda > ) ,
415
435
ListComprehension ( AstNodeRef < ast:: ExprListComp > ) ,
416
436
SetComprehension ( AstNodeRef < ast:: ExprSetComp > ) ,
@@ -423,9 +443,11 @@ impl NodeWithScopeKind {
423
443
match self {
424
444
Self :: Module => ScopeKind :: Module ,
425
445
Self :: Class ( _) => ScopeKind :: Class ,
426
- Self :: Function ( _) => ScopeKind :: Function ,
427
- Self :: Lambda ( _) => ScopeKind :: Function ,
428
- Self :: FunctionTypeParameters ( _) | Self :: ClassTypeParameters ( _) => ScopeKind :: Annotation ,
446
+ Self :: Function ( _) | Self :: Lambda ( _) => ScopeKind :: Function ,
447
+ Self :: FunctionTypeParameters ( _)
448
+ | Self :: ClassTypeParameters ( _)
449
+ | Self :: TypeAliasTypeParameters ( _) => ScopeKind :: Annotation ,
450
+ Self :: TypeAlias ( _) => ScopeKind :: TypeAlias ,
429
451
Self :: ListComprehension ( _)
430
452
| Self :: SetComprehension ( _)
431
453
| Self :: DictComprehension ( _)
@@ -446,6 +468,13 @@ impl NodeWithScopeKind {
446
468
_ => panic ! ( "expected function" ) ,
447
469
}
448
470
}
471
+
472
+ pub fn expect_type_alias ( & self ) -> & ast:: StmtTypeAlias {
473
+ match self {
474
+ Self :: TypeAlias ( type_alias) => type_alias. node ( ) ,
475
+ _ => panic ! ( "expected type alias" ) ,
476
+ }
477
+ }
449
478
}
450
479
451
480
#[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
@@ -455,6 +484,8 @@ pub(crate) enum NodeWithScopeKey {
455
484
ClassTypeParameters ( NodeKey ) ,
456
485
Function ( NodeKey ) ,
457
486
FunctionTypeParameters ( NodeKey ) ,
487
+ TypeAlias ( NodeKey ) ,
488
+ TypeAliasTypeParameters ( NodeKey ) ,
458
489
Lambda ( NodeKey ) ,
459
490
ListComprehension ( NodeKey ) ,
460
491
SetComprehension ( NodeKey ) ,
0 commit comments