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

refactor: Support for mypy and add to lint workflow #51

Merged
merged 2 commits into from
Oct 30, 2022
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
26 changes: 24 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ jobs:
cache-dependency-path: |
setup.py

# all extras are installed to test
- name: Install dependencies
run: python -m pip install -e ".[dev]" -e ".[docs]"
run: python -m pip install -r requirements.txt -e ".[dev]" -e ".[docs]"

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

mypy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: |
setup.py

- name: Install dependencies
run: python -m pip install -r requirements.txt -e ".[dev]" -e ".[docs]"

- name: Run mypy
run: mypy .

slotscheck:
runs-on: ubuntu-latest

Expand Down
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ reportUnsupportedDunderAll = true
reportUnusedVariable = true
reportUnnecessaryComparison = true
reportUnnecessaryTypeIgnoreComment = true


[tool.mypy]
python_version = "3.10"
namespace_packages = true


[[tool.mypy.overrides]]
module = [
"setuptools.*",
]
ignore_missing_imports = true
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def requirements():
"pyright==1.1.244",
"tox==3.24.5",
"pytest==7.1.2",
"mypy==0.982",
],
}

Expand Down
9 changes: 3 additions & 6 deletions table2ascii/annotations.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sys
from abc import abstractmethod
from typing import TYPE_CHECKING

try:
# Python 3.8+
if sys.version_info >= (3, 8):
from typing import Protocol, runtime_checkable
except ImportError:
# Python 3.7
else:
from typing_extensions import Protocol, runtime_checkable

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

__slots__ = ()

@abstractmethod
def __str__(self) -> str:
pass
4 changes: 2 additions & 2 deletions table2ascii/preset_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PresetStyle:
ascii_minimalist = TableStyle.from_string(" --- | === --- -- ")
ascii_borderless = TableStyle.from_string(" | - ")
ascii_simple = TableStyle.from_string(" = | = ")
ascii_rounded = TableStyle.from_string("/===\|| |=|=||-|-|\|=/")
ascii_rounded_box = TableStyle.from_string("/===\||||=||||-|||\||/")
ascii_rounded = TableStyle.from_string(r"/===\|| |=|=||-|-|\|=/")
ascii_rounded_box = TableStyle.from_string(r"/===\||||=||||-|||\||/")
markdown = TableStyle.from_string(" ||||-||| ")
plain = TableStyle.from_string(" ")
1 change: 1 addition & 0 deletions table2ascii/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Marker file for PEP 561. The table2ascii package uses inline types.
15 changes: 6 additions & 9 deletions table2ascii/table_to_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,20 @@ def __auto_column_widths(self) -> List[int]:
The minimum number of characters needed for each column
"""

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

column_widths = []
# get the width necessary for each column
for i in range(self.__columns):
# col_widest returns the width of the widest line in the ith cell of a given list
col_widest: Callable[[List[SupportsStr], int], int] = lambda row, i=i: widest_line(
str(row[i])
)
# number of characters in column of i of header, each body row, and footer
header_size = col_widest(self.__header) if self.__header else 0
body_size = map(col_widest, self.__body) if self.__body else [0]
footer_size = col_widest(self.__footer) if self.__footer else 0
header_size = widest_line(self.__header[i]) if self.__header else 0
body_size = max(widest_line(row[i]) for row in self.__body) if self.__body else 0
footer_size = widest_line(self.__footer[i]) if self.__footer else 0
# get the max and add 2 for padding each side with a space
column_widths.append(max(header_size, *body_size, footer_size) + 2)
column_widths.append(max(header_size, body_size, footer_size) + 2)
return column_widths

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