Skip to content

Commit

Permalink
Mypy, Python 3.10 - 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasst committed Jun 27, 2024
1 parent 7acc68c commit dee3fbb
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/setup-python@v2
name: Install Python
with:
python-version: 3.9
python-version: 3.10

- run: |
pip install packaging
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
lint:
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']
name: Lint ${{ matrix.python-version }}
runs-on: 'ubuntu-20.04'
container: python:${{ matrix.python-version }}
Expand All @@ -26,11 +26,16 @@ jobs:
ruff format --check
ruff check --select I
- name: Type check code
run: |
pip install mypy==1.10.1
mypy
# Run tests
test:
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']
# Do not cancel any jobs when a single job fails
fail-fast: false
name: Python ${{ matrix.python-version }}
Expand Down
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ max-branches = 16

[tool.ruff.lint.per-file-ignores]
"tests/test_quotequail.py" = ["E501", "PT009"]

[tool.mypy]
python_version = "3.10"
ignore_missing_imports = true
no_implicit_optional = true
strict_equality = true
follow_imports = "normal"
warn_unreachable = true
show_error_context = true
pretty = true
files = "quotequail"
56 changes: 30 additions & 26 deletions quotequail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__all__ = ["quote", "quote_html", "unwrap", "unwrap_html"]


def quote(text, limit=1000):
def quote(text: str, limit: int = 1000) -> list[tuple[bool, str]]:
"""
Take a plain text message as an argument, return a list of tuples. The
first argument of the tuple denotes whether the text should be expanded by
Expand All @@ -33,7 +33,7 @@ def quote(text, limit=1000):
return [(True, text)]


def quote_html(html, limit=1000):
def quote_html(html: str, limit: int = 1000) -> list[tuple[bool, str]]:
"""
Like quote(), but takes an HTML message as an argument. The limit param
represents the maximum number of lines to traverse until quoting the rest
Expand Down Expand Up @@ -62,7 +62,7 @@ def quote_html(html, limit=1000):
]


def unwrap(text):
def unwrap(text: str) -> dict[str, str] | None:
"""
If the passed text is the text body of a forwarded message, a reply, or
contains quoted text, a dictionary with the following keys is returned:
Expand All @@ -78,31 +78,33 @@ def unwrap(text):
"""
lines = text.split("\n")

result = _internal.unwrap(
unwrap_result = _internal.unwrap(
lines,
_patterns.MAX_WRAP_LINES,
_patterns.MIN_HEADER_LINES,
_patterns.MIN_QUOTED_LINES,
)
if not result:
if not unwrap_result:
return None

typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = (
unwrap_result
)

text_top = lines[slice(*top_range)] if top_range else ""
text = lines[slice(*main_range)] if main_range else ""
text_bottom = lines[slice(*bottom_range)] if bottom_range else ""
text_top_lines = lines[slice(*top_range)] if top_range else []
text_lines = lines[slice(*main_range)] if main_range else []
text_bottom_lines = lines[slice(*bottom_range)] if bottom_range else []

if needs_unindent:
text = _internal.unindent_lines(text)
text_lines = _internal.unindent_lines(text_lines)

result = {
"type": typ,
}

text = "\n".join(text).strip()
text_top = "\n".join(text_top).strip()
text_bottom = "\n".join(text_bottom).strip()
text = "\n".join(text_lines).strip()
text_top = "\n".join(text_top_lines).strip()
text_bottom = "\n".join(text_bottom_lines).strip()

if text:
result["text"] = text
Expand All @@ -117,7 +119,7 @@ def unwrap(text):
return result


def unwrap_html(html):
def unwrap_html(html: str) -> dict[str, str] | None:
"""
If the passed HTML is the HTML body of a forwarded message, a dictionary
with the following keys is returned:
Expand All @@ -137,38 +139,40 @@ def unwrap_html(html):

start_refs, end_refs, lines = _html.get_line_info(tree)

result = _internal.unwrap(lines, 1, _patterns.MIN_HEADER_LINES, 1)
unwrap_result = _internal.unwrap(lines, 1, _patterns.MIN_HEADER_LINES, 1)

if result:
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = result
if unwrap_result:
typ, top_range, hdrs, main_range, bottom_range, needs_unindent = (
unwrap_result
)

result = {
"type": typ,
}

top_range = _html.trim_slice(lines, top_range)
main_range = _html.trim_slice(lines, main_range)
bottom_range = _html.trim_slice(lines, bottom_range)
top_range_slice = _html.trim_slice(lines, top_range)
main_range_slice = _html.trim_slice(lines, main_range)
bottom_range_slice = _html.trim_slice(lines, bottom_range)

if top_range:
if top_range_slice:
top_tree = _html.slice_tree(
tree, start_refs, end_refs, top_range, html_copy=html
tree, start_refs, end_refs, top_range_slice, html_copy=html
)
html_top = _html.render_html_tree(top_tree)
if html_top:
result["html_top"] = html_top

if bottom_range:
if bottom_range_slice:
bottom_tree = _html.slice_tree(
tree, start_refs, end_refs, bottom_range, html_copy=html
tree, start_refs, end_refs, bottom_range_slice, html_copy=html
)
html_bottom = _html.render_html_tree(bottom_tree)
if html_bottom:
result["html_bottom"] = html_bottom

if main_range:
if main_range_slice:
main_tree = _html.slice_tree(
tree, start_refs, end_refs, main_range
tree, start_refs, end_refs, main_range_slice
)
if needs_unindent:
_html.unindent_tree(main_tree)
Expand Down
Loading

0 comments on commit dee3fbb

Please sign in to comment.