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

docs: Fix and improve alt.Optional doc #3449

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 24 additions & 6 deletions altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,15 +746,33 @@ def __repr__(self) -> str:
Undefined = UndefinedType()
T = TypeVar("T")
Optional: TypeAlias = Union[T, UndefinedType]
"""One of the sepcified types, or the `Undefined` singleton.

"""One of ``T`` specified type(s), or the ``Undefined`` singleton.

Examples
--------
```py
MaybeDictOrStr: TypeAlias = Optional[dict[str, Any] | str]
LongerDictOrStr: TypeAlias = dict[str, Any] | str | UndefinedType
```
The parameters ``short``, ``long`` accept the same range of types::

# ruff: noqa: UP006, UP007
from altair import Optional

def func_1(
short: Optional[str | bool | float | dict[str, Any] | SchemaBase] = Undefined,
long: Union[
str, bool, float, Dict[str, Any], SchemaBase, UndefinedType
] = Undefined,
): ...

This is distinct from `typing.Optional <https://typing.readthedocs.io/en/latest/spec/historical.html#union-and-optional>`__ as ``altair.Optional`` treats ``None`` like any other type::

# ruff: noqa: UP006, UP007
from altair import Optional

def func_2(
short: Optional[str | float | dict[str, Any] | None | SchemaBase] = Undefined,
long: Union[
str, float, Dict[str, Any], None, SchemaBase, UndefinedType
] = Undefined,
): ...
"""


Expand Down
30 changes: 24 additions & 6 deletions tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,33 @@ def __repr__(self) -> str:
Undefined = UndefinedType()
T = TypeVar("T")
Optional: TypeAlias = Union[T, UndefinedType]
"""One of the sepcified types, or the `Undefined` singleton.

"""One of ``T`` specified type(s), or the ``Undefined`` singleton.

Examples
--------
```py
MaybeDictOrStr: TypeAlias = Optional[dict[str, Any] | str]
LongerDictOrStr: TypeAlias = dict[str, Any] | str | UndefinedType
```
The parameters ``short``, ``long`` accept the same range of types::

# ruff: noqa: UP006, UP007
from altair import Optional

def func_1(
short: Optional[str | bool | float | dict[str, Any] | SchemaBase] = Undefined,
long: Union[
str, bool, float, Dict[str, Any], SchemaBase, UndefinedType
] = Undefined,
): ...

This is distinct from `typing.Optional <https://typing.readthedocs.io/en/latest/spec/historical.html#union-and-optional>`__ as ``altair.Optional`` treats ``None`` like any other type::

# ruff: noqa: UP006, UP007
from altair import Optional

def func_2(
short: Optional[str | float | dict[str, Any] | None | SchemaBase] = Undefined,
long: Union[
str, float, Dict[str, Any], None, SchemaBase, UndefinedType
] = Undefined,
): ...
"""


Expand Down