Skip to content

Commit

Permalink
feat(typing): Update api._top_schema_base to work with `super(..., …
Browse files Browse the repository at this point in the history
…copy)`

The call to `.to_dict`, and 3 line comment also appeared in the traceback for validation errors on `ChartType`s.
This removes the need for the comment and type ignore that it is describing.
See vega#3480 for more info on the introduction of `_top_schema_base`
  • Loading branch information
dangotbanned committed Aug 9, 2024
1 parent 44d2873 commit 1a51948
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1712,9 +1712,27 @@ def _top_schema_base( # noqa: ANN202
"""
Enforces an intersection type w/ `SchemaBase` & `TopLevelMixin` objects.
Use for instance methods.
Use for methods, called from `TopLevelMixin` that are defined in `SchemaBase`.
Notes
-----
- The `super` sub-branch is not statically checked *here*.
- It would widen the inferred intersection to:
- `(<subclass of SchemaBase and TopLevelMixin> | super)`
- Both dunder attributes are not in the `super` type stubs
- Requiring 2x *# type: ignore[attr-defined]*
- However it is required at runtime for any cases that use `super(..., copy)`.
- The inferred type **is** used statically **outside** of this function.
"""
if isinstance(obj, core.SchemaBase) and isinstance(obj, TopLevelMixin):
SchemaBase = core.SchemaBase
if (isinstance(obj, SchemaBase) and isinstance(obj, TopLevelMixin)) or (
not TYPE_CHECKING
and (
isinstance(obj, super)
and issubclass(obj.__self_class__, SchemaBase)
and obj.__thisclass__ is TopLevelMixin
)
):
return obj
else:
msg = f"{type(obj).__name__!r} does not derive from {type(core.SchemaBase).__name__!r}"
Expand Down Expand Up @@ -1803,10 +1821,7 @@ def to_dict(
# remaining to_dict calls are not at top level
context["top_level"] = False

# TopLevelMixin instance does not necessarily have to_dict defined
# but due to how Altair is set up this should hold.
# Too complex to type hint right now
vegalite_spec: Any = super(TopLevelMixin, copy).to_dict( # type: ignore[misc]
vegalite_spec: Any = _top_schema_base(super(TopLevelMixin, copy)).to_dict(
validate=validate, ignore=ignore, context=dict(context, pre_transform=False)
)

Expand Down

0 comments on commit 1a51948

Please sign in to comment.