Skip to content

Commit

Permalink
dev: fix some mypy errors and switch to ruff (#511)
Browse files Browse the repository at this point in the history
* Update `_body.py`

* Remove unused function signatures.

* Update `_export.py`

* Update `_stub.py`

* Remove unused import modules.

* Update `_locations.py`

* Update `_modify_rows.py`

* Update `_tbl_data.py`

* Update `_utils_render_html.py`

* Update `gt.py`

* Update `_utils_render_latex.py`

* Update `_styles.py`

* Clarify that `sides` in `CellStyleBorders` does not support `ColumnExpr`.

* Update `_spanners.py`

* Update `_utils_nanoplots.py`

* Move `_flatten_list()` to `_utils.py`

* Update `_utils.py`

* Use `_flatten_list()` to deduplicate formatted cells in `_migrate_unformatted_to_output()`.

* Update `_formats.py`

* Update `_gt_data.py`

* Refactor `ColumnAlignment` to a `TypeAlias` as the `Enum` definition is unused in the code.
* Propose preserving the signatures of `__new__()` and `__init__()` in `Boxhead`.

* dev: switch to ruff

* dev: configure ruff for pyproject.toml and vscode

* chore: format with ruff, mostly removing newlines after block start

* dev: do not format ipynbs, format unused imports

* chore: run ruff formatter

* chore: run ruff formatter

---------

Co-authored-by: Michael Chow <[email protected]>
  • Loading branch information
jrycw and machow authored Nov 22, 2024
1 parent 538fbf1 commit 549f668
Show file tree
Hide file tree
Showing 38 changed files with 170 additions and 537 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/no_pandas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# %%
from ast import NodeVisitor, Import, ImportFrom, alias, parse
from ast import NodeVisitor, Import, ImportFrom, parse
from pathlib import Path
from os import walk

Expand Down
20 changes: 8 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
exclude: "(.*\\.svg)|(.*\\.qmd)|(.*\\.ambr)|(.*\\.csv)|(.*\\.txt)|(.*\\.json)"
exclude: "(.*\\.svg)|(.*\\.qmd)|(.*\\.ambr)|(.*\\.csv)|(.*\\.txt)|(.*\\.json)|(.*\\.ipynb)"
repos:
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.0
hooks:
- id: flake8
types:
- python
additional_dependencies:
- flake8-pyproject
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -16,7 +16,3 @@ repos:
- id: check-yaml
args: ["--unsafe"]
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 24.2.0
hooks:
- id: black
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
},
"editor.rulers": [100],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
},
"python.testing.pytestArgs": ["tests"],
"python.testing.unittestEnabled": false,
Expand Down
4 changes: 2 additions & 2 deletions great_tables/_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from ._tbl_data import copy_data

if TYPE_CHECKING:
from ._gt_data import Body, Boxhead, RowGroups, Stub
from ._gt_data import Body


def body_reassemble(body: Body, stub_df: Stub, boxhead: Boxhead) -> Body:
def body_reassemble(body: Body) -> Body:
# Note that this used to order the body based on groupings, but now that occurs in the
# renderer itself.
return body.__class__(copy_data(body.body))
2 changes: 0 additions & 2 deletions great_tables/_boxhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ def cols_label(
new_kwargs: dict[str, UnitStr | str | BaseText] = {}

for k, v in new_cases.items():

if isinstance(v, str):

unitstr_v = UnitStr.from_str(v)

if len(unitstr_v.units_str) == 1 and isinstance(unitstr_v.units_str[0], str):
Expand Down
2 changes: 0 additions & 2 deletions great_tables/_data_color/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ def _html_color(colors: list[str], alpha: int | float | None = None) -> list[str
all_hex_colors = all(_is_hex_col(colors=colors))

if not all_hex_colors:

# Translate named colors to hexadecimal values
colors = _color_name_to_hex(colors=colors)

Expand Down Expand Up @@ -509,7 +508,6 @@ def _color_name_to_hex(colors: list[str]) -> list[str]:
hex_colors: list[str] = []

for color in colors:

if _is_hex_col([color])[0]:
hex_colors.append(color)
else:
Expand Down
20 changes: 10 additions & 10 deletions great_tables/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,28 @@
import time
import warnings
import webbrowser

from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
from pathlib import Path
from typing import TYPE_CHECKING, Literal

from typing_extensions import TypeAlias

from ._utils import _try_import
from ._utils_render_latex import _render_as_latex


if TYPE_CHECKING:
# Note that as_raw_html uses methods on the GT class, not just data
from .gt import GT
from ._types import GTSelf

from selenium import webdriver
from IPython.core.interactiveshell import InteractiveShell
from selenium import webdriver

from ._types import GTSelf
from .gt import GT


class PatchedHTTPRequestHandler(SimpleHTTPRequestHandler):
"""Patched handler, which does not log requests to stderr"""

def log_request(self, *args, **kwargs):
pass


class MISSING:
"""Represent a missing argument (where None has a special meaning)."""
Expand All @@ -44,10 +40,13 @@ def _create_temp_file_server(fname: Path) -> HTTPServer:
return server


def _infer_render_target(ipy: InteractiveShell | None | MISSING = MISSING) -> str:
def _infer_render_target(
ipy: InteractiveShell | None | type = MISSING,
) -> Literal["auto", "notebook", "browser"]:
# adapted from py-htmltools
# Note that `ipy` arguments are possible return values of IPython.get_ipython()
# They are manually passed in from unit tests to validate this function.
target: Literal["auto", "notebook", "browser"]
try:
import IPython # pyright: ignore[reportUnknownVariableType]
from IPython.terminal.interactiveshell import TerminalInteractiveShell
Expand Down Expand Up @@ -392,6 +391,7 @@ def _save_screenshot(
driver: webdriver.Chrome, scale: float, path: str, debug: DebugDumpOptions | None
) -> None:
from io import BytesIO

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
Expand Down
Loading

0 comments on commit 549f668

Please sign in to comment.