From b4ab51dd589c2914a7e669194ebd2061b74fbdd2 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 30 Oct 2022 16:27:46 -0600 Subject: [PATCH 1/2] refactor: Support for mypy and add to workflow --- .github/workflows/lint.yml | 26 ++++++++++++++++++++++++-- pyproject.toml | 12 ++++++++++++ table2ascii/annotations.py | 9 +++------ table2ascii/preset_style.py | 4 ++-- table2ascii/py.typed | 1 + table2ascii/table_to_ascii.py | 15 ++++++--------- 6 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 table2ascii/py.typed diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 31f7efa..fea7e62 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index e7e32d6..8d7680c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/table2ascii/annotations.py b/table2ascii/annotations.py index cb78bdd..241e787 100644 --- a/table2ascii/annotations.py +++ b/table2ascii/annotations.py @@ -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: @@ -16,8 +15,6 @@ class SupportsStr(Protocol): """An ABC with one abstract method __str__.""" - __slots__ = () - @abstractmethod def __str__(self) -> str: pass diff --git a/table2ascii/preset_style.py b/table2ascii/preset_style.py index 460d7b2..5732219 100644 --- a/table2ascii/preset_style.py +++ b/table2ascii/preset_style.py @@ -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(" ") diff --git a/table2ascii/py.typed b/table2ascii/py.typed new file mode 100644 index 0000000..356bf16 --- /dev/null +++ b/table2ascii/py.typed @@ -0,0 +1 @@ +# Marker file for PEP 561. The table2ascii package uses inline types. \ No newline at end of file diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index 7b8ab81..8a00449 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -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: From a6e19cb655375e93af387063018f39f08c316197 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sun, 30 Oct 2022 16:30:37 -0600 Subject: [PATCH 2/2] Add dependency --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index c4f622c..6cf7af7 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ def requirements(): "pyright==1.1.244", "tox==3.24.5", "pytest==7.1.2", + "mypy==0.982", ], }