Skip to content

Commit 722735d

Browse files
Fix grammar for type alias support (#3949)
Fixes #3948
1 parent abe57e3 commit 722735d

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
<!-- Changes to the parser or to version autodetection -->
3939

40+
- Add support for PEP 695 type aliases containing lambdas and other unusual expressions
41+
(#3949)
42+
4043
### Performance
4144

4245
<!-- Changes that improve Black's performance. -->

src/blib2to3/Grammar.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ dotted_as_names: dotted_as_name (',' dotted_as_name)*
108108
dotted_name: NAME ('.' NAME)*
109109
global_stmt: ('global' | 'nonlocal') NAME (',' NAME)*
110110
assert_stmt: 'assert' test [',' test]
111-
type_stmt: "type" NAME [typeparams] '=' expr
111+
type_stmt: "type" NAME [typeparams] '=' test
112112

113113
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt | match_stmt
114114
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)

tests/data/cases/type_aliases.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# flags: --minimum-version=3.12
2+
23
type A=int
34
type Gen[T]=list[T]
5+
type Alias[T]=lambda: T
6+
type And[T]=T and T
7+
type IfElse[T]=T if T else T
48

59
type = aliased
610
print(type(42))
@@ -9,6 +13,9 @@
913

1014
type A = int
1115
type Gen[T] = list[T]
16+
type Alias[T] = lambda: T
17+
type And[T] = T and T
18+
type IfElse[T] = T if T else T
1219

1320
type = aliased
1421
print(type(42))

0 commit comments

Comments
 (0)