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: Improve the syntax for conditions with multiple predicates #3427

Merged
merged 122 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
303f784
fix: add overloads for `condition`
dangotbanned May 29, 2024
866a50e
build: regen `__all__` excluding `@overload`
dangotbanned May 29, 2024
7ddd214
fix: add statement overloads for `condition`
dangotbanned May 29, 2024
2b3c2f8
refactor: add `typing.Tuple` to `api` and `update_init_file`
dangotbanned May 30, 2024
61ab923
feat(typing): amend and extend aliases for conditions
dangotbanned May 30, 2024
a33efa3
feat(typing): update `condition` signature, overloads to use new aliases
dangotbanned May 30, 2024
79af12d
refactor: extract condition utility functions
dangotbanned May 30, 2024
d901679
refactor: extract condition parsing functions
dangotbanned May 30, 2024
03a372c
refactor: add `ruff` ignores to keep `expr` in `api`
dangotbanned May 30, 2024
8d455d1
refactor: replaced contents of `condition` with extracted functions
dangotbanned May 30, 2024
8df67e1
style: rerun `ruff`
dangotbanned May 30, 2024
aa35acf
test: Skip tests on Win that require a tz database
dangotbanned May 30, 2024
747ca6b
fix(typing): Correct `SchemaBase.to_dict` return type
dangotbanned May 31, 2024
14f6ea1
perf: Build base `units` for `utils.parse_shorthand` once
dangotbanned May 31, 2024
f20949d
feat: align both sides of `if_true`/ `if_false` parsing
dangotbanned May 31, 2024
9d202e5
test: Update single test that relied on intermediate shorthand
dangotbanned May 31, 2024
278f4f4
feat(typing): add `_is_statement_type` guard
dangotbanned May 31, 2024
113a793
docs: add reference to `predicate`
dangotbanned May 31, 2024
d6fea15
feat(typing): adds `_Conditions` intermediate representation
dangotbanned May 31, 2024
9c89bce
feat: Adds initial `when-then-otherwise` implementation
dangotbanned May 31, 2024
a09c13c
test: Add tests for `when-then-otherwise`
dangotbanned May 31, 2024
e7be069
fix(typing): Use `typing.List`
dangotbanned May 31, 2024
1ad06f4
fix(typing): fix(typing): Correct SchemaBase.to_dict return type
dangotbanned May 31, 2024
61279cd
fix(typing): Use safer `typing_extensions.runtime_checkable` unless `…
dangotbanned May 31, 2024
cf92402
perf: Don't recheck type after positive case in `infer_encoding_types`
dangotbanned May 31, 2024
22e89a8
feat: adds `_Then.to_dict`
dangotbanned May 31, 2024
dfc14de
feat: allow `_Then` as an encoding input
dangotbanned May 31, 2024
328338a
test: Add test for `_Then` as encoding condition
dangotbanned May 31, 2024
430898f
fix(typing): Remove `UndefinedType` from `_ConditionType`
dangotbanned Jun 2, 2024
5f49715
refactor: Simplify `_predicate_to_condition`
dangotbanned Jun 2, 2024
460444c
refactor: removed outdated comments
dangotbanned Jun 2, 2024
c8469ed
fix(typing): Correct `alt.value` return annotation
dangotbanned Jun 2, 2024
4b8353b
refactor: moved comment
dangotbanned Jun 2, 2024
f1d9dc2
feat(typing): extends aliases and guards
dangotbanned Jun 2, 2024
38d425f
feat: extend and refactor `when-then-otherwise` functionality
dangotbanned Jun 2, 2024
80c32d3
fix(typing): exclude `typing_extensions.Protocol` and short `TypeVar`…
dangotbanned Jun 2, 2024
7b5f711
test: update and extend tests for `when-then-otherwise`
dangotbanned Jun 2, 2024
b962c48
fix: add item to `pyproject.toml` to silence import errors for `pylan…
dangotbanned Jun 2, 2024
3a1c640
test: check output type on all `when-then-otherwise` type errors
dangotbanned Jun 2, 2024
7b671db
test: update tests, examples using deprecated `pandas` parameters
dangotbanned Jun 2, 2024
5f631de
feat: Support additional predicate types in `when`, `condition`
dangotbanned Jun 3, 2024
fbd9205
feat: adds conversion functions between `alt.condition`/`when-then-ot…
dangotbanned Jun 3, 2024
9af55c2
feat: Adds config to `alt.value` wrapping in `when-then-otherwise`
dangotbanned Jun 3, 2024
80fbcbd
test: replace now-removed `wrap_value` parameter
dangotbanned Jun 3, 2024
5da5622
test: additional `when-then-otherwise` tests
dangotbanned Jun 3, 2024
c994bf8
fix(typing): satisfy mypy
dangotbanned Jun 3, 2024
8c0f45a
revert: 7b671db85d344fd6e0ca2d2a25a862d84118988c
dangotbanned Jun 3, 2024
989b1f5
feat(typing): adds `_FieldEqualType` for when constraints
dangotbanned Jun 16, 2024
7d41219
docs, feat: Add draft doc for `alt.when` and make public
dangotbanned Jun 16, 2024
80a0812
docs: Replace markdown syntax with rst directives
dangotbanned Jun 16, 2024
bfa6d51
Merge branch 'vega:main' into condition-multiple
dangotbanned Jun 19, 2024
237c874
sync
dangotbanned Jun 19, 2024
f4b51b8
Merge branch 'condition-multiple' of https://github.com/dangotbanned/…
dangotbanned Jun 19, 2024
0a070c9
refactor: Remove unused `expr` import in `api`
dangotbanned Jun 19, 2024
b85223f
feat(typing): Preserve wrapper type in `alt.condition`
dangotbanned Jun 19, 2024
30c1ad1
fix(typing): remove now-unused ignores
dangotbanned Jun 19, 2024
95093d7
fix: Simplify complex `expr` import behaviour
dangotbanned Jun 19, 2024
55cc301
fix: silence `unused-ignore` for `test_api` until `Chart.encode` fix …
dangotbanned Jun 19, 2024
62ad8d1
docs: refine `alt.when` doc
dangotbanned Jun 22, 2024
9cf84f9
docs: Improve `_When` docs
dangotbanned Jun 22, 2024
58e0054
feat: make `alt.When` public
dangotbanned Jun 22, 2024
fe8ab25
test: Update test references to use public `alt.When`
dangotbanned Jun 22, 2024
e159ea0
docs: Fix some user guide sphinx errors
dangotbanned Jun 22, 2024
8e92953
Merge remote-tracking branch 'origin/main' into condition-multiple
dangotbanned Jun 28, 2024
910f493
revert(typing): re-enable mypy unused-ignore
dangotbanned Jun 28, 2024
2276d50
test: better utilise `pytest.raises(match=...)`
dangotbanned Jun 28, 2024
591a55b
refactor: UX-first improvements to imports, annotations
dangotbanned Jun 28, 2024
0be41e8
docs: Add doc for `_Then.otherwise`
dangotbanned Jun 29, 2024
11841d0
docs: Add doc for `_Then.when`
dangotbanned Jun 29, 2024
b48a812
feat: make `Then`, `ChainedWhen` public
dangotbanned Jun 29, 2024
8c1e36b
docs: Add doc for `ChainedWhen.then`, misc fixes
dangotbanned Jun 29, 2024
5baaa34
docs: add `api-cls` tree
dangotbanned Jun 29, 2024
801cccc
refactor: remove `seq_as_lit` option
dangotbanned Jun 30, 2024
25ac2fa
Merge branch 'main' into condition-multiple
dangotbanned Jun 30, 2024
b238aef
Merge branch 'main' into condition-multiple
dangotbanned Jul 2, 2024
5f7d949
chore: remove type ignore comments
dangotbanned Jul 2, 2024
f89990f
revert: remove condition -> expr conversion experiment
dangotbanned Jul 3, 2024
c217b4f
fix: `mypy` error
dangotbanned Jul 3, 2024
8f66b7a
Merge branch 'main' into condition-multiple
dangotbanned Jul 3, 2024
d12a741
feat: un-special case `Then`
dangotbanned Jul 6, 2024
87b6014
fix: exclude `_typing` imports from `__all__`
dangotbanned Jul 6, 2024
6f06b80
refactor(typing): replace `dict` w/ `Map` when possible
dangotbanned Jul 6, 2024
1292962
feat: redesign `when-then-otherwise` to account for condition restric…
dangotbanned Jul 6, 2024
b110f65
test: update test to use `Then`'s getattr
dangotbanned Jul 6, 2024
f33a9cd
test: adds `test_when_stress`, `test_when_condition_parity`
dangotbanned Jul 6, 2024
f95be1a
ci: fix `PT001` rule inversion conflict
dangotbanned Jul 6, 2024
31bad3f
feat(DRAFT): add experimental `_str_as`
dangotbanned Jul 6, 2024
d0784f8
refactor: `str_as_lit` -> `str_as`, invert default
dangotbanned Jul 7, 2024
fbd9088
Merge branch 'main' into condition-multiple
dangotbanned Jul 7, 2024
c8f7d6c
refactor: update `str_as` error
dangotbanned Jul 8, 2024
8f5d352
refactor: rename `kwargs` -> `kwds`
dangotbanned Jul 8, 2024
49c4d24
refactor(typing): use `_FieldEqualType` more consistently
dangotbanned Jul 8, 2024
38d8a8e
refactor(typing): consistently annotate `TypeAlias`s
dangotbanned Jul 8, 2024
bd26c07
refactor(typing): remove lesser used aliases
dangotbanned Jul 8, 2024
6245a8b
refactor(typing): replace lingering `UndefinedType`
dangotbanned Jul 8, 2024
5e64fcb
refactor: inline `_str_as`, add note for `PEP728`
dangotbanned Jul 8, 2024
a3531fb
test: remove `alt.value` from example-based tests
dangotbanned Jul 8, 2024
03d7ca5
Merge branch 'main' into condition-multiple
dangotbanned Jul 9, 2024
f8d7529
Merge branch 'main' into condition-multiple
dangotbanned Jul 13, 2024
c0afbad
Merge branch 'main' into condition-multiple
dangotbanned Jul 13, 2024
86880c9
merge remote
dangotbanned Jul 15, 2024
e712b61
Merge branch 'condition-multiple' of https://github.com/dangotbanned/…
dangotbanned Jul 15, 2024
d607c70
Merge branch 'main' into condition-multiple
dangotbanned Jul 15, 2024
f992324
Merge branch 'main' into condition-multiple
dangotbanned Jul 16, 2024
0f862ad
Merge branch 'main' into condition-multiple
dangotbanned Jul 18, 2024
290da86
refactor: remove `str_as` and `alt.value(statement)` wrapping
dangotbanned Jul 18, 2024
3fa4a3f
refactor(typing): remove/reorganize aliases and guards
dangotbanned Jul 18, 2024
528ca16
docs: minor consistency update to `_ComposablePredicateType`
dangotbanned Jul 18, 2024
159bcf1
chore: exclude `TypeAliasType` from `__all__`
dangotbanned Jul 18, 2024
500f7e1
test: update existing `when` tests
dangotbanned Jul 18, 2024
db473c3
test: adds tests @mattijn authored during review
dangotbanned Jul 18, 2024
de08676
docs: fix lowercase
dangotbanned Jul 18, 2024
633736d
docs: remove `str_as` references
dangotbanned Jul 18, 2024
0f54057
revert: remove unrelated `expr` changes
dangotbanned Jul 18, 2024
0ed9eab
docs(typing): add example to `_OneOrSeq`
dangotbanned Jul 18, 2024
fc6ce16
revert: Remove unrelated `pyproject.toml` changes
dangotbanned Jul 18, 2024
4bc4732
chore: remove TODO comment
dangotbanned Jul 18, 2024
f0bc7b8
refactor: Define `SHORTHAND_KEYS` in `utils.core`
dangotbanned Jul 18, 2024
33e17fa
docs: Update `_TestPredicateType` link
dangotbanned Jul 18, 2024
59af293
refactor: remove single use `_is_composable_type` guard
dangotbanned Jul 18, 2024
c70d535
fix: More useful error message for `alt.[condition|when](predicate)`
dangotbanned Jul 18, 2024
87cd9bd
docs: another consistency update
dangotbanned Jul 18, 2024
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
14 changes: 6 additions & 8 deletions altair/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
# ruff: noqa
__version__ = "5.4.0dev"

from typing import Any

# Necessary as mypy would see expr as the module alt.expr although due to how
# the imports are set up it is expr in the alt.expr module
expr: Any


# The content of __all__ is automatically written by
# tools/update_init_file.py. Do not modify directly.
__all__ = [
Expand Down Expand Up @@ -54,6 +47,7 @@
"BrushConfig",
"CalculateTransform",
"Categorical",
"ChainedWhen",
"Chart",
"ChartDataType",
"ChartType",
Expand Down Expand Up @@ -488,6 +482,7 @@
"TextDef",
"TextDirection",
"TextValue",
"Then",
"Theta",
"Theta2",
"Theta2Datum",
Expand Down Expand Up @@ -565,6 +560,7 @@
"VegaLiteSchema",
"ViewBackground",
"ViewConfig",
"When",
"WindowEventType",
"WindowFieldDef",
"WindowOnlyOp",
Expand Down Expand Up @@ -622,7 +618,6 @@
"load_ipython_extension",
"load_schema",
"mixins",
"overload",
"param",
"parse_shorthand",
"renderers",
Expand All @@ -645,6 +640,7 @@
"vconcat",
"vegalite",
"vegalite_compilers",
"when",
"with_property_setters",
]

Expand All @@ -654,7 +650,9 @@ def __dir__():


from altair.vegalite import *
from altair.vegalite.v5.schema.core import Dict
dangotbanned marked this conversation as resolved.
Show resolved Hide resolved
from altair.jupyter import JupyterChart
from altair.expr import expr
from altair.utils import AltairDeprecationWarning


Expand Down
2 changes: 2 additions & 0 deletions altair/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
update_nested,
display_traceback,
SchemaBase,
SHORTHAND_KEYS,
)
from .html import spec_to_html
from .plugin_registry import PluginRegistry
Expand All @@ -16,6 +17,7 @@


__all__ = (
"SHORTHAND_KEYS",
"AltairDeprecationWarning",
"Optional",
"PluginRegistry",
Expand Down
47 changes: 23 additions & 24 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@
import sys
import traceback
import warnings
from typing import (
Callable,
TypeVar,
Any,
Iterator,
cast,
Literal,
Protocol,
TYPE_CHECKING,
runtime_checkable,
)
from typing import Callable, TypeVar, Any, Iterator, cast, Literal, TYPE_CHECKING
from itertools import groupby
from operator import itemgetter

Expand All @@ -33,6 +23,10 @@

from altair.utils.schemapi import SchemaBase, Undefined

if sys.version_info >= (3, 12):
from typing import runtime_checkable, Protocol
else:
from typing_extensions import runtime_checkable, Protocol
if sys.version_info >= (3, 10):
from typing import ParamSpec
else:
Expand Down Expand Up @@ -199,6 +193,22 @@ def __dataframe__(
"utcsecondsmilliseconds",
]

VALID_TYPECODES = list(itertools.chain(iter(TYPECODE_MAP), iter(INV_TYPECODE_MAP)))

SHORTHAND_UNITS = {
"field": "(?P<field>.*)",
"type": "(?P<type>{})".format("|".join(VALID_TYPECODES)),
"agg_count": "(?P<aggregate>count)",
"op_count": "(?P<op>count)",
"aggregate": "(?P<aggregate>{})".format("|".join(AGGREGATES)),
"window_op": "(?P<op>{})".format("|".join(AGGREGATES + WINDOW_AGGREGATES)),
"timeUnit": "(?P<timeUnit>{})".format("|".join(TIMEUNITS)),
}

SHORTHAND_KEYS: frozenset[Literal["field", "aggregate", "type", "timeUnit"]] = (
frozenset(("field", "aggregate", "type", "timeUnit"))
)


def infer_vegalite_type_for_pandas(
data: object,
Expand Down Expand Up @@ -577,18 +587,6 @@ def parse_shorthand(
if not shorthand:
return {}

valid_typecodes = list(TYPECODE_MAP) + list(INV_TYPECODE_MAP)

units = {
"field": "(?P<field>.*)",
"type": "(?P<type>{})".format("|".join(valid_typecodes)),
"agg_count": "(?P<aggregate>count)",
"op_count": "(?P<op>count)",
"aggregate": "(?P<aggregate>{})".format("|".join(AGGREGATES)),
"window_op": "(?P<op>{})".format("|".join(AGGREGATES + WINDOW_AGGREGATES)),
"timeUnit": "(?P<timeUnit>{})".format("|".join(TIMEUNITS)),
}

patterns = []

if parse_aggregates:
Expand All @@ -606,7 +604,8 @@ def parse_shorthand(
patterns = list(itertools.chain(*((p + ":{type}", p) for p in patterns)))

regexps = (
re.compile(r"\A" + p.format(**units) + r"\Z", re.DOTALL) for p in patterns
re.compile(r"\A" + p.format(**SHORTHAND_UNITS) + r"\Z", re.DOTALL)
for p in patterns
)

# find matches depending on valid fields passed
Expand Down
Loading