Skip to content

Commit

Permalink
Support type hints in prelude files. (pantsbuild#18442)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos authored Mar 8, 2023
1 parent 952b773 commit f5710a2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/python/pants/engine/internals/build_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os.path
import sys
import typing
from dataclasses import dataclass
from pathlib import PurePath
from typing import Any, Sequence, cast
Expand Down Expand Up @@ -93,6 +94,7 @@ async def evaluate_preludes(
)
globals: dict[str, Any] = {
**{name: getattr(builtins, name) for name in dir(builtins) if name.endswith("Error")},
**{name: getattr(typing, name) for name in dir(typing)},
# Ensure the globals for each prelude includes the builtin symbols (E.g. `python_sources`)
# and any build file aliases (e.g. from plugins)
**parser.symbols,
Expand Down
73 changes: 73 additions & 0 deletions src/python/pants/engine/internals/build_files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,79 @@ def make_a_target():
result.symbols["make_a_target"]()


def test_prelude_type_hint_code() -> None:
# Issue 18435
prelude_content = dedent(
"""\
def ecr_docker_image(
*,
name: Optional[str] = None,
dependencies: Optional[List[str]] = None,
image_tags: Optional[List[str]] = None,
git_tag_prefix: Optional[str] = None,
latest_tag_prefix: Optional[str] = None,
buildcache_tag: str = "buildcache",
image_labels: Optional[Mapping[str, str]] = None,
tags: Optional[List[str]] = None,
extra_build_args: Optional[List[str]] = None,
source: Optional[str] = None,
target_stage: Optional[str] = None,
instructions: Optional[List[str]] = None,
repository: Optional[str] = None,
context_root: Optional[str] = None,
push_in_pants_ci: bool = True,
push_latest: bool = False,
) -> int:
return 42
"""
)
result = run_prelude_parsing_rule(prelude_content)
ecr_docker_image = result.info["ecr_docker_image"]
assert ecr_docker_image.signature in (
(
"(*,"
" name: Optional[str] = None,"
" dependencies: Optional[List[str]] = None,"
" image_tags: Optional[List[str]] = None,"
" git_tag_prefix: Optional[str] = None,"
" latest_tag_prefix: Optional[str] = None,"
" buildcache_tag: str = 'buildcache',"
" image_labels: Optional[Mapping[str, str]] = None,"
" tags: Optional[List[str]] = None,"
" extra_build_args: Optional[List[str]] = None,"
" source: Optional[str] = None,"
" target_stage: Optional[str] = None,"
" instructions: Optional[List[str]] = None,"
" repository: Optional[str] = None,"
" context_root: Optional[str] = None,"
" push_in_pants_ci: bool = True,"
" push_latest: bool = False"
") -> int"
),
(
"(*,"
" name: Union[str, NoneType] = None,"
" dependencies: Union[List[str], NoneType] = None,"
" image_tags: Union[List[str], NoneType] = None,"
" git_tag_prefix: Union[str, NoneType] = None,"
" latest_tag_prefix: Union[str, NoneType] = None,"
" buildcache_tag: str = 'buildcache',"
" image_labels: Union[Mapping[str, str], NoneType] = None,"
" tags: Union[List[str], NoneType] = None,"
" extra_build_args: Union[List[str], NoneType] = None,"
" source: Union[str, NoneType] = None,"
" target_stage: Union[str, NoneType] = None,"
" instructions: Union[List[str], NoneType] = None,"
" repository: Union[str, NoneType] = None,"
" context_root: Union[str, NoneType] = None,"
" push_in_pants_ci: bool = True,"
" push_latest: bool = False"
") -> int"
),
)
assert 42 == ecr_docker_image.value()


def test_prelude_docstrings() -> None:
macro_docstring = "This is the doc-string for `macro_func`."
prelude_content = dedent(
Expand Down

0 comments on commit f5710a2

Please sign in to comment.