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

fix: Restrict static & runtime top-level imports #3482

Merged
merged 24 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2864c07
feat: generate an `__all__` for `channels.py`
dangotbanned Jul 16, 2024
2444f70
build: run `generate-schema-wrapper`
dangotbanned Jul 16, 2024
db31ff1
refactor: change the sources for some import in `api`
dangotbanned Jul 16, 2024
160ce60
refactor: add explicit import for `utils.parse_shorthand`
dangotbanned Jul 16, 2024
e21b8c8
build: run `update-init-file`
dangotbanned Jul 16, 2024
fac81d6
chore(ruff): replace blanket `noqa` with specific rules
dangotbanned Jul 16, 2024
7e3c5bd
refactor: add explicit imports in `v5.__init__.py`
dangotbanned Jul 16, 2024
2c286b5
feat: define an `__all__` for `api.py` manually
dangotbanned Jul 16, 2024
42cf5e8
refactor: add explicit imports for `Optional`, `Undefined`
dangotbanned Jul 16, 2024
973dfc7
build: run `update-init-file`
dangotbanned Jul 16, 2024
38535a0
test: use explicit import for `Undefined`
dangotbanned Jul 16, 2024
a85d77a
test: use explicit import for `AltairDeprecationWarning`
dangotbanned Jul 16, 2024
b913cd8
fix: add deprecations to `api.py`s `__all__`
dangotbanned Jul 16, 2024
c956968
Merge branch 'main' into fix-pylance-imports
dangotbanned Jul 17, 2024
a7be727
Merge branch 'main' into fix-pylance-imports
dangotbanned Jul 19, 2024
1e0c12d
Merge branch 'main' into fix-pylance-imports
dangotbanned Jul 20, 2024
52c939c
revert: Include `mixins`, `DataType`, `TOPLEVEL_ONLY_KEYS` in `api.__…
dangotbanned Jul 21, 2024
54723ec
chore: remove TODO
dangotbanned Jul 21, 2024
f3ea6ff
revert: include channels mixins and `with_property_setters` in `chann…
dangotbanned Jul 21, 2024
13c2d87
build: run `generate-schema-wrapper`
dangotbanned Jul 21, 2024
d0dcc5d
build: run `update-init-file`
dangotbanned Jul 21, 2024
050f16b
Merge remote-tracking branch 'upstream/main' into fix-pylance-imports
dangotbanned Jul 21, 2024
76d10e3
Merge branch 'main' into fix-pylance-imports
dangotbanned Jul 22, 2024
fa65edd
Merge branch 'main' into fix-pylance-imports
dangotbanned Jul 22, 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
2 changes: 1 addition & 1 deletion altair/__init__.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@binste altair.__all__ is now identical to main 👍

Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def __dir__():
from altair.vegalite.v5.schema.core import Dict
from altair.jupyter import JupyterChart
from altair.expr import expr
from altair.utils import AltairDeprecationWarning
from altair.utils import AltairDeprecationWarning, parse_shorthand, Optional, Undefined


