@@ -120,26 +120,27 @@ impl SemanticSyntaxChecker {
120
120
fn check_annotation < Ctx : SemanticSyntaxContext > ( stmt : & ast:: Stmt , ctx : & Ctx ) {
121
121
match stmt {
122
122
Stmt :: FunctionDef ( ast:: StmtFunctionDef {
123
- type_params,
123
+ type_params : Some ( type_params ) ,
124
124
parameters,
125
125
returns,
126
126
..
127
127
} ) => {
128
128
// test_ok valid_annotation_function
129
129
// def f() -> (y := 3): ...
130
130
// def g(arg: (x := 1)): ...
131
+ // def outer():
132
+ // def i(x: (yield 1)): ...
133
+ // def k() -> (yield 1): ...
134
+ // def m(x: (yield from 1)): ...
135
+ // def o() -> (yield from 1): ...
131
136
132
137
// test_err invalid_annotation_function
133
138
// def f[T]() -> (y := 3): ...
134
139
// def g[T](arg: (x := 1)): ...
135
140
// def h[T](x: (yield 1)): ...
136
- // def i(x: (yield 1)): ...
137
141
// def j[T]() -> (yield 1): ...
138
- // def k() -> (yield 1): ...
139
142
// def l[T](x: (yield from 1)): ...
140
- // def m(x: (yield from 1)): ...
141
143
// def n[T]() -> (yield from 1): ...
142
- // def o() -> (yield from 1): ...
143
144
// def p[T: (yield 1)](): ... # yield in TypeVar bound
144
145
// def q[T = (yield 1)](): ... # yield in TypeVar default
145
146
// def r[*Ts = (yield 1)](): ... # yield in TypeVarTuple default
@@ -148,20 +149,11 @@ impl SemanticSyntaxChecker {
148
149
// def u[T = (x := 1)](): ... # named expr in TypeVar default
149
150
// def v[*Ts = (x := 1)](): ... # named expr in TypeVarTuple default
150
151
// def w[**Ts = (x := 1)](): ... # named expr in ParamSpec default
151
- let is_generic = type_params. is_some ( ) ;
152
152
let mut visitor = InvalidExpressionVisitor {
153
- allow_named_expr : !is_generic,
154
- position : InvalidExpressionPosition :: TypeAnnotation ,
153
+ position : InvalidExpressionPosition :: GenericDefinition ,
155
154
ctx,
156
155
} ;
157
- if let Some ( type_params) = type_params {
158
- visitor. visit_type_params ( type_params) ;
159
- }
160
- if is_generic {
161
- visitor. position = InvalidExpressionPosition :: GenericDefinition ;
162
- } else {
163
- visitor. position = InvalidExpressionPosition :: TypeAnnotation ;
164
- }
156
+ visitor. visit_type_params ( type_params) ;
165
157
for param in parameters
166
158
. iter ( )
167
159
. filter_map ( ast:: AnyParameterRef :: annotation)
@@ -173,36 +165,29 @@ impl SemanticSyntaxChecker {
173
165
}
174
166
}
175
167
Stmt :: ClassDef ( ast:: StmtClassDef {
176
- type_params,
168
+ type_params : Some ( type_params ) ,
177
169
arguments,
178
170
..
179
171
} ) => {
180
172
// test_ok valid_annotation_class
181
173
// class F(y := list): ...
174
+ // def f():
175
+ // class G((yield 1)): ...
176
+ // class H((yield from 1)): ...
182
177
183
178
// test_err invalid_annotation_class
184
179
// class F[T](y := list): ...
185
- // class G((yield 1)): ...
186
- // class H((yield from 1)): ...
187
180
// class I[T]((yield 1)): ...
188
181
// class J[T]((yield from 1)): ...
189
182
// class K[T: (yield 1)]: ... # yield in TypeVar
190
183
// class L[T: (x := 1)]: ... # named expr in TypeVar
191
- let is_generic = type_params. is_some ( ) ;
192
184
let mut visitor = InvalidExpressionVisitor {
193
- allow_named_expr : !is_generic,
194
- position : InvalidExpressionPosition :: TypeAnnotation ,
185
+ position : InvalidExpressionPosition :: TypeVarBound ,
195
186
ctx,
196
187
} ;
197
- if let Some ( type_params) = type_params {
198
- visitor. visit_type_params ( type_params) ;
199
- }
200
- if is_generic {
201
- visitor. position = InvalidExpressionPosition :: GenericDefinition ;
202
- } else {
203
- visitor. position = InvalidExpressionPosition :: BaseClass ;
204
- }
188
+ visitor. visit_type_params ( type_params) ;
205
189
if let Some ( arguments) = arguments {
190
+ visitor. position = InvalidExpressionPosition :: GenericDefinition ;
206
191
visitor. visit_arguments ( arguments) ;
207
192
}
208
193
}
@@ -217,7 +202,6 @@ impl SemanticSyntaxChecker {
217
202
// type Y = (yield 1) # yield in value
218
203
// type Y = (x := 1) # named expr in value
219
204
let mut visitor = InvalidExpressionVisitor {
220
- allow_named_expr : false ,
221
205
position : InvalidExpressionPosition :: TypeAlias ,
222
206
ctx,
223
207
} ;
@@ -625,12 +609,6 @@ impl Display for SemanticSyntaxError {
625
609
write ! ( f, "cannot delete `__debug__` on Python {python_version} (syntax was removed in 3.9)" )
626
610
}
627
611
} ,
628
- SemanticSyntaxErrorKind :: InvalidExpression (
629
- kind,
630
- InvalidExpressionPosition :: BaseClass ,
631
- ) => {
632
- write ! ( f, "{kind} cannot be used as a base class" )
633
- }
634
612
SemanticSyntaxErrorKind :: InvalidExpression ( kind, position) => {
635
613
write ! ( f, "{kind} cannot be used within a {position}" )
636
614
}
@@ -857,8 +835,6 @@ pub enum InvalidExpressionPosition {
857
835
TypeVarDefault ,
858
836
TypeVarTupleDefault ,
859
837
ParamSpecDefault ,
860
- TypeAnnotation ,
861
- BaseClass ,
862
838
GenericDefinition ,
863
839
TypeAlias ,
864
840
}
@@ -870,9 +846,7 @@ impl Display for InvalidExpressionPosition {
870
846
InvalidExpressionPosition :: TypeVarDefault => "TypeVar default" ,
871
847
InvalidExpressionPosition :: TypeVarTupleDefault => "TypeVarTuple default" ,
872
848
InvalidExpressionPosition :: ParamSpecDefault => "ParamSpec default" ,
873
- InvalidExpressionPosition :: TypeAnnotation => "type annotation" ,
874
849
InvalidExpressionPosition :: GenericDefinition => "generic definition" ,
875
- InvalidExpressionPosition :: BaseClass => "base class" ,
876
850
InvalidExpressionPosition :: TypeAlias => "type alias" ,
877
851
} )
878
852
}
@@ -1086,16 +1060,6 @@ impl<'a, Ctx: SemanticSyntaxContext> MatchPatternVisitor<'a, Ctx> {
1086
1060
}
1087
1061
1088
1062
struct InvalidExpressionVisitor < ' a , Ctx > {
1089
- /// Allow named expressions (`x := ...`) to appear in annotations.
1090
- ///
1091
- /// These are allowed in non-generic functions, for example:
1092
- ///
1093
- /// ```python
1094
- /// def foo(arg: (x := int)): ... # ok
1095
- /// def foo[T](arg: (x := int)): ... # syntax error
1096
- /// ```
1097
- allow_named_expr : bool ,
1098
-
1099
1063
/// Context used for emitting errors.
1100
1064
ctx : & ' a Ctx ,
1101
1065
@@ -1108,7 +1072,7 @@ where
1108
1072
{
1109
1073
fn visit_expr ( & mut self , expr : & Expr ) {
1110
1074
match expr {
1111
- Expr :: Named ( ast:: ExprNamed { range, .. } ) if ! self . allow_named_expr => {
1075
+ Expr :: Named ( ast:: ExprNamed { range, .. } ) => {
1112
1076
SemanticSyntaxChecker :: add_error (
1113
1077
self . ctx ,
1114
1078
SemanticSyntaxErrorKind :: InvalidExpression (
0 commit comments