From ff581397d11894490b432985d252b2342aa8117e Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 9 Jan 2024 15:52:49 -0800 Subject: [PATCH 1/3] Accept multiline quoted annotations See discussion in https://discuss.python.org/t/newlines-within-triple-quoted-type-expressions/42833 --- mypy/fastparse.py | 9 ++++++++- test-data/unit/check-basic.test | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index cba01eab2e4e..b7cee32b2e56 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -318,7 +318,14 @@ def parse_type_string( string expression "blah" using this function. """ try: - _, node = parse_type_comment(expr_string.strip(), line=line, column=column, errors=None) + try: + _, node = parse_type_comment( + expr_string.strip(), line=line, column=column, errors=None + ) + except SyntaxError: + _, node = parse_type_comment( + f"({expr_string.strip()})", line=line, column=column, errors=None + ) if isinstance(node, UnboundType) and node.original_str_expr is None: node.original_str_expr = expr_string node.original_str_fallback = expr_fallback_name diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 61a7160ce4f4..7a426c3eca9f 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -502,3 +502,19 @@ s2: str = 42 # E: Incompatible types in assignment (expression has type "int", s3: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [file c.py] s3: str = 'foo' + +[case testMultilineQuotedAnnotation] +x: """ + + int | + str + +""" +reveal_type(x) # N: Revealed type is "Union[builtins.int, builtins.str]" + +y: """( + int | + str +) +""" +reveal_type(y) # N: Revealed type is "Union[builtins.int, builtins.str]" From 52a7623754467bbbe935dd9b3a940ea92a6174d7 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 11 Jan 2024 18:42:59 -0800 Subject: [PATCH 2/3] . --- mypy/fastparse.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index b7cee32b2e56..49ddd5cebd98 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -318,14 +318,9 @@ def parse_type_string( string expression "blah" using this function. """ try: - try: - _, node = parse_type_comment( - expr_string.strip(), line=line, column=column, errors=None - ) - except SyntaxError: - _, node = parse_type_comment( - f"({expr_string.strip()})", line=line, column=column, errors=None - ) + _, node = parse_type_comment( + f"({expr_string})", line=line, column=column, errors=None + ) if isinstance(node, UnboundType) and node.original_str_expr is None: node.original_str_expr = expr_string node.original_str_fallback = expr_fallback_name From a1deff0770df856220bcc9e547c5cae17a142ed8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 12 Jan 2024 02:43:25 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/fastparse.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 49ddd5cebd98..5a88df2404ea 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -318,9 +318,7 @@ def parse_type_string( string expression "blah" using this function. """ try: - _, node = parse_type_comment( - f"({expr_string})", line=line, column=column, errors=None - ) + _, node = parse_type_comment(f"({expr_string})", line=line, column=column, errors=None) if isinstance(node, UnboundType) and node.original_str_expr is None: node.original_str_expr = expr_string node.original_str_fallback = expr_fallback_name