Skip to content

Commit e6890b5

Browse files
authored
refactor: Support for mypy and add to lint workflow (#51)
1 parent a102a28 commit e6890b5

File tree

7 files changed

+49
-19
lines changed

7 files changed

+49
-19
lines changed

.github/workflows/lint.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ jobs:
4848
cache-dependency-path: |
4949
setup.py
5050
51-
# all extras are installed to test
5251
- name: Install dependencies
53-
run: python -m pip install -e ".[dev]" -e ".[docs]"
52+
run: python -m pip install -r requirements.txt -e ".[dev]" -e ".[docs]"
5453

5554
- name: Set up pyright
5655
run: echo "PYRIGHT_VERSION=$(python -c 'import pyright; print(pyright.__pyright_version__)')" >> $GITHUB_ENV
@@ -77,6 +76,29 @@ jobs:
7776
no-comments: true
7877
warnings: true
7978

79+
mypy:
80+
runs-on: ubuntu-latest
81+
strategy:
82+
matrix:
83+
python-version: ["3.10"]
84+
steps:
85+
- name: Checkout code
86+
uses: actions/checkout@v3
87+
88+
- name: Set up python ${{ matrix.python-version }}
89+
uses: actions/setup-python@v4
90+
with:
91+
python-version: ${{ matrix.python-version }}
92+
cache: pip
93+
cache-dependency-path: |
94+
setup.py
95+
96+
- name: Install dependencies
97+
run: python -m pip install -r requirements.txt -e ".[dev]" -e ".[docs]"
98+
99+
- name: Run mypy
100+
run: mypy .
101+
80102
slotscheck:
81103
runs-on: ubuntu-latest
82104

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,15 @@ reportUnsupportedDunderAll = true
6767
reportUnusedVariable = true
6868
reportUnnecessaryComparison = true
6969
reportUnnecessaryTypeIgnoreComment = true
70+
71+
72+
[tool.mypy]
73+
python_version = "3.10"
74+
namespace_packages = true
75+
76+
77+
[[tool.mypy.overrides]]
78+
module = [
79+
"setuptools.*",
80+
]
81+
ignore_missing_imports = true

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def requirements():
4848
"pyright==1.1.244",
4949
"tox==3.24.5",
5050
"pytest==7.1.2",
51+
"mypy==0.982",
5152
],
5253
}
5354

table2ascii/annotations.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import sys
12
from abc import abstractmethod
23
from typing import TYPE_CHECKING
34

4-
try:
5-
# Python 3.8+
5+
if sys.version_info >= (3, 8):
66
from typing import Protocol, runtime_checkable
7-
except ImportError:
8-
# Python 3.7
7+
else:
98
from typing_extensions import Protocol, runtime_checkable
109

1110
if TYPE_CHECKING:
@@ -16,8 +15,6 @@
1615
class SupportsStr(Protocol):
1716
"""An ABC with one abstract method __str__."""
1817

19-
__slots__ = ()
20-
2118
@abstractmethod
2219
def __str__(self) -> str:
2320
pass

table2ascii/preset_style.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PresetStyle:
4343
ascii_minimalist = TableStyle.from_string(" --- | === --- -- ")
4444
ascii_borderless = TableStyle.from_string(" | - ")
4545
ascii_simple = TableStyle.from_string(" = | = ")
46-
ascii_rounded = TableStyle.from_string("/===\|| |=|=||-|-|\|=/")
47-
ascii_rounded_box = TableStyle.from_string("/===\||||=||||-|||\||/")
46+
ascii_rounded = TableStyle.from_string(r"/===\|| |=|=||-|-|\|=/")
47+
ascii_rounded_box = TableStyle.from_string(r"/===\||||=||||-|||\||/")
4848
markdown = TableStyle.from_string(" ||||-||| ")
4949
plain = TableStyle.from_string(" ")

table2ascii/py.typed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Marker file for PEP 561. The table2ascii package uses inline types.

table2ascii/table_to_ascii.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,20 @@ def __auto_column_widths(self) -> List[int]:
9696
The minimum number of characters needed for each column
9797
"""
9898

99-
def widest_line(text: str) -> int:
99+
def widest_line(value: SupportsStr) -> int:
100100
"""Returns the width of the longest line in a multi-line string"""
101+
text = str(value)
101102
return max(len(line) for line in text.splitlines()) if len(text) else 0
102103

103104
column_widths = []
104105
# get the width necessary for each column
105106
for i in range(self.__columns):
106-
# col_widest returns the width of the widest line in the ith cell of a given list
107-
col_widest: Callable[[List[SupportsStr], int], int] = lambda row, i=i: widest_line(
108-
str(row[i])
109-
)
110107
# number of characters in column of i of header, each body row, and footer
111-
header_size = col_widest(self.__header) if self.__header else 0
112-
body_size = map(col_widest, self.__body) if self.__body else [0]
113-
footer_size = col_widest(self.__footer) if self.__footer else 0
108+
header_size = widest_line(self.__header[i]) if self.__header else 0
109+
body_size = max(widest_line(row[i]) for row in self.__body) if self.__body else 0
110+
footer_size = widest_line(self.__footer[i]) if self.__footer else 0
114111
# get the max and add 2 for padding each side with a space
115-
column_widths.append(max(header_size, *body_size, footer_size) + 2)
112+
column_widths.append(max(header_size, body_size, footer_size) + 2)
116113
return column_widths
117114

118115
def __pad(self, cell_value: SupportsStr, width: int, alignment: Alignment) -> str:

0 commit comments

Comments
 (0)