35
35
#define NAMED_EXPR_COMP_IN_CLASS \
36
36
"assignment expression within a comprehension cannot be used in a class body"
37
37
38
+ #define NAMED_EXPR_COMP_IN_TYPEVAR_BOUND \
39
+ "assignment expression within a comprehension cannot be used in a TypeVar bound"
40
+
41
+ #define NAMED_EXPR_COMP_IN_TYPEALIAS \
42
+ "assignment expression within a comprehension cannot be used in a type alias"
43
+
44
+ #define NAMED_EXPR_COMP_IN_TYPEPARAM \
45
+ "assignment expression within a comprehension cannot be used within the definition of a generic"
46
+
38
47
#define NAMED_EXPR_COMP_CONFLICT \
39
48
"assignment expression cannot rebind comprehension iteration variable '%U'"
40
49
@@ -1857,7 +1866,7 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1857
1866
}
1858
1867
1859
1868
/* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
1860
- if (_PyST_IsFunctionLike ( ste ) ) {
1869
+ if (ste -> ste_type == FunctionBlock ) {
1861
1870
long target_in_scope = _PyST_GetSymbol (ste , target_name );
1862
1871
if (target_in_scope & DEF_GLOBAL ) {
1863
1872
if (!symtable_add_def (st , target_name , DEF_GLOBAL , LOCATION (e )))
@@ -1880,9 +1889,27 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1880
1889
1881
1890
return symtable_add_def_helper (st , target_name , DEF_GLOBAL , ste , LOCATION (e ));
1882
1891
}
1883
- /* Disallow usage in ClassBlock */
1884
- if (ste -> ste_type == ClassBlock ) {
1885
- PyErr_Format (PyExc_SyntaxError , NAMED_EXPR_COMP_IN_CLASS );
1892
+ /* Disallow usage in ClassBlock and type scopes */
1893
+ if (ste -> ste_type == ClassBlock ||
1894
+ ste -> ste_type == TypeParamBlock ||
1895
+ ste -> ste_type == TypeAliasBlock ||
1896
+ ste -> ste_type == TypeVarBoundBlock ) {
1897
+ switch (ste -> ste_type ) {
1898
+ case ClassBlock :
1899
+ PyErr_Format (PyExc_SyntaxError , NAMED_EXPR_COMP_IN_CLASS );
1900
+ break ;
1901
+ case TypeParamBlock :
1902
+ PyErr_Format (PyExc_SyntaxError , NAMED_EXPR_COMP_IN_TYPEPARAM );
1903
+ break ;
1904
+ case TypeAliasBlock :
1905
+ PyErr_Format (PyExc_SyntaxError , NAMED_EXPR_COMP_IN_TYPEALIAS );
1906
+ break ;
1907
+ case TypeVarBoundBlock :
1908
+ PyErr_Format (PyExc_SyntaxError , NAMED_EXPR_COMP_IN_TYPEVAR_BOUND );
1909
+ break ;
1910
+ default :
1911
+ Py_UNREACHABLE ();
1912
+ }
1886
1913
PyErr_RangedSyntaxLocationObject (st -> st_filename ,
1887
1914
e -> lineno ,
1888
1915
e -> col_offset + 1 ,
0 commit comments