Skip to content

Commit

Permalink
feat(typing): Properly annotate dataset_name, suffix
Browse files Browse the repository at this point in the history
Makes more sense following (755ab4f)
  • Loading branch information
dangotbanned committed Feb 10, 2025
1 parent a776e2f commit ddda22c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 7 additions & 2 deletions altair/datasets/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
else:
from typing_extensions import TypedDict

if sys.version_info >= (3, 11):
from typing import LiteralString
else:
from typing_extensions import LiteralString

if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
Expand Down Expand Up @@ -181,8 +186,8 @@ class Metadata(TypedDict, total=False):
```
"""

dataset_name: str
suffix: str
dataset_name: Dataset | LiteralString
suffix: Extension
file_name: str
bytes: int
is_image: bool
Expand Down
1 change: 1 addition & 0 deletions tools/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def generate_typing(self, dpkg: datapackage.DataPackage) -> None:
"import sys",
"from typing import Literal, TYPE_CHECKING",
utils.import_typing_extensions((3, 14), "TypedDict"),
utils.import_typing_extensions((3, 11), "LiteralString"),
utils.import_typing_extensions((3, 10), "TypeAlias"),
"\n",
f"__all__ = {[NAME, EXT, dpkg._NAME_TYPED_DICT]}\n",
Expand Down
24 changes: 20 additions & 4 deletions tools/datasets/datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@


class Column:
def __init__(self, name: str, expr: pl.Expr, /, doc: str = "_description_") -> None:
def __init__(
self, name: str, expr: pl.Expr, /, doc: str = "_description_", tp_str: str = ""
) -> None:
self._name: str = name
self._expr: pl.Expr = expr
self._doc: str = doc
self._tp_str: str = tp_str

@property
def expr(self) -> pl.Expr:
Expand Down Expand Up @@ -161,7 +164,10 @@ def _metadata_examples(self) -> str:
@property
def _metadata_td_args(self) -> str:
schema = self.core.collect_schema().to_python()
return f"\n{INDENT}".join(f"{p}: {tp.__name__}" for p, tp in schema.items())
return f"\n{INDENT}".join(
f"{column._name}: {column._tp_str or tp.__name__}"
for column, tp in zip(self.columns, schema.values())
)

@property
def _url(self) -> Column:
Expand Down Expand Up @@ -237,8 +243,18 @@ def note(s: str, /) -> str:

fmt = col("format")
DataPackage.with_columns(
Column("dataset_name", path_stem("path"), "Name of the dataset/`Path.stem`_."),
Column("suffix", path_suffix("path"), "File extension/`Path.suffix`_."),
Column(
"dataset_name",
path_stem("path"),
"Name of the dataset/`Path.stem`_.",
tp_str="Dataset | LiteralString",
),
Column(
"suffix",
path_suffix("path"),
"File extension/`Path.suffix`_.",
tp_str="Extension",
),
Column("file_name", col("path"), "Equivalent to `Path.name`_."),
Column("bytes", col("bytes"), "File size in *bytes*."),
Column("is_image", fmt == "png", "Only accessible via url."),
Expand Down

0 comments on commit ddda22c

Please sign in to comment.