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

maint: change to ruff for formatting #3243

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
name: black-ruff-mypy
name: ruff-mypy
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
Expand All @@ -17,13 +17,13 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install .[dev]
- name: Check formatting with black
run: |
black --diff --color .
black --check .
- name: Lint with ruff
run: |
ruff check .
ruff check .
- name: Check formatting with ruff
run: |
ruff format --diff .
ruff format --check .
- name: Lint with mypy
run: |
mypy altair tests
5 changes: 2 additions & 3 deletions altair/_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ def _get_variable(name):
if ip is None:
raise ValueError(
"Magic command must be run within an IPython "
"environemnt, in which get_ipython() is defined."
"environment, in which get_ipython() is defined."
)
if name not in ip.user_ns:
raise NameError(
"argument '{}' does not match the "
"name of any defined variable".format(name)
"argument '{}' does not match the name of any defined variable".format(name)
)
return ip.user_ns[name]

Expand Down
27 changes: 15 additions & 12 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,21 @@ def to_list_if_array(val):
# geopandas >=0.6.1 uses the dtype geometry. Continue here
# otherwise it will give an error on np.issubdtype(dtype, np.integer)
continue
elif dtype_name in {
"Int8",
"Int16",
"Int32",
"Int64",
"UInt8",
"UInt16",
"UInt32",
"UInt64",
"Float32",
"Float64",
}: # nullable integer datatypes (since 24.0) and nullable float datatypes (since 1.2.0)
elif (
dtype_name
in {
"Int8",
"Int16",
"Int32",
"Int64",
"UInt8",
"UInt16",
"UInt32",
"UInt64",
"Float32",
"Float64",
}
): # nullable integer datatypes (since 24.0) and nullable float datatypes (since 1.2.0)
# https://pandas.pydata.org/pandas-docs/version/0.25/whatsnew/v0.24.0.html#optional-integer-na-support
col = df[col_name].astype(object)
df[col_name] = col.where(col.notnull(), None)
Expand Down
2 changes: 1 addition & 1 deletion altair/utils/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ def from_json(
cls,
json_string: str,
validate: bool = True,
**kwargs: Any
**kwargs: Any,
# Type hints for this method would get rather complicated
# if we want to provide a more specific return type
) -> Any:
Expand Down
59 changes: 23 additions & 36 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ Source = "https://github.com/altair-viz/altair"
[project.optional-dependencies]
dev = [
"hatch",
"ruff",
"black<24",
"ruff>=0.1.3",
"ipython",
"pytest",
"pytest-cov",
Expand Down Expand Up @@ -104,8 +103,8 @@ features = ["dev"]

[tool.hatch.envs.default.scripts]
test = [
"black --diff --color --check .",
"ruff check .",
"ruff format --diff --check .",
"mypy altair tests",
"python -m pytest --pyargs --doctest-modules tests altair",
]
Expand Down Expand Up @@ -146,27 +145,21 @@ publish-clean-build = [
"(cd doc && bash sync_website.sh)",
]

[tool.black]
line-length = 88
target-version = ["py38", "py39", "py310", "py311"]
include = '\.pyi?$'
extend-exclude = '''
/(
\.eggs
| \.git
| \.mypy_cache
| build
| dist
| doc
| tests/examples_arguments_syntax
| tests/examples_methods_syntax
| altair/vegalite/v\d*/schema
)/
'''

[tool.ruff]
target-version = "py38"
line-length = 88
indent-width = 4
exclude = [
".git",
"build",
"__pycache__",
"tests/examples_arguments_syntax",
"tests/examples_methods_syntax",
"tests/test_transformed_data.py",
"altair/vegalite/v?/schema",
]

[tool.ruff.lint]
select = [
# flake8-bugbear
"B",
Expand All @@ -181,15 +174,11 @@ select = [
# flake8-tidy-imports
"TID",
]
ignore = [
# E203, E266, W503 not yet supported by ruff,
# see https://github.com/charliermarsh/ruff/issues/2402
ignore = [
# Whitespace before ':'
# "E203",
"E203",
# Too many leading '#' for block comment
# "E266",
# Line break occurred before a binary operator
# "W503",
"E266",
# Line too long
"E501",
# Relative imports are banned
Expand All @@ -198,14 +187,12 @@ ignore = [
# python>=3.10 only
"B905",
]
exclude = [
".git",
"build",
"__pycache__",
"tests/examples_arguments_syntax",
"tests/examples_methods_syntax",
"altair/vegalite/v?/schema",
]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "lf"

[tool.ruff.mccabe]
max-complexity = 18
Expand Down
5 changes: 3 additions & 2 deletions sphinxext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def get_docstring_and_rest(filename):

if not isinstance(node, ast.Module):
raise TypeError(
"This function only supports modules. "
"You provided {}".format(node.__class__.__name__)
"This function only supports modules. You provided {}".format(
node.__class__.__name__
)
)
try:
# In python 3.7 module knows its docstring.
Expand Down
4 changes: 1 addition & 3 deletions tests/test_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import pandas as pd
table = pd.DataFrame.from_records({})
the_data = table
""".format(
DATA_RECORDS
)
""".format(DATA_RECORDS)
)


Expand Down
2 changes: 1 addition & 1 deletion tools/schemapi/schemapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def from_json(
cls,
json_string: str,
validate: bool = True,
**kwargs: Any
**kwargs: Any,
# Type hints for this method would get rather complicated
# if we want to provide a more specific return type
) -> Any:
Expand Down
16 changes: 13 additions & 3 deletions tools/update_init_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"""
import inspect
import sys
import subprocess
from pathlib import Path
from os.path import abspath, dirname, join
from typing import TypeVar, Type, cast, List, Any, Optional, Iterable, Union

import black

if sys.version_info >= (3, 11):
from typing import Self
else:
Expand Down Expand Up @@ -58,13 +57,24 @@ def update__all__variable() -> None:
+ lines[last_definition_line + 1 :]
)
# Format file content with black
new_file_content = black.format_str("\n".join(new_lines), mode=black.Mode())
new_file_content = ruff_format_str("\n".join(new_lines))

# Write new version of altair/__init__.py
with open(init_path, "w") as f:
f.write(new_file_content)


def ruff_format_str(code: str) -> str:
r = subprocess.run(
# Name of the file does not seem to matter but ruff requires one
["ruff", "format", "--stdin-filename", "placeholder.py"],
input=code.encode(),
check=True,
capture_output=True,
)
return r.stdout.decode()


def _is_relevant_attribute(attr_name: str) -> bool:
attr = getattr(alt, attr_name)
if (
Expand Down