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: Adds ChartType type and type guard is_chart_type. Change PurePath to Path type hints #3420

Merged
merged 17 commits into from
May 23, 2024

Commits on May 12, 2024

  1. feat: Type hints: Adds ChartType and guard is_chart_type

    I'm working on a library that uses `altair`, and looking to make it an optional dependency. During this process, I thought `ChartType` and `is_chart_type` might be a good fit in `altair` itself.
    
    I don't believe any existing `altair` code would directly benefit from these additions - but you could likely reduce some `isinstance` checks with [type narrowing](https://typing.readthedocs.io/en/latest/spec/narrowing.html) functions like `is_chart_type`.
    For example `altair/vegalite/v5/api.py` has **63** occurrences of `isinstance`.
    
    ---
    
    per [CONTRIBUTING.md](https://github.com/vega/altair/blob/main/CONTRIBUTING.md)
    > In particular, we welcome companion efforts from other visualization libraries to render the Vega-Lite specifications output by Altair. We see this portion of the effort as much bigger than Altair itself
    
    Towards this aim, it could be beneficial to provide `typing` constructs/tools for downstream use.
    
    Regarding my specfic use case, the below [Protocol](https://typing.readthedocs.io/en/latest/spec/protocol.html#runtime-checkable-decorator-and-narrowing-types-by-isinstance) - I believe - describes the concept of a `_/Chart`, for the purposes of writing to file:
    
    ```py
    from typing import Any, Protocol, runtime_checkable
    
    @runtime_checkable
    class AltairChart(Protocol):
        data: Any
    
        def copy(self, *args: Any, **kwargs: Any) -> Any: ...
        def save(self, *args: Any, **kwargs: Any) -> None: ...
        def to_dict(self, *args: Any, **kwargs: Any) -> dict[Any, Any]: ...
        def to_html(self, *args: Any, **kwargs: Any) -> str: ...
    ```
    
    Due to the number of symbols in `altair` and heavy use of mixins, coming to this conclusion can be a stumbling block.
    
    However, exporting things like `ChartType`, `is_chart_type`, `AltairChart` could be a way to expose these shared behaviours - without requiring any changes to the core API.
    dangotbanned committed May 12, 2024
    Configuration menu
    Copy the full SHA
    6964d0d View commit details
    Browse the repository at this point in the history
  2. fix: Type hints: Add missing pathlib.Path in save method annotation

    `TopLevelMixin.save` calls a function accepting `pathlib` objects, but the two didn't share the same `Union` for `fp`.
    When using `pyright`, the following warning is presented:
    
    > Argument of type "Path" cannot be assigned to parameter "fp" of type "str | IO[Unknown]" in function "save"
    > Type "Path" is incompatible with type "str | IO[Unknown]"
    > "Path" is incompatible with "str"
    > "Path" is incompatible with "IO[Unknown]"
    
    This fix avoids the need to use type ignores on the public facing API, as a user.
    dangotbanned committed May 12, 2024
    Configuration menu
    Copy the full SHA
    96bf092 View commit details
    Browse the repository at this point in the history
  3. refactor: Simplify save.py path handling

    By normalizing `fp` in `save`, it simplifies the signatures of two other functions and removes the need for `str` specific suffix handling.
    dangotbanned committed May 12, 2024
    Configuration menu
    Copy the full SHA
    9c770a5 View commit details
    Browse the repository at this point in the history
  4. refactor: Replace duplicated save get encoding with a single expres…

    …sion
    
    Although there are branches it isn't needed, I think this is easier to read and maintain.
    
    Very low priority change, just something I noticed while reading.
    dangotbanned committed May 12, 2024
    Configuration menu
    Copy the full SHA
    99b4f81 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    55cddd5 View commit details
    Browse the repository at this point in the history

Commits on May 13, 2024

  1. Configuration menu
    Copy the full SHA
    a97f760 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    32010bb View commit details
    Browse the repository at this point in the history
  3. build: Make new additions to api private

    Believe these should be public, but it seems like the issue at build-time is that I'd be extending the public API.
    dangotbanned committed May 13, 2024
    Configuration menu
    Copy the full SHA
    14bd0e6 View commit details
    Browse the repository at this point in the history

Commits on May 15, 2024

  1. Configuration menu
    Copy the full SHA
    8a7bada View commit details
    Browse the repository at this point in the history

Commits on May 20, 2024

  1. Configuration menu
    Copy the full SHA
    8dd2cde View commit details
    Browse the repository at this point in the history
  2. Make ChartType and is_chart_type public. Use typing.get_args to reduc…

    …e duplication of list of charts
    binste committed May 20, 2024
    Configuration menu
    Copy the full SHA
    a4de6d1 View commit details
    Browse the repository at this point in the history
  3. Update API docs

    binste committed May 20, 2024
    Configuration menu
    Copy the full SHA
    f12aea0 View commit details
    Browse the repository at this point in the history

Commits on May 21, 2024

  1. refactor: Adds ruff SIM101 and apply fixes

    As [requested](https://github.com/vega/altair/pull/3420/files/a4de6d1feb6f3f17c7f6d4c7738eb008ff33d17e#r1606780541).
    Seems to only occur in a few places, but all were autofix-able.
    dangotbanned committed May 21, 2024
    Configuration menu
    Copy the full SHA
    9b8f6a8 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    19010ba View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    082cd60 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    66a07d4 View commit details
    Browse the repository at this point in the history

Commits on May 23, 2024

  1. revert: Add str support back for "private" save functions

    An earlier commit assumed a minor optimization would be possible, by normalizing a `Union` early and reuse the narrowed type in "private" functions.
    
    @binste [comment](https://github.com/vega/altair/pull/3420/files#r1610440552)
    dangotbanned committed May 23, 2024
    Configuration menu
    Copy the full SHA
    fad765b View commit details
    Browse the repository at this point in the history