Skip to content

Commit

Permalink
Accept multiline quoted annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Jan 9, 2024
1 parent 761965d commit ff58139
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]"

0 comments on commit ff58139

Please sign in to comment.