mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Use the existing f-string body unparser for template interpolation format specs, while preserving the outer interpolation source text. Add compile-only Python annotation-constant coverage. Assisted-by: OpenAI Codex:model identifier unavailable
23 lines
688 B
Python
23 lines
688 B
Python
from typing import get_type_hints
|
|
|
|
def func(s: str) -> int:
|
|
return int(s)
|
|
|
|
hints = get_type_hints(func)
|
|
|
|
# The order of type hints matters for certain functions
|
|
# e.g. functools.singledispatch
|
|
assert list(hints.items()) == [('s', str), ('return', int)]
|
|
|
|
|
|
# Template format specs use canonical expression text inside future annotations.
|
|
for template, expected in (
|
|
("t'{value:04}'", "t'{value:04}'"),
|
|
("t'{value:{(width)}}'", "t'{value:{width}}'"),
|
|
("t'{(value):{(width)!r}}'", "t'{(value):{width!r}}'"),
|
|
):
|
|
annotation_code = compile(
|
|
"from __future__ import annotations\nx: " + template, "<test>", "exec"
|
|
)
|
|
assert expected in annotation_code.co_consts
|