feat(typing): Fully annotate api.py#3508
Conversation
To highlight all the missing annotations to fix and autofix `None` return
Excluding `*args` on `ChartType` wrappers. They need to be defined in alignment in multiple places, which is more complex
…`dict` Among these, many locations already assume `str` keys in the implementation - without checking
``` altair\vegalite\v5\api.py:4368: error: Argument 1 of "__iadd__" is incompatible with "__add__" of supertype "TopLevelMixin"; supertype defines the argument type as "Chart | RepeatChart | ConcatChart | HConcatChart | VConcatChart | FacetChart | LayerChart" [override] def __iadd__(self, other: LayerChart | Chart) -> Self: ^~~~~~~~~~~~~~~~~~~~~~~~~ altair\vegalite\v5\api.py:4368: note: This violates the Liskov substitution principle altair\vegalite\v5\api.py:4368: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides altair\vegalite\v5\api.py:4376: error: Argument 1 of "__add__" is incompatible with supertype "TopLevelMixin"; supertype defines the argument type as "Chart | RepeatChart | ConcatChart | HConcatChart | VConcatChart | FacetChart | LayerChart" [override] def __add__(self, other: LayerChart | Chart) -> Self: ^~~~~~~~~~~~~~~~~~~~~~~~~ altair\vegalite\v5\api.py:4376: note: This violates the Liskov substitution principle altair\vegalite\v5\api.py:4376: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides ```
This could later be extended to other modules, but for now `api` is complete.
binste
left a comment
There was a problem hiding this comment.
It's very satisfying to see api.py finally being fully typed :) Didn't have it in me when doing #3143 to go all the way. Thanks!
Either all of these functions should expect SchemaBase | dict or all SchemaBase.
Unless I'm missing something, there does not seem to be a way to use a dict to build a compound chart this way.
Thanks for uncovering it! Agree with your suggestion, dicts don't make sense at this stage.
|
Thanks! ❤️ |
@binste would it be worth opening an issue for the Minimal Repro section? I'd still need to double-check the implications of this, but it should mean these two could be simplified significantly: altair/altair/vegalite/v5/api.py Lines 3844 to 3874 in a847833 altair/altair/vegalite/v5/api.py Lines 3877 to 3912 in a847833 Since |
This could later be extended to other modules, but for now
apiis complete.Follows work in #3480
Main motivation for this was trying to understand all of the private functions.
There seems to be an assumption that
ChartType's can be combined withdict's early on (_check_if_valid_subspec,_check_if_can_be_layered), but later code assumes all deriveSchemaBase.Minimal Repro
Test code block
Test Failures
Summary
Without typing information, finding this the conflict between
_combine_subchart_dataand the earlier_check_if_valid_subspec,_check_if_can_be_layeredis much less clear.Suggestion
Either all of these functions should expect
SchemaBase | dictor allSchemaBase.Unless I'm missing something, there does not seem to be a way to use a
dictto build a compound chart this way.