Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typing): Adds public altair.typing module #3515

Merged
merged 36 commits into from
Aug 8, 2024
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
680b69e
feat(typing): Create `altair.typing`
dangotbanned Aug 3, 2024
785f89c
chore: Add comment on `tooltip` annotation
dangotbanned Aug 3, 2024
5423748
feat(typing): Add reference impl `EncodeKwds` from comment
dangotbanned Aug 3, 2024
da64738
feat(typing): Use `OneOrSeq[tps]` instead of `Union[*tps, list]` in `…
dangotbanned Aug 3, 2024
5877b38
build: run `generate-schema-wrapper`
dangotbanned Aug 3, 2024
4fe5f3a
wip: generate `typed_dict_args`
dangotbanned Aug 3, 2024
0014cc8
refactor: Simplify `tools`, remove redundant code
dangotbanned Aug 3, 2024
226038d
refactor: finish removing `altair_classes_prefix`
dangotbanned Aug 3, 2024
77b101a
feat: `_create_encode_signature()` -> `EncodingArtifacts`
dangotbanned Aug 3, 2024
675bc4e
build: run `generate-schema-wrapper`
dangotbanned Aug 3, 2024
51a84a5
feat(typing): Provide a public export for `_EncodeKwds`
dangotbanned Aug 3, 2024
2ef4b0f
Merge branch 'main' into public-typing
dangotbanned Aug 3, 2024
4e0a098
Merge branch 'main' into pr/dangotbanned/3515
binste Aug 4, 2024
2b9ad2c
Add docstring to _EncodeKwds
binste Aug 4, 2024
79f317d
Rewrite EncodeArtifacts dataclass as a function
binste Aug 4, 2024
1eb466d
Fix ruff issue due to old local ruff version
binste Aug 4, 2024
0287eba
Change generate_encoding_artifacts to an iterator
binste Aug 4, 2024
bac1f67
docs: run `generate-schema-wrapper` with `indent_level=4`
dangotbanned Aug 4, 2024
3419250
feat(typing): Move `ChartType`, `is_chart_type` to `alt.typing`
dangotbanned Aug 4, 2024
5321b4b
Merge remote-tracking branch 'upstream/main' into public-typing
dangotbanned Aug 4, 2024
d16ec34
revert(ruff): Restore original ('RUF001`) line
dangotbanned Aug 4, 2024
e903528
Add type aliases for each channel
binste Aug 5, 2024
6662fc9
Format
binste Aug 5, 2024
28de27b
Use Union instead of | for compatibility with Py <3.10
binste Aug 5, 2024
b3fbe9c
Add channel type aliases to typing module. Add 'Type hints' section t…
binste Aug 6, 2024
5ba8a8d
chore(ruff): Remove unused `F401` ignore
dangotbanned Aug 6, 2024
49122b1
feat(typing): Move `Optional` export to `typing`
dangotbanned Aug 6, 2024
fe22c80
refactor: Move blank line append to `indent_docstring`
dangotbanned Aug 6, 2024
d3daf51
docs(typing): Remove empty type list from `EncodeKwds`
dangotbanned Aug 6, 2024
914428a
refactor: Renaming, grouping, reducing repetition
dangotbanned Aug 6, 2024
11c58c3
refactor: More tidying up, annotating, reformat
dangotbanned Aug 6, 2024
067f455
docs: Reference aliases in `generate_encoding_artifacts`
dangotbanned Aug 6, 2024
6fefd12
Use full type hints instead of type alias in signatures for typeddict…
binste Aug 7, 2024
9299a81
Merge remote-tracking branch 'upstream/main' into public-typing
dangotbanned Aug 7, 2024
b6f84e4
Rename 'Type hints' to 'Typing'
binste Aug 8, 2024
d4313c0
Ruff fix
binste Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 66 additions & 46 deletions tools/generate_schema_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def configure_{prop}(self, *args, **kwargs) -> Self:

ENCODE_METHOD: Final = '''
class _EncodingMixin:
def encode({encode_method_args}) -> Self:
def encode({method_args}) -> Self:
"""Map properties of the data to visual properties of the chart (see :class:`FacetedEncoding`)
{docstring}"""
# Compat prep for `infer_encoding_types` signature
Expand All @@ -233,6 +233,13 @@ def encode({encode_method_args}) -> Self:
return copy
'''

ENCODE_TYPED_DICT: Final = '''
class _EncodeKwds(TypedDict, total=False):
"""{docstring}"""
{channels}

'''

