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

refactor(typing): reduce type ignores in api.py #3480

Merged
merged 13 commits into from
Jul 20, 2024

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Jul 15, 2024

This PR is mainly working through type: ignore / TODO comments and fixing what I can.

My primary motivation for this was to be able to use pyright within altair more often.

Despite the existing type: ignore comments, I was getting 46 errors in api alone.

Anywhere that it appears I've made a change unrelated to existing comments, please know it was to solve one of these:

Large screenshot

image

Copy link
Contributor

@binste binste left a comment

Choose a reason for hiding this comment

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

Great improvements! I especially like the type intersection :) Added some questions.

altair/utils/schemapi.py Outdated Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
tools/schemapi/schemapi.py Outdated Show resolved Hide resolved
altair/vegalite/v5/api.py Show resolved Hide resolved
altair/vegalite/v5/api.py Show resolved Hide resolved
altair/vegalite/v5/api.py Outdated Show resolved Hide resolved
altair/vegalite/v5/api.py Show resolved Hide resolved
@dangotbanned
Copy link
Member Author

dangotbanned commented Jul 17, 2024

Great improvements! I especially like the type intersection :) Added some questions.

Thanks for the review @binste, I've just finished going through your comments.

No rush, but I'll wait for your response before reverting/making those changes.

FYI #3480 (commits) is not addressing that

I removed these in 6adf564 due to how complex they were getting, but both `mypy` and `pyright` seem happy with this solution
@dangotbanned
Copy link
Member Author

@binste as #3480 (comment) was the last part to resolve, I thought I'd go ahead with it in 21a3e84 (#3480)

Hopefully that will make this easier to close, as you can just revert that commit if needed

@dangotbanned dangotbanned requested a review from binste July 19, 2024 12:25
Copy link
Contributor

@binste binste left a comment

Choose a reason for hiding this comment

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

Thanks again! :)

@binste binste merged commit 72bd84d into vega:main Jul 20, 2024
11 checks passed
@dangotbanned dangotbanned deleted the reduce-api-type-ignore branch July 21, 2024 11:02
dangotbanned added a commit to dangotbanned/altair that referenced this pull request Jul 23, 2024
dangotbanned added a commit to dangotbanned/altair that referenced this pull request Jul 29, 2024
mattijn added a commit that referenced this pull request Jul 29, 2024
* docs: adapt `empty` note for `when`, `Then.when`

* docs: Add example for `Then.when`

* docs: Add an example to `Then.otherwise`

* chore(typing): add pyright ignore

Wasn't picked up in #3480

* docs: Add example for `When.then`

Adapted from the final example in https://vega.github.io/vega-lite/docs/condition.html

* docs: Add example for `ChainedWhen.then`

* docs: Add real-world examples for `alt.when`

Covers everything apart from chained when

* docs: Update `polars.when` references

Now all link to docs, rather than source code

#3492 (comment)

* feat: Add `__repr__` for `When`, `ChainedWhen`

#3492 (comment)

* Update altair/vegalite/v5/api.py

Co-authored-by: Mattijn van Hoek <mattijn@gmail.com>

---------

Co-authored-by: Mattijn van Hoek <mattijn@gmail.com>
binste pushed a commit that referenced this pull request Aug 2, 2024
* ci(ruff): Add `ANN` rules for `api.py` only

To highlight all the missing annotations to fix and autofix `None` return

* feat(typing): Complete annotations for most `api` functions

Excluding `*args` on `ChartType` wrappers. They need to be defined in alignment in multiple places, which is more complex

* feat(typing): Annotate more expr/params in `api`

* feat(typing): Various changes to enforce `dict[str, Any]` instead of `dict`

Among these, many locations already assume `str` keys in the implementation - without checking

* feat(typing): Misc minor method annotations

* feat(typing): Use `ChartType` in all `ChartType` dunder methods

* fix(ruff): Ignore some `ANN` rules that won't be fixed

* chore: add pyright ignore from #3492

* feat(typing): Improve `ChartType` constructor/factory annotations

* feat(typing): Annotate remaining functions in `api`

* feat(typing): Complete `RepeatChart` annotations

* fix(typing): Resolve Liskov violations

```
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
```

* chore(typing): Update more `dict` -> `dict[str, Any]`

* style(ruff): fix whitespace

* chore: Remove TODO fixed in #3480

* fix(typing): Enable `ANN003` and fix all in `api`

* fix(typing): Enable `ANN002` and fix all in `api`

* chore(ruff): Add note on `ANN`

This could later be extended to other modules, but for now `api` is complete.

* fix(typing): Add missing `FacetChart` annotations

To align with the other `ChartType`s
dangotbanned added a commit to dangotbanned/altair that referenced this pull request Aug 9, 2024
…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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants