Skip to content

Commit

Permalink
maintain: modernize and check types
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Dec 2, 2024
1 parent c668baa commit ab2d1ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
20 changes: 14 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
hooks:
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
- id: check-yaml
- id: check-toml
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
rev: 3a6eb0fadf60b3cccfd80bad9dbb6fae7e47b316 # frozen: v1.10.0
hooks:
- id: python-check-blanket-noqa
- repo: https://github.com/timothycrosley/isort
rev: 5.13.2
- repo: https://github.com/PyCQA/isort
rev: c235f5e450b4b84e58d114ed4c589cbf454175a3 # frozen: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.2.0
rev: 1b2427a2b785cc4aac97c19bb4b9a0de063f9547 # frozen: 24.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
rev: e43806be3607110919eff72939fda031776e885a # frozen: 7.1.1
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear>=20.11.1
- flake8-builtins>=1.5.3
- flake8-comprehensions>=3.3.1
- repo: https://github.com/pre-commit/mirrors-mypy
rev: f56614daa94d5cd733d3b7004c5df9caad267b4a # frozen: v1.13.0
hooks:
- id: mypy
args: ["--scripts-are-modules"]
additional_dependencies:
- mdformat
files: ^src/mdformat_tables/.+\.py$
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,22 @@ deps = ["pre-commit"]
commands = [
["pre-commit", "run", "--config", ".pre-commit-test.yaml", { replace = "posargs", default = ["--all-files", "--verbose", "--show-diff-on-failure"], extend = true }],
]


[tool.mypy]
show_error_codes = true
warn_unreachable = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
# Disabling incremental mode is required for `warn_unused_configs = true` to work
incremental = false
disallow_untyped_defs = true
check_untyped_defs = true
strict_equality = true
implicit_reexport = false
no_implicit_optional = true

[[tool.mypy.overrides]]
module = "wcwidth.*"
ignore_missing_imports = true
16 changes: 8 additions & 8 deletions src/mdformat_tables/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import argparse
from typing import Iterable, List, Mapping, Sequence, Union
from collections.abc import Iterable, Mapping, Sequence

from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode
Expand Down Expand Up @@ -40,8 +40,8 @@ def _center(text: str, width: int) -> str:

def _to_string(
rows: Sequence[Sequence[str]], align: Sequence[Sequence[str]], widths: Sequence[int]
) -> List[str]:
def join_row(items: Union[Iterable[str], Sequence[str]]) -> str:
) -> list[str]:
def join_row(items: Iterable[str]) -> str:
return "| " + " | ".join(items) + " |"

def format_delimiter_cell(index: int, align: str) -> str:
Expand All @@ -60,13 +60,13 @@ def format_delimiter_cell(index: int, align: str) -> str:
delimiter = join_row(
(format_delimiter_cell(i, al) for i, al in enumerate(align[0]))
)
rows = [
joined_rows = (
join_row(
pad[_al](text, widths[i]) for i, (text, _al) in enumerate(zip(row, aligns))
)
for row, aligns in zip(rows[1:], align[1:])
]
return [header, delimiter, *rows]
)
return [header, delimiter, *joined_rows]


def _render_table(node: RenderTreeNode, context: RenderContext) -> str:
Expand All @@ -80,8 +80,8 @@ def _render_table(node: RenderTreeNode, context: RenderContext) -> str:
compact_tables_from_api = context.options["mdformat"].get("compact_tables")
compact_tables = compact_tables_from_cli_or_toml or compact_tables_from_api
# gather rendered cell content into row * column array
rows: List[List[str]] = []
align: List[List[str]] = []
rows: list[list[str]] = []
align: list[list[str]] = []
for descendant in node.walk(include_self=False):
if descendant.type == "tr":
rows.append([])
Expand Down

0 comments on commit ab2d1ec

Please sign in to comment.