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): Support generating Union aliases #3656

Merged
merged 3 commits into from
Oct 23, 2024

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Oct 23, 2024

Initially covering one very specific case, PrimitiveValue.

Targeting that one specifically for use in #3653, where it will be used 3 times in a single annotation.

PrimitiveValue_T: TypeAlias = Union[str, bool, float, None]

Having this generated makes manually typing what will be the revision to 30b5a3c (#3653) a bit easier to read.

from collections.abc import Sequence, Mapping
from typing_extensions import TypeAlias, LiteralString

from altair.utils import Optional, SchemaBase, Undefined
from altair.vegalite.v5.schema._typing import PrimitiveValue_T, Temporal, SingleDefUnitChannel_T

# NOTE: These names are just for demo purposes
_AnyValue: TypeAlias = PrimitiveValue_T | Temporal | SchemaBase
_ChannelOrField: TypeAlias = SingleDefUnitChannel_T | LiteralString


def selection_interval(
    name: str | None = None,
    value: Optional[
        _AnyValue | Sequence[_AnyValue] | Mapping[_ChannelOrField, _AnyValue]
    ] = Undefined,
): ...

def selection_point(
    name: str | None = None,
    value: Optional[
        _AnyValue | Sequence[_AnyValue] | Mapping[SingleDefUnitChannel_T, _AnyValue]
    ] = Undefined,
): ...

Related

Initially covering one very specific case, `PrimitiveValue`.
tools\schemapi\utils.py:494: error: Incompatible types in assignment (expression has type "Generator[str, None, None]", variable has type "chain[str]")  [assignment]
@dangotbanned dangotbanned marked this pull request as ready for review October 23, 2024 19:06
@dangotbanned dangotbanned merged commit 5a6f70d into main Oct 23, 2024
23 checks passed
@dangotbanned dangotbanned deleted the typing-primitive-value branch October 23, 2024 19:47
dangotbanned added a commit that referenced this pull request Oct 23, 2024
dangotbanned added a commit that referenced this pull request Oct 25, 2024
…3653)

* feat: Support `datetime.(date|datetime)` as a `SchemaBase` parameter

#3651

* test: Adds `test_to_dict_datetime`

* feat: Reject non-UTC timezones

The [vega-lite docs](https://vega.github.io/vega-lite/docs/datetime.html) don't mention the `utc` property - but this restricts us to `utc` or `None`

* test(typing): Adds `test_to_dict_datetime_typing`

Annotation support will be complete once `mypy` starts complaining that these comments are unused

* fix(typing): Remove unused ignore

/~https://github.com/vega/altair/actions/runs/11460348337/job/31886851940?pr=3653

* feat(typing): Adds `Temporal` alias

* build: run `generate-schema-wrapper`

**Expecting to fail `mypy`**

#3653 (comment)

* fix(typing): Remove unused ignores

#3653 (comment), /~https://github.com/vega/altair/actions/runs/11461315861/job/31890019636?pr=3653

* fix: Use object identity for `utc` test instead of name

Adds two tests with "fake" utc timezones

* refactor(typing): Narrow `selection_(interval|point)(value=...)` types

- Tentative
- Copied from `core.SelectionParameter`

* refactor(typing): Utilize `PrimitiveValue_T`

#3656

* refactor(typing): Factor out `_LiteralValue` alias

This was incomplete as it didn't include `None`, but was serving the same purpose as the generated alias

* feat(typing): Accurately type `selection_(interval|point)(value=...)`

Added some docs to the new types, since what they describe is somewhat complex

References:
- https://vega.github.io/vega-lite/docs/selection.html#current-limitations
- https://vega.github.io/vega-lite/docs/param-value.html
- https://vega.github.io/vega-lite/docs/selection.html#project
- /~https://github.com/vega/altair/blob/5a6f70dc193f7ed9c89e6cc22e95c9d885167939/altair/vegalite/v5/schema/vega-lite-schema.json#L23087-L23118

Resolves:
- #3653 (comment)
- #3653 (comment)

* test(typing): Add (failing) `test_selection_interval_value_typing`

Need to fix this, but adding this first to demonstrate the issue.
Currently the way I've typed it prevents using different types for each encoding.

The goal is for each encoding to restrict types independently.
E.g. here `x` is **only** `date`, `y` is **only** `float`

* fix(typing): Correct failing `selection_interval` typing

#3653 (comment)

* test(typing): Improve coverage for `test_selection_interval_value_typing`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant