Skip to content

fix: cleanup ruff rules#3967

Open
alec-bike wants to merge 3 commits intovega:mainfrom
alec-bike:cleanup-ruff-rules
Open

fix: cleanup ruff rules#3967
alec-bike wants to merge 3 commits intovega:mainfrom
alec-bike:cleanup-ruff-rules

Conversation

@alec-bike
Copy link
Contributor

@alec-bike alec-bike commented Feb 26, 2026

This PR tidies up some ruff rules and comments in pyproject.toml:

  • increase max-complexity to 15 (allows removal of some noqa: C901 annotations) reverted
  • remove commented-out preview-only ruff rules1

Also, the experimental tool.pyright section has been simplified (and tested against pyright and basedpyright).

Footnotes

  1. ruff preview was turned off in fix: type issues with Chart mark methods. #3936

@alec-bike alec-bike force-pushed the cleanup-ruff-rules branch 4 times, most recently from 1b4a996 to 07f7cb9 Compare March 2, 2026 18:24
Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

@alec-bike there are 2 PRs here

Pyright

Also, the experimental tool.pyright section has been simplified (and tested against pyright and basedpyright).

Could you set up pyright to run in CI first, before making changes like this please?

I was working towards that a while back, but there's nothing verifying your claims.

Locally, pyright is complaining about things that are invisible to a reviewer:

Show arbitrary example

image

Ruff

I don't see any value in removing C901 comments.

Refactoring the functions is a good way to learn about how altair works or just simplify things for future readers 🙂

@alec-bike alec-bike force-pushed the cleanup-ruff-rules branch from 07f7cb9 to fc7c6ff Compare March 4, 2026 16:16
@alec-bike
Copy link
Contributor Author

Thanks for the review @dangotbanned.

Could you set up pyright to run in CI first, before making changes like this please?

I've been wondering about the status of pyright (and multiple type checkers) in altair. Is the goal for altair to support both mypy and pyright in CI?

I've been running ty for my testing, using a local ty.toml config to keep it separate from pyproject.toml. Would altair also want to run ty in CI?

@alec-bike alec-bike force-pushed the cleanup-ruff-rules branch 5 times, most recently from 0bac43d to c32f652 Compare March 6, 2026 17:22
@alec-bike alec-bike requested a review from dangotbanned March 6, 2026 17:37
@alec-bike alec-bike marked this pull request as ready for review March 6, 2026 17:38
@alec-bike alec-bike force-pushed the cleanup-ruff-rules branch 2 times, most recently from 0d679f9 to 41687d0 Compare March 6, 2026 18:40
@dangotbanned
Copy link
Member

#3967 (comment)

I've been wondering about the status of pyright (and multiple type checkers) in altair. Is the goal for altair to support both mypy and pyright in CI?

I guess this is a history of the progress I made with pyright support:

Before that, there was a long road to supporting mypy:

This section of the config was where there were still gaps, but IIRC some test modules were an issue as well:

altair/pyproject.toml

Lines 374 to 378 in 356c78b

ignore = [
"./altair/vegalite/v6/schema/channels.py", # 716 warns
"./altair/vegalite/v6/schema/mixins.py", # 1001 warns
"./altair/jupyter/", # Mostly untyped
"./tests/test_jupyter_chart.py", # Based on untyped module

I've been running ty for my testing, using a local ty.toml config to keep it separate from pyproject.toml. Would altair also want to run ty in CI?

I'm very much looking forward to using ty when it is ready, but there are many gaps that impact an upstream project I contribute to:

Using ty locally is probably the best move for now - I have no doubt it will play a really important role in moving python forward.

Running pyright in ci is quite doable - with the right amount of effort.
If you look at Python Type System Conformance Test Results - it really is the bar for following the rules 😄
Wish it was faster though

Copy link
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks for the changes @alec-bike

I do still have a few more questions though

"""
context = context or {}
kwds: Map = {
kwds: dict = {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
kwds: dict = {
kwds: Map = {

That alias expands to a type that actually supports **kwds

Map: TypeAlias = Mapping[str, Any]

from __future__ import annotations

from typing import Any


def unpack(**kwargs: Any) -> dict[str, Any]:
    return kwargs


def any_dict(kwds: dict) -> dict[str, Any]:
    return unpack(**kwds)


def safe_dict(kwds: dict[str, Any]) -> dict[str, Any]:
    return unpack(**kwds)


any_dict({"keyword": "argument"})
any_dict({1: "boom"})

safe_dict({"keyword": "argument"})
safe_dict({1: "boom"})
# Argument of type "dict[int, str]" cannot be assigned to parameter "kwds" of type
# "dict[str, Any]" in function "safe_dict"
#  "Literal[1]" is not assignable to "str"
Traceback (most recent call last):
  File "../t.py", line 19, in <module>
    any_dict({1: "boom"})
    ~~~~~~~~^^^^^^^^^^^^^
  File "../t", line 11, in any_dict
    return unpack(**kwds)
TypeError: keywords must be strings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. ty flagged kwds: Map as invalid-argument-type whereas kwds: dict fixed the ty warning. I've reverted this for now and will check with the ty devs.

context["top_level"] = False

vegalite_spec = _top_schema_base(super(TopLevelMixin, copy)).to_dict(
vegalite_spec: Any = _top_schema_base(super(TopLevelMixin, copy)).to_dict(
Copy link
Member

Choose a reason for hiding this comment

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

The Any is a result of the change made on _top_schema_base

Suggested change
vegalite_spec: Any = _top_schema_base(super(TopLevelMixin, copy)).to_dict(
vegalite_spec = _top_schema_base(super(TopLevelMixin, copy)).to_dict(

Copy link
Contributor Author

@alec-bike alec-bike Mar 6, 2026

Choose a reason for hiding this comment

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

I added this today to revert a change I'd made in commit a2afad3. That commit fixed a ty issue but introduced a pyright error.

I also reverted line 460 back to nm: Any.

With these changes, I no longer see pyright errors on those lines.

@alec-bike alec-bike force-pushed the cleanup-ruff-rules branch from 212841c to e84ddc9 Compare March 7, 2026 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants