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

Apply ruff rules (RUF) #350

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
33 changes: 20 additions & 13 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def _is_file(f):
return isinstance(f, io.IOBase)


__all__ = ["tabulate", "tabulate_formats", "simple_separated_format"]
__all__ = ["simple_separated_format", "tabulate", "tabulate_formats"]
try:
from .version import version as __version__ # noqa: F401
from .version import version as __version__
except ImportError:
pass # running __init__.py as a script, AppVeyor pytests

Expand Down Expand Up @@ -1090,7 +1090,7 @@ def _choose_width_fn(has_invisible, enable_widechars, is_multiline):
else:
line_width_fn = len
if is_multiline:
width_fn = lambda s: _multiline_width(s, line_width_fn) # noqa
width_fn = lambda s: _multiline_width(s, line_width_fn)
else:
width_fn = line_width_fn
return width_fn
Expand Down Expand Up @@ -1130,7 +1130,7 @@ def _align_column_choose_width_fn(has_invisible, enable_widechars, is_multiline)
else:
line_width_fn = len
if is_multiline:
width_fn = lambda s: _align_column_multiline_width(s, line_width_fn) # noqa
width_fn = lambda s: _align_column_multiline_width(s, line_width_fn)
else:
width_fn = line_width_fn
return width_fn
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
tabulate(tbl, headers=hrow) == good_result
True

""" # noqa
"""
if val is None:
return missingval

Expand Down Expand Up @@ -2542,7 +2542,7 @@ def _format_table(

padded_widths = [(w + 2 * pad) for w in colwidths]
if is_multiline:
pad_row = lambda row, _: row # noqa do it later, in _append_multiline_row
pad_row = lambda row, _: row
append_row = partial(_append_multiline_row, pad=pad)
else:
pad_row = _pad_row
Expand Down Expand Up @@ -2685,11 +2685,14 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
if _ansi_codes.search(chunk) is not None:
for group, _, _, _ in _ansi_codes.findall(chunk):
escape_len = len(group)
if group in chunk[last_group: i + total_escape_len + escape_len - 1]:
if (
group
in chunk[last_group : i + total_escape_len + escape_len - 1]
):
total_escape_len += escape_len
found = _ansi_codes.search(chunk[last_group:])
last_group += found.end()
cur_line.append(chunk[: i + total_escape_len - 1])
cur_line.append(chunk[: i + total_escape_len - 1])
reversed_chunks[-1] = chunk[i + total_escape_len - 1 :]

# Otherwise, we have to preserve the long word intact. Only add
Expand Down Expand Up @@ -2780,12 +2783,16 @@ def _wrap_chunks(self, chunks):
self.max_lines is None
or len(lines) + 1 < self.max_lines
or (
not chunks
or self.drop_whitespace
and len(chunks) == 1
and not chunks[0].strip()
(
not chunks
or (
self.drop_whitespace
and len(chunks) == 1
and not chunks[0].strip()
)
)
and cur_len <= width
)
and cur_len <= width
):
# Convert current line back to a string and store it in
# list of all lines (return value).
Expand Down
4 changes: 2 additions & 2 deletions test/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest # noqa
from pytest import skip, raises # noqa
import pytest
from pytest import skip, raises
import warnings


Expand Down
8 changes: 4 additions & 4 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def test_tabulate_formats():
print("tabulate_formats = %r" % supported)
assert type(supported) is list
for fmt in supported:
assert type(fmt) is str # noqa
assert type(fmt) is str


def _check_signature(function, expected_sig):
if not signature:
skip("")
actual_sig = signature(function)
print(f"expected: {expected_sig}\nactual: {str(actual_sig)}\n")
print(f"expected: {expected_sig}\nactual: {actual_sig}\n")

assert len(actual_sig.parameters) == len(expected_sig)

Expand All @@ -36,7 +36,7 @@ def _check_signature(function, expected_sig):

def test_tabulate_signature():
"API: tabulate() type signature is unchanged" ""
assert type(tabulate) is type(lambda: None) # noqa
assert type(tabulate) is type(lambda: None)
expected_sig = [
("tabular_data", _empty),
("headers", ()),
Expand All @@ -62,6 +62,6 @@ def test_tabulate_signature():

def test_simple_separated_format_signature():
"API: simple_separated_format() type signature is unchanged" ""
assert type(simple_separated_format) is type(lambda: None) # noqa
assert type(simple_separated_format) is type(lambda: None)
expected_sig = [("separator", _empty)]
_check_signature(simple_separated_format, expected_sig)
4 changes: 2 additions & 2 deletions test/test_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_wrap_text_to_colwidths():
def test_wrap_text_wide_chars():
"Internal: Wrap wide characters based on column width"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_text_wide_chars is skipped")

Expand Down Expand Up @@ -242,7 +242,7 @@ def test_wrap_text_to_colwidths_single_ansi_colors_full_cell():
def test_wrap_text_to_colwidths_colors_wide_char():
"""Internal: autowrapped text can retain a ANSI colors with wide chars"""
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_text_to_colwidths_colors_wide_char is skipped")

Expand Down
36 changes: 18 additions & 18 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_plain_maxcolwidth_autowraps_with_sep():
def test_plain_maxcolwidth_autowraps_wide_chars():
"Output: maxcolwidth and autowrapping functions with wide characters"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_text_wide_chars is skipped")

Expand Down Expand Up @@ -492,7 +492,7 @@ def test_grid():
def test_grid_wide_characters():
"Output: grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -627,7 +627,7 @@ def test_simple_grid():
def test_simple_grid_wide_characters():
"Output: simple_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_simple_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -762,7 +762,7 @@ def test_rounded_grid():
def test_rounded_grid_wide_characters():
"Output: rounded_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_rounded_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -897,7 +897,7 @@ def test_heavy_grid():
def test_heavy_grid_wide_characters():
"Output: heavy_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_heavy_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1032,7 +1032,7 @@ def test_mixed_grid():
def test_mixed_grid_wide_characters():
"Output: mixed_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_mixed_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1167,7 +1167,7 @@ def test_double_grid():
def test_double_grid_wide_characters():
"Output: double_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_double_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1302,7 +1302,7 @@ def test_fancy_grid():
def test_fancy_grid_wide_characters():
"Output: fancy_grid with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_fancy_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1471,7 +1471,7 @@ def test_colon_grid():
def test_colon_grid_wide_characters():
"Output: colon_grid with wide chars in header"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_colon_grid_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1565,7 +1565,7 @@ def test_outline():
def test_outline_wide_characters():
"Output: outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1617,7 +1617,7 @@ def test_simple_outline():
def test_simple_outline_wide_characters():
"Output: simple_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_simple_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1669,7 +1669,7 @@ def test_rounded_outline():
def test_rounded_outline_wide_characters():
"Output: rounded_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_rounded_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1721,7 +1721,7 @@ def test_heavy_outline():
def test_heavy_outline_wide_characters():
"Output: heavy_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_heavy_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1773,7 +1773,7 @@ def test_mixed_outline():
def test_mixed_outline_wide_characters():
"Output: mixed_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_mixed_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1825,7 +1825,7 @@ def test_double_outline():
def test_double_outline_wide_characters():
"Output: double_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_double_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -1877,7 +1877,7 @@ def test_fancy_outline():
def test_fancy_outline_wide_characters():
"Output: fancy_outline with wide characters in headers"
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_fancy_outline_wide_characters is skipped")
headers = list(_test_table_headers)
Expand Down Expand Up @@ -2536,7 +2536,7 @@ def test_html():
[
"<table>",
"<thead>",
'<tr><th>&lt;strings&gt; </th><th style="text-align: right;"> &lt;&amp;numbers&amp;&gt;</th></tr>', # noqa
'<tr><th>&lt;strings&gt; </th><th style="text-align: right;"> &lt;&amp;numbers&amp;&gt;</th></tr>',
"</thead>",
"<tbody>",
'<tr><td>spam &gt; </td><td style="text-align: right;"> 41.9999</td></tr>',
Expand All @@ -2557,7 +2557,7 @@ def test_unsafehtml():
[
"<table>",
"<thead>",
"<tr><th>strings </th><th>numbers </th></tr>", # noqa
"<tr><th>strings </th><th>numbers </th></tr>",
"</thead>",
"<tbody>",
'<tr><td>spam </td><td><font color="red">41.9999</font></td></tr>',
Expand Down
4 changes: 2 additions & 2 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class textclass(str):
def test_mix_normal_and_wide_characters():
"Regression: wide characters in a grid format (issue #51)"
try:
import wcwidth # noqa
import wcwidth

ru_text = "\u043f\u0440\u0438\u0432\u0435\u0442"
cn_text = "\u4f60\u597d"
Expand All @@ -322,7 +322,7 @@ def test_mix_normal_and_wide_characters():
def test_multiline_with_wide_characters():
"Regression: multiline tables with varying number of wide characters (github issue #28)"
try:
import wcwidth # noqa
import wcwidth

table = [["가나\n가ab", "가나", "가나"]]
result = tabulate(table, tablefmt="fancy_grid")
Expand Down
6 changes: 3 additions & 3 deletions test/test_textwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_wrap_longword_non_wide():
def test_wrap_wide_char_multiword():
"""TextWrapper: wrapping support for wide characters with multiple words"""
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_wide_char is skipped")

Expand All @@ -63,7 +63,7 @@ def test_wrap_wide_char_multiword():
def test_wrap_wide_char_longword():
"""TextWrapper: wrapping wide char word that needs to be broken up"""
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_wide_char_longword is skipped")

Expand All @@ -80,7 +80,7 @@ def test_wrap_wide_char_longword():
def test_wrap_mixed_string():
"""TextWrapper: wrapping string with mix of wide and non-wide chars"""
try:
import wcwidth # noqa
import wcwidth
except ImportError:
skip("test_wrap_wide_char is skipped")

Expand Down
Loading