Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0da945d

Browse files
committed
Improve call expressions in type annotations with native parser
In particular, `Annotated[foo, some_call(x=y)]` shouldn't generate an error. This requires recent changes to ast_serialize.
1 parent 3b2b1dd commit 0da945d

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

mypy/nativeparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def parse_to_binary_ast(
271271
platform=options.platform,
272272
always_true=options.always_true,
273273
always_false=options.always_false,
274-
cache_version=1,
274+
cache_version=2,
275275
)
276276
return (
277277
ast_bytes,
@@ -939,6 +939,7 @@ def read_type(state: State, data: ReadBuffer) -> Type:
939939
elif tag == types.RAW_EXPRESSION_TYPE:
940940
type_name = read_str(data)
941941
value: types.LiteralValue | str | None
942+
note: str | None = None
942943
if type_name == "builtins.bool":
943944
value = read_bool(data)
944945
elif type_name == "builtins.int":
@@ -953,9 +954,11 @@ def read_type(state: State, data: ReadBuffer) -> Type:
953954
tag = read_tag(data)
954955
assert tag == LITERAL_NONE, f"Expected LITERAL_NONE for invalid type, got {tag}"
955956
value = None
957+
# Read optional note (cache_version >= 2)
958+
note = read_str_opt(data)
956959
else:
957960
assert False, f"Unsupported RawExpressionType: {type_name}"
958-
raw_type = RawExpressionType(value, type_name)
961+
raw_type = RawExpressionType(value, type_name, note=note)
959962
read_loc(data, raw_type)
960963
expect_end_tag(data)
961964
return raw_type

test-data/unit/check-annotated.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ x: Annotated[int, THESE, ARE, IGNORED, FOR, NOW]
1717
reveal_type(x) # N: Revealed type is "builtins.int"
1818
[builtins fixtures/tuple.pyi]
1919

20-
[case testAnnotated3_no_native_parse]
20+
[case testAnnotated3]
2121
from typing_extensions import Annotated
2222
x: Annotated[int, -+~12.3, "som"[e], more(anno+a+ions, that=[are]), (b"ignored",), 4, N.O.W, ...]
2323
reveal_type(x) # N: Revealed type is "builtins.int"

test-data/unit/check-fastparse.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def f(x, y): # E: Type signature has too few parameters
194194
f(1, 2)
195195
f(1) # E: Missing positional argument "y" in call to "f"
196196

197-
[case testFasterParseTypeErrorCustom_no_native_parse]
197+
[case testFasterParseTypeErrorCustom]
198198

199199
from typing import TypeVar, Generic
200200
T = TypeVar('T')
@@ -207,7 +207,7 @@ def f(a: Foo(int)) -> int:
207207
main:7: error: Invalid type comment or annotation
208208
main:7: note: Suggestion: use Foo[...] instead of Foo(...)
209209

210-
[case testFasterParseCallWithKeywordArgs_no_native_parse]
210+
[case testFasterParseCallWithKeywordArgs]
211211

212212
def Foo(sort: bool) -> type:
213213
return int

0 commit comments

Comments
 (0)