# NOTE: Not yet reasonable to generalize `TypeAliasType`, `TypeVar`
# Revisit if this starts to become more common
TYPING_EXTRA: Final = '''
Expand Down Expand Up @@ -654,7 +661,7 @@ def generate_vegalite_channel_wrappers(

imports = imports or [
"from __future__ import annotations\n",
"from typing import Any, overload, Sequence, List, Literal, Union, TYPE_CHECKING",
"from typing import Any, overload, Sequence, List, Literal, Union, TYPE_CHECKING, TypedDict",
"from narwhals.dependencies import is_pandas_dataframe as _is_pandas_dataframe",
"from altair.utils.schemapi import Undefined, with_property_setters",
"from altair.utils import infer_encoding_types as _infer_encoding_types",
Expand All @@ -674,11 +681,8 @@ def generate_vegalite_channel_wrappers(
"\n" f"__all__ = {sorted(all_)}\n",
CHANNEL_MIXINS,
*class_defs,
*EncodingArtifacts(channel_infos, ENCODE_METHOD, ENCODE_TYPED_DICT),
]

# Generate the type signature for the encode method
encode_signature = _create_encode_signature(channel_infos)
contents.append(encode_signature)
return "\n".join(contents)


Expand Down Expand Up @@ -859,48 +863,64 @@ def vegalite_main(skip_download: bool = False) -> None:
ruff_write_lint_format_str(fp, contents)


def _create_encode_signature(
channel_infos: dict[str, ChannelInfo],
) -> str:
signature_args: list[str] = ["self", "*args: Any"]
docstring_parameters: list[str] = ["", "Parameters", "----------"]

# TODO: refactor so extracting the `TypedDict` here makes sense
# - Want to avoid simply returning tuple[str, str]
typed_dict_args: list[str] = []

for channel, info in channel_infos.items():
# `Map` stands for the return types of alt.datum, alt.value as well as
# the dictionary representation of an encoding channel class. See
# discussions in https://github.com/vega/altair/pull/3208
# for more background.
union_types = ["str", info.field_class_name, "Map"]
it_rst_names = (rst_syntax_for_class(c) for c in info.all_names)
docstring_union_types = ["str", next(it_rst_names), "Dict"]
tp_inner: str = " | ".join(chain(union_types, info.non_field_names))
if info.supports_arrays:
# We could be more specific about what types are accepted in the list
# but then the signatures would get rather long and less useful
# to a user when it shows up in their IDE.
docstring_union_types.append("List")
tp_inner = f"OneOrSeq[{tp_inner}]"

signature_args.append(f"{channel}: Optional[{tp_inner}] = Undefined")
typed_dict_args.append(f"{channel}: {tp_inner}")
docstring_parameters.extend(
(
f"{channel} : {', '.join(chain(docstring_union_types, it_rst_names))}",
f" {process_description(info.deep_description)}",
@dataclass
class EncodingArtifacts:
"""
Wrapper for what was previously `_create_encode_signature()`.

Now also creates a `TypedDict` as part of https://github.com/pola-rs/polars/pull/17995
"""

channel_infos: dict[str, ChannelInfo]
fmt_method: str
fmt_typed_dict: str

def __iter__(self) -> Iterator[str]:
dangotbanned marked this conversation as resolved.
Show resolved Hide resolved
"""After construction, this allows for unpacking (`*`)."""
yield from self._gen_artifacts()

def _gen_artifacts(self) -> None:
"""
Generate `.encode()` related things.

Notes
-----
- `Map`/`Dict` stands for the return types of `alt.(datum|value)`, and any encoding channel class.
- See discussions in https://github.com/vega/altair/pull/3208
- We could be more specific about what types are accepted in the `List`
- but this translates poorly to an IDE
- `info.supports_arrays`
"""
signature_args: list[str] = ["self", "*args: Any"]
docstring_parameters: list[str] = ["", "Parameters", "----------"]
typed_dict_args: list[str] = []
for channel, info in self.channel_infos.items():
it = info.all_names
it_rst_names = (rst_syntax_for_class(c) for c in info.all_names)

docstring_union_types = ["str", next(it_rst_names), "Dict"]
tp_inner: str = " | ".join(chain(("str", next(it), "Map"), it))
if info.supports_arrays:
docstring_union_types.append("List")
tp_inner = f"OneOrSeq[{tp_inner}]"
signature_args.append(f"{channel}: Optional[{tp_inner}] = Undefined")
typed_dict_args.append(f"{channel}: {tp_inner}")
docstring_parameters.extend(
(
f"{channel} : {', '.join(chain(docstring_union_types, it_rst_names))}",
f" {process_description(info.deep_description)}",
)
)
)
if len(docstring_parameters) > 1:
docstring_parameters += [""]
docstring = indent_docstring(
docstring_parameters, indent_level=8, width=100, lstrip=False
)
return ENCODE_METHOD.format(
encode_method_args=", ".join(signature_args), docstring=docstring
)
doc = indent_docstring(
docstring_parameters, indent_level=8, width=100, lstrip=False
)
yield self.fmt_method.format(
method_args=", ".join(signature_args), docstring=doc
)
yield self.fmt_typed_dict.format(
channels="\n ".join(typed_dict_args), docstring="Placeholder (FIXME)"
)


def main() -> None:
Expand Down