From 5ae38dd3706ad8906d39bb717bb2616dca1cc701 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 3 Mar 2025 00:20:23 -0800 Subject: [PATCH] Fix parser for TypeVar bounds (#4602) --- CHANGES.md | 3 +++ src/blib2to3/Grammar.txt | 6 +++--- tests/data/cases/type_param_defaults.py | 6 ++++++ tests/data/cases/type_params.py | 6 ++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d8348c9..a81e407 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,9 @@ +- Fix bug where certain unusual expressions (e.g., lambdas) were not accepted + in type parameter bounds and defaults. (#4602) + ### Performance diff --git a/src/blib2to3/Grammar.txt b/src/blib2to3/Grammar.txt index c8800e2..b779b49 100644 --- a/src/blib2to3/Grammar.txt +++ b/src/blib2to3/Grammar.txt @@ -12,9 +12,9 @@ file_input: (NEWLINE | stmt)* ENDMARKER single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE eval_input: testlist NEWLINE* ENDMARKER -typevar: NAME [':' expr] ['=' expr] -paramspec: '**' NAME ['=' expr] -typevartuple: '*' NAME ['=' (expr|star_expr)] +typevar: NAME [':' test] ['=' test] +paramspec: '**' NAME ['=' test] +typevartuple: '*' NAME ['=' (test|star_expr)] typeparam: typevar | paramspec | typevartuple typeparams: '[' typeparam (',' typeparam)* [','] ']' diff --git a/tests/data/cases/type_param_defaults.py b/tests/data/cases/type_param_defaults.py index feba64a..f8c24eb 100644 --- a/tests/data/cases/type_param_defaults.py +++ b/tests/data/cases/type_param_defaults.py @@ -20,6 +20,8 @@ def trailing_comma1[T=int,](a: str): def trailing_comma2[T=int](a: str,): pass +def weird_syntax[T=lambda: 42, **P=lambda: 43, *Ts=lambda: 44](): pass + # output type A[T = int] = float @@ -61,3 +63,7 @@ def trailing_comma2[T = int]( a: str, ): pass + + +def weird_syntax[T = lambda: 42, **P = lambda: 43, *Ts = lambda: 44](): + pass diff --git a/tests/data/cases/type_params.py b/tests/data/cases/type_params.py index 720a775..f8fc385 100644 --- a/tests/data/cases/type_params.py +++ b/tests/data/cases/type_params.py @@ -13,6 +13,8 @@ def it_gets_worse[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplit def magic[Trailing, Comma,](): pass +def weird_syntax[T: lambda: 42, U: a or b](): pass + # output @@ -56,3 +58,7 @@ def magic[ Comma, ](): pass + + +def weird_syntax[T: lambda: 42, U: a or b](): + pass