-
-
Notifications
You must be signed in to change notification settings - Fork 802
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
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 785f89c
chore: Add comment on `tooltip` annotation
dangotbanned 5423748
feat(typing): Add reference impl `EncodeKwds` from comment
dangotbanned da64738
feat(typing): Use `OneOrSeq[tps]` instead of `Union[*tps, list]` in `…
dangotbanned 5877b38
build: run `generate-schema-wrapper`
dangotbanned 4fe5f3a
wip: generate `typed_dict_args`
dangotbanned 0014cc8
refactor: Simplify `tools`, remove redundant code
dangotbanned 226038d
refactor: finish removing `altair_classes_prefix`
dangotbanned 77b101a
feat: `_create_encode_signature()` -> `EncodingArtifacts`
dangotbanned 675bc4e
build: run `generate-schema-wrapper`
dangotbanned 51a84a5
feat(typing): Provide a public export for `_EncodeKwds`
dangotbanned 2ef4b0f
Merge branch 'main' into public-typing
dangotbanned 4e0a098
Merge branch 'main' into pr/dangotbanned/3515
binste 2b9ad2c
Add docstring to _EncodeKwds
binste 79f317d
Rewrite EncodeArtifacts dataclass as a function
binste 1eb466d
Fix ruff issue due to old local ruff version
binste 0287eba
Change generate_encoding_artifacts to an iterator
binste bac1f67
docs: run `generate-schema-wrapper` with `indent_level=4`
dangotbanned 3419250
feat(typing): Move `ChartType`, `is_chart_type` to `alt.typing`
dangotbanned 5321b4b
Merge remote-tracking branch 'upstream/main' into public-typing
dangotbanned d16ec34
revert(ruff): Restore original ('RUF001`) line
dangotbanned e903528
Add type aliases for each channel
binste 6662fc9
Format
binste 28de27b
Use Union instead of | for compatibility with Py <3.10
binste b3fbe9c
Add channel type aliases to typing module. Add 'Type hints' section t…
binste 5ba8a8d
chore(ruff): Remove unused `F401` ignore
dangotbanned 49122b1
feat(typing): Move `Optional` export to `typing`
dangotbanned fe22c80
refactor: Move blank line append to `indent_docstring`
dangotbanned d3daf51
docs(typing): Remove empty type list from `EncodeKwds`
dangotbanned 914428a
refactor: Renaming, grouping, reducing repetition
dangotbanned 11c58c3
refactor: More tidying up, annotating, reformat
dangotbanned 067f455
docs: Reference aliases in `generate_encoding_artifacts`
dangotbanned 6fefd12
Use full type hints instead of type alias in signatures for typeddict…
binste 9299a81
Merge remote-tracking branch 'upstream/main' into public-typing
dangotbanned b6f84e4
Rename 'Type hints' to 'Typing'
binste d4313c0
Ruff fix
binste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import Any, Mapping, Union | ||
from typing_extensions import TypedDict | ||
__all__ = ["EncodeKwds"] | ||
|
||
if sys.version_info >= (3, 10): | ||
from typing import TypeAlias | ||
else: | ||
from typing_extensions import TypeAlias | ||
|
||
ChannelType: TypeAlias = Union[str, Mapping[str, Any], Any] | ||
|
||
|
||
class EncodeKwds(TypedDict, total=False): | ||
""" | ||
Reference implementation from [comment code block](https://github.com/pola-rs/polars/pull/17995#issuecomment-2263609625). | ||
|
||
Aiming to define more specific `ChannelType`, without being exact. | ||
|
||
This would be generated alongside `tools.generate_schema_wrapper._create_encode_signature` | ||
""" | ||
|
||
angle: ChannelType | ||
color: ChannelType | ||
column: ChannelType | ||
description: ChannelType | ||
detail: ChannelType | list[Any] | ||
facet: ChannelType | ||
fill: ChannelType | ||
fillOpacity: ChannelType | ||
href: ChannelType | ||
key: ChannelType | ||
latitude: ChannelType | ||
latitude2: ChannelType | ||
longitude: ChannelType | ||
longitude2: ChannelType | ||
opacity: ChannelType | ||
order: ChannelType | list[Any] | ||
radius: ChannelType | ||
radius2: ChannelType | ||
row: ChannelType | ||
shape: ChannelType | ||
size: ChannelType | ||
stroke: ChannelType | ||
strokeDash: ChannelType | ||
strokeOpacity: ChannelType | ||
strokeWidth: ChannelType | ||
text: ChannelType | ||
theta: ChannelType | ||
theta2: ChannelType | ||
tooltip: ChannelType | list[Any] | ||
url: ChannelType | ||
x: ChannelType | ||
x2: ChannelType | ||
xError: ChannelType | ||
xError2: ChannelType | ||
xOffset: ChannelType | ||
y: ChannelType | ||
y2: ChannelType | ||
yError: ChannelType | ||
yError2: ChannelType | ||
yOffset: ChannelType | ||
from altair.vegalite.v5.schema.channels import _EncodeKwds as EncodeKwds |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@binste I know I removed
ChannelType
here, butaltair.typing
could define the precise types for each of the positional-args.pola-rs/polars#17995 (comment)
E.g.
ChannelX
,ChannelY
, ..., or similar?These did vary between each method, but I think this would be 6 unique channel type aliases total:
Unsure if there would be more needed for the yet-unwritten methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already got very far! Great to see that we're almost there 😄
I'm going through it now but I'm also wondering if we maybe just want to specify, for each channel, the types separately.
ChannelX
,ChannelY
, etc. works for me regarding naming. So basically what's there now forEncodingKwgs
but as individual type aliases.I don't know if we then even need a
TypedDict
, what do you think?Unpack
supports the usage suggested in pola-rs/polars#17995 (comment) only for Python >= 3.12. And I think the plot methods should then only accept**kwargs: Unpack[EncodeKwgs]
as else the arguments which are explicitly defined are defined twice. See typing docs.Downside would be that in Polars, users only get autocomplete and type checking for the channels which are explicitly defined but for simple exploratory charts, this could be enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @binste for such a thorough response!
I was trying to demonstrate the correct way to use
Unpack
there, to avoid that issue (the second one):Long screenshot
I'm not sure there's an issue with
typing_extensions.Unpack
which I used unconditionally there?Usually
ruff
orpylance
will warn me when I do something that isn't allowed inpy3.8
@binste
@dangotbanned comment
This was essentially the compromise of that, if that makes sense?
You have your common ones that can be called without kwargs, but more options with kwargs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're absolutely right! Thanks for the detailed explanation. Lost a bit the overview there but this brought me back on track :)
Just tested
/, **kwargs: Unpack[EncodeKwds]
withtyping_extensions.Unpack
on Python 3.11 and works as expected.Reviewing the rest now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarcoGorelli when you see this
The need for this solution is that
alt.Chart.encode
has alphabetically ordered parameters, whereaspl.DataFrame.plot
has:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marking as unresolved purely so this is easier to find during review
It is actually resolved unless #3515 (comment) turns up anything new