Skip to content

Commit

Permalink
ci(ruff): Enforce the default C901 complexity (#3531)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned authored Aug 10, 2024
1 parent ae7e702 commit e402b3c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion altair/jupyter/jupyter_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(
)

@traitlets.observe("chart")
def _on_change_chart(self, change):
def _on_change_chart(self, change): # noqa: C901
"""Updates the JupyterChart's internal state when the wrapped Chart instance changes."""
new_chart = change.new
selection_watches = []
Expand Down
4 changes: 2 additions & 2 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def numpy_is_subtype(dtype: Any, subtype: Any) -> bool:
return False


def sanitize_pandas_dataframe(df: pd.DataFrame) -> pd.DataFrame:
def sanitize_pandas_dataframe(df: pd.DataFrame) -> pd.DataFrame: # noqa: C901
"""
Sanitize a DataFrame to prepare it for serialization.
Expand Down Expand Up @@ -502,7 +502,7 @@ def to_eager_narwhals_dataframe(data: IntoDataFrame) -> nw.DataFrame[Any]:
return data_nw


def parse_shorthand(
def parse_shorthand( # noqa: C901
shorthand: dict[str, Any] | str,
data: pd.DataFrame | DataFrameLike | None = None,
parse_aggregates: bool = True,
Expand Down
3 changes: 1 addition & 2 deletions altair/utils/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ def save(
version="5.0.0",
)

if json_kwds is None:
json_kwds = {}
json_kwds = json_kwds or {}
encoding = kwargs.get("encoding", "utf-8")
format = set_inspect_format_argument(format, fp, inline) # type: ignore[assignment]

Expand Down
14 changes: 8 additions & 6 deletions altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def _from_array_like(obj: Iterable[Any], /) -> list[Any]:
return list(obj)


def _todict(obj: Any, context: dict[str, Any] | None, np_opt: Any, pd_opt: Any) -> Any:
def _todict(obj: Any, context: dict[str, Any] | None, np_opt: Any, pd_opt: Any) -> Any: # noqa: C901
"""Convert an object to a dict representation."""
if np_opt is not None:
np = np_opt
Expand Down Expand Up @@ -761,10 +761,12 @@ def _get_default_error_message(
# Add unformatted messages of any remaining errors which were not
# considered so far. This is not expected to be used but more exists
# as a fallback for cases which were not known during development.
for validator, errors in errors_by_validator.items():
if validator not in {"enum", "type"}:
message += "\n".join([e.message for e in errors])

it = (
"\n".join(e.message for e in errors)
for validator, errors in errors_by_validator.items()
if validator not in {"enum", "type"}
)
message += "".join(it)
return message


Expand Down Expand Up @@ -868,7 +870,7 @@ def __init__(self, *args: Any, **kwds: Any) -> None:
if DEBUG_MODE and self._class_is_valid_at_instantiation:
self.to_dict(validate=True)

def copy(
def copy( # noqa: C901
self, deep: bool | Iterable[Any] = True, ignore: list[str] | None = None
) -> Self:
"""
Expand Down
9 changes: 5 additions & 4 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ class TopLevelMixin(mixins.ConfigMethodMixin):
_class_is_valid_at_instantiation: bool = False
data: Any

def to_dict(
def to_dict( # noqa: C901
self,
validate: bool = True,
*,
Expand Down Expand Up @@ -3876,7 +3876,8 @@ def _check_if_valid_subspec(
raise ValueError(err.format(attr, classname))


def _check_if_can_be_layered(spec: LayerType | dict[str, Any]) -> None:
# C901 fixed in https://github.com/vega/altair/pull/3520
def _check_if_can_be_layered(spec: LayerType | dict[str, Any]) -> None: # noqa: C901
"""Check if the spec can be layered."""

def _get(spec: LayerType | dict[str, Any], attr: str) -> Any:
Expand Down Expand Up @@ -4715,7 +4716,7 @@ def _remove_duplicate_params(layer: list[ChartType]) -> list[ChartType]:
return subcharts


def _combine_subchart_params(
def _combine_subchart_params( # noqa: C901
params: Optional[Sequence[_Parameter]], subcharts: list[ChartType]
) -> tuple[Optional[Sequence[_Parameter]], list[ChartType]]:
if utils.is_undefined(params):
Expand Down Expand Up @@ -4852,7 +4853,7 @@ def _repeat_names(
return params_named


def _remove_layer_props(
def _remove_layer_props( # noqa: C901
chart: LayerChart, subcharts: list[ChartType], layer_props: Iterable[str]
) -> tuple[dict[str, Any], list[ChartType]]:
def remove_prop(subchart: ChartType, prop: str) -> ChartType:
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ select = [
"ANN",
# unsorted-imports
"I001",
# complex-structure
"C901",
]
ignore = [
# Whitespace before ':'
Expand Down Expand Up @@ -360,7 +362,7 @@ ignore = [
]
# https://docs.astral.sh/ruff/settings/#lintpydocstyle
pydocstyle={ convention="numpy" }
mccabe={ max-complexity=18 }
mccabe={ max-complexity=10 }

[tool.ruff.lint.isort]
classes = ["expr", "datum"]
Expand All @@ -386,6 +388,8 @@ See https://github.com/vega/altair/pull/3449
[tool.ruff.lint.per-file-ignores]
# Only enforce type annotation rules on public api
"!altair/vegalite/v5/api.py" = ["ANN"]
# Allow complex if/elif branching during tests
"tests/**/*.py"= ["C901"]


[tool.ruff.format]
Expand Down
14 changes: 8 additions & 6 deletions tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def _from_array_like(obj: Iterable[Any], /) -> list[Any]:
return list(obj)


def _todict(obj: Any, context: dict[str, Any] | None, np_opt: Any, pd_opt: Any) -> Any:
def _todict(obj: Any, context: dict[str, Any] | None, np_opt: Any, pd_opt: Any) -> Any: # noqa: C901
"""Convert an object to a dict representation."""
if np_opt is not None:
np = np_opt
Expand Down Expand Up @@ -759,10 +759,12 @@ def _get_default_error_message(
# Add unformatted messages of any remaining errors which were not
# considered so far. This is not expected to be used but more exists
# as a fallback for cases which were not known during development.
for validator, errors in errors_by_validator.items():
if validator not in {"enum", "type"}:
message += "\n".join([e.message for e in errors])

it = (
"\n".join(e.message for e in errors)
for validator, errors in errors_by_validator.items()
if validator not in {"enum", "type"}
)
message += "".join(it)
return message


Expand Down Expand Up @@ -866,7 +868,7 @@ def __init__(self, *args: Any, **kwds: Any) -> None:
if DEBUG_MODE and self._class_is_valid_at_instantiation:
self.to_dict(validate=True)

def copy(
def copy( # noqa: C901
self, deep: bool | Iterable[Any] = True, ignore: list[str] | None = None
) -> Self:
"""
Expand Down
4 changes: 2 additions & 2 deletions tools/schemapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def get_python_type_representation(
return_as_str: Literal[False] = ...,
additional_type_hints: list[str] | None = ...,
) -> list[str]: ...
def get_python_type_representation(
def get_python_type_representation( # noqa: C901
self,
for_type_hints: bool = False,
return_as_str: bool = True,
Expand Down Expand Up @@ -682,7 +682,7 @@ def __call__(self, s: str) -> str:
rst_parse: RSTParse = RSTParse(RSTRenderer())


def indent_docstring(
def indent_docstring( # noqa: C901
lines: list[str], indent_level: int, width: int = 100, lstrip=True
) -> str:
"""Indent a docstring for use in generated code."""
Expand Down

0 comments on commit e402b3c

Please sign in to comment.