def load_ipython_extension(ipython):
Expand Down
2 changes: 1 addition & 1 deletion altair/vegalite/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# ruff: noqa
# ruff: noqa: F403
from .v5 import *
2 changes: 1 addition & 1 deletion altair/vegalite/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Altair schema wrappers"""

# ruff: noqa
# ruff: noqa: F403
from .v5.schema import *
11 changes: 9 additions & 2 deletions altair/vegalite/v5/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# ruff: noqa
# ruff: noqa: F401, F403
from .schema import *
from .api import *

from altair.expr.core import datum

from .display import VegaLite, renderers
from .display import (
VegaLite,
renderers,
VEGALITE_VERSION,
VEGAEMBED_VERSION,
VEGA_VERSION,
)
from .compiler import vegalite_compilers

from .data import (
Expand All @@ -17,3 +23,4 @@
default_data_transformer,
data_transformers,
)
from .theme import themes
57 changes: 55 additions & 2 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import functools
import operator
from copy import deepcopy as _deepcopy
from .schema import core, channels, mixins, Undefined, SCHEMA_URL

from altair.utils import Optional
from .schema import core, channels, mixins, SCHEMA_URL

from altair.utils import Optional, Undefined
from .data import data_transformers
from ... import utils
from ...expr import core as _expr_core
Expand Down Expand Up @@ -128,6 +129,58 @@
SingleTimeUnit_T,
)

__all__ = [
"TOPLEVEL_ONLY_KEYS",
"Bin",
"ChainedWhen",
"Chart",
"ChartDataType",
"ChartType",
"ConcatChart",
"DataType",
"FacetChart",
"FacetMapping",
"HConcatChart",
"Impute",
"LayerChart",
"LookupData",
"Parameter",
"ParameterExpression",
"RepeatChart",
"SelectionExpression",
"SelectionPredicateComposition",
"Then",
"Title",
"TopLevelMixin",
"VConcatChart",
"When",
"binding",
"binding_checkbox",
"binding_radio",
"binding_range",
"binding_select",
"check_fields_and_encodings",
"concat",
"condition",
"graticule",
"hconcat",
"is_chart_type",
"layer",
"mixins",
"param",
"repeat",
"selection",
"selection_interval",
"selection_multi",
"selection_point",
"selection_single",
"sequence",
"sphere",
"topo_feature",
"value",
"vconcat",
"when",
]

ChartDataType: TypeAlias = Optional[Union[DataType, core.Data, str, core.Generator]]
_TSchemaBase = TypeVar("_TSchemaBase", bound=core.SchemaBase)
Expand Down
107 changes: 107 additions & 0 deletions altair/vegalite/v5/schema/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,113 @@
from ._typing import * # noqa: F403


__all__ = [
"X2",
"Y2",
"Angle",
"AngleDatum",
"AngleValue",
"Color",
"ColorDatum",
"ColorValue",
"Column",
"DatumChannelMixin",
"Description",
"DescriptionValue",
"Detail",
"Facet",
"FieldChannelMixin",
"Fill",
"FillDatum",
"FillOpacity",
"FillOpacityDatum",
"FillOpacityValue",
"FillValue",
"Href",
"HrefValue",
"Key",
"Latitude",
"Latitude2",
"Latitude2Datum",
"Latitude2Value",
"LatitudeDatum",
"Longitude",
"Longitude2",
"Longitude2Datum",
"Longitude2Value",
"LongitudeDatum",
"Opacity",
"OpacityDatum",
"OpacityValue",
"Order",
"OrderValue",
"Radius",
"Radius2",
"Radius2Datum",
"Radius2Value",
"RadiusDatum",
"RadiusValue",
"Row",
"Shape",
"ShapeDatum",
"ShapeValue",
"Size",
"SizeDatum",
"SizeValue",
"Stroke",
"StrokeDash",
"StrokeDashDatum",
"StrokeDashValue",
"StrokeDatum",
"StrokeOpacity",
"StrokeOpacityDatum",
"StrokeOpacityValue",
"StrokeValue",
"StrokeWidth",
"StrokeWidthDatum",
"StrokeWidthValue",
"Text",
"TextDatum",
"TextValue",
"Theta",
"Theta2",
"Theta2Datum",
"Theta2Value",
"ThetaDatum",
"ThetaValue",
"Tooltip",
"TooltipValue",
"Url",
"UrlValue",
"ValueChannelMixin",
"X",
"X2Datum",
"X2Value",
"XDatum",
"XError",
"XError2",
"XError2Value",
"XErrorValue",
"XOffset",
"XOffsetDatum",
"XOffsetValue",
"XValue",
"Y",
"Y2Datum",
"Y2Value",
"YDatum",
"YError",
"YError2",
"YError2Value",
"YErrorValue",
"YOffset",
"YOffsetDatum",
"YOffsetValue",
"YValue",
"with_property_setters",
]


class FieldChannelMixin:
_encoding_name: str

Expand Down
19 changes: 10 additions & 9 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import polars as pl

import altair as alt
from altair.utils.schemapi import Undefined
dangotbanned marked this conversation as resolved.
Show resolved Hide resolved

try:
import vl_convert as vlc
Expand Down Expand Up @@ -1351,14 +1352,14 @@ def test_subcharts_with_same_data(method, data):
text = point.mark_text()

chart1 = func(point, line, text)
assert chart1.data is not alt.Undefined
assert all(c.data is alt.Undefined for c in getattr(chart1, method))
assert chart1.data is not Undefined
assert all(c.data is Undefined for c in getattr(chart1, method))

if method != "concat":
op = OP_DICT[method]
chart2 = op(op(point, line), text)
assert chart2.data is not alt.Undefined
assert all(c.data is alt.Undefined for c in getattr(chart2, method))
assert chart2.data is not Undefined
assert all(c.data is Undefined for c in getattr(chart2, method))


@pytest.mark.parametrize("method", ["layer", "hconcat", "vconcat", "concat"])
Expand All @@ -1373,20 +1374,20 @@ def test_subcharts_different_data(method, data):
nodata = alt.Chart().mark_point().encode(x="x:Q", y="y:Q")

chart1 = func(point, otherdata)
assert chart1.data is alt.Undefined
assert chart1.data is Undefined
assert getattr(chart1, method)[0].data is data

chart2 = func(point, nodata)
assert chart2.data is alt.Undefined
assert chart2.data is Undefined
assert getattr(chart2, method)[0].data is data


def test_layer_facet(basic_chart):
chart = (basic_chart + basic_chart).facet(row="row:Q")
assert chart.data is not alt.Undefined
assert chart.spec.data is alt.Undefined
assert chart.data is not Undefined
assert chart.spec.data is Undefined
for layer in chart.spec.layer:
assert layer.data is alt.Undefined
assert layer.data is Undefined

dct = chart.to_dict()
assert "data" in dct
Expand Down
7 changes: 4 additions & 3 deletions tests/vegalite/v5/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re

import altair.vegalite.v5 as alt
from altair.utils.deprecation import AltairDeprecationWarning


def test_variable_param():
Expand Down Expand Up @@ -82,9 +83,9 @@ def test_selection_deprecation():
alt.selection_interval()

# this v4 syntax is deprecated
with pytest.warns(alt.utils.deprecation.AltairDeprecationWarning):
with pytest.warns(AltairDeprecationWarning):
alt.selection_single()
with pytest.warns(alt.utils.deprecation.AltairDeprecationWarning):
with pytest.warns(AltairDeprecationWarning):
alt.selection_multi()

# new syntax
Expand All @@ -95,7 +96,7 @@ def test_selection_deprecation():
# this v4 syntax is deprecated
brush = alt.selection_interval()
c = alt.Chart().mark_point()
with pytest.warns(alt.utils.deprecation.AltairDeprecationWarning):
with pytest.warns(AltairDeprecationWarning):
c.add_selection(brush)


Expand Down
Loading