diff --git a/pyproject.toml b/pyproject.toml index a695d81c..e736a380 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,7 +52,7 @@ develop = ["python-gmp[tests]", "prek", "pyperf"] line-length = 79 [tool.ruff.lint] -select = ["E", "F", "I", "PT", "W", "Q", "SIM"] +select = ["E", "F", "I", "PT", "W", "Q", "SIM", "B", "UP", "RUF"] [tool.cibuildwheel] build-frontend = {name="build", args=["--verbose", "-Csetup-args=--vsenv"]} diff --git a/scripts/gitversion.py b/scripts/gitversion.py index 6dac5ce3..15ad28a8 100755 --- a/scripts/gitversion.py +++ b/scripts/gitversion.py @@ -12,7 +12,7 @@ def git_version(): stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__)) - out, err = p.communicate() + out, _ = p.communicate() if p.returncode: raise RuntimeError("Non-zero return code from git-describe: " f"{p.returncode}") diff --git a/tests/test_mpz.py b/tests/test_mpz.py index e0a07744..3a034a01 100644 --- a/tests/test_mpz.py +++ b/tests/test_mpz.py @@ -127,7 +127,7 @@ def test_format_interface(): with pytest.raises(ValueError, match="Unknown format code"): format(mx, "q") with pytest.raises(ValueError, - match="(Unknown format code|Invalid format specifier)"): + match=r"(Unknown format code|Invalid format)"): format(mx, "\x81") with pytest.raises(ValueError, match=(r"Negative zero coercion \(z\) not allowed|" @@ -135,53 +135,57 @@ def test_format_interface(): format(mx, "zd") with pytest.raises(ValueError, match="Precision not allowed"): format(mx, ".10d") - with pytest.raises(ValueError, match="Cannot specify '_' with 'n'."): + with pytest.raises(ValueError, match=r"Cannot specify '_' with 'n'."): format(mx, "_n") - with pytest.raises(ValueError, match="Cannot specify ',' with 'n'."): + with pytest.raises(ValueError, match=r"Cannot specify ',' with 'n'."): format(mx, ",n") - with pytest.raises(ValueError, match="Cannot specify '_' with 'c'."): + with pytest.raises(ValueError, match=r"Cannot specify '_' with 'c'."): format(mx, "_c") with pytest.raises(ValueError, - match=("Invalid format specifier|" + match=(r"Invalid format specifier|" "Invalid conversion specification")): format(mx, "f=10dx") with pytest.raises(ValueError, match=("Format specifier missing precision" - "|no precision given")): + r"|no precision given")): format(mx, ".d") - with pytest.raises(ValueError, match="many decimal digits|width too big"): + with pytest.raises(ValueError, match=r"many decimal digits|width too big"): format(mx, "f=10000000000000000000d") - with pytest.raises(ValueError, match=("many decimal digits|" + with pytest.raises(ValueError, match=(r"many decimal digits|" "precision too big")): format(mx, ".10000000000000000000f") - with pytest.raises(ValueError, match="Cannot specify both ',' and '_'."): + with pytest.raises(ValueError, match=r"Cannot specify both ',' and '_'."): format(mx, ",_d") - with pytest.raises(ValueError, match="Cannot specify both ',' and '_'."): + with pytest.raises(ValueError, match=r"Cannot specify both ',' and '_'."): format(mx, "_,d") - with pytest.raises(ValueError, match="Cannot specify ',' with 'x'."): + with pytest.raises(ValueError, match=r"Cannot specify ',' with 'x'."): format(mx, ",x") with pytest.raises(ValueError, - match=("Cannot specify ',' with|" + match=(r"Cannot specify ',' with|" "Invalid format specifier")): format(mx, ",\xa0") with pytest.raises(ValueError, match="Sign not allowed"): format(mx, "+c") with pytest.raises(ValueError, match=r"Alternate form \(#\) not allowed"): format(mx, "#c") - pytest.raises(OverflowError, lambda: format(mpz(123456789), "c")) - pytest.raises(OverflowError, lambda: format(mpz(10**100), "c")) - pytest.raises(OverflowError, lambda: format(mpz(-1), "c")) - pytest.raises(OverflowError, lambda: format(mpz(1<<32), "c")) + with pytest.raises(OverflowError): + format(mpz(123456789), "c") + with pytest.raises(OverflowError): + format(mpz(10**100), "c") + with pytest.raises(OverflowError): + format(mpz(-1), "c") + with pytest.raises(OverflowError): + format(mpz(1<<32), "c") if sys.version_info >= (3, 14): with pytest.raises(ValueError, - match="Cannot specify both ',' and '_'."): + match=r"Cannot specify both ',' and '_'."): format(mx, ".10,_f") with pytest.raises(ValueError, - match="Cannot specify both ',' and '_'."): + match=r"Cannot specify both ',' and '_'."): format(mx, ".10_,f") - with pytest.raises(ValueError, match="Cannot specify '_' with 'n'."): + with pytest.raises(ValueError, match=r"Cannot specify '_' with 'n'."): format(mx, ".10_n") - with pytest.raises(ValueError, match="Cannot specify ',' with 'n'."): + with pytest.raises(ValueError, match=r"Cannot specify ',' with 'n'."): format(mx, ".10,n") assert format(mx, ".2f") == "123.00" assert format(mx, "") == "123" @@ -366,9 +370,9 @@ def test_richcompare_mixed(x, y): def test_richcompare_errors(): mx = mpz(123) with pytest.raises(TypeError): - mx > 1j + assert mx > 1j with pytest.raises(TypeError): - mx > object() + assert mx > object() def test_hash_caching(): @@ -604,8 +608,10 @@ def test_truediv_mixed(x, y): def test_truediv_errors(): mx = mpz(123) - pytest.raises(TypeError, lambda: mx / object()) - pytest.raises(TypeError, lambda: object() / mx) + with pytest.raises(TypeError): + mx / object() + with pytest.raises(TypeError): + object() / mx @given(bigints(), integers(max_value=100000)) @@ -980,7 +986,7 @@ def test_round_interface(): with pytest.raises(OverflowError): x.__round__((-1<<64) + 1) with pytest.raises(OverflowError): - x.__round__((-1<<62)) + x.__round__(-1<<62) with pytest.raises(OverflowError): x.__round__(-1<<127) diff --git a/tests/utils.py b/tests/utils.py index 1b0935e6..06c6b6fb 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -69,7 +69,7 @@ def python_truediv(a, b): # find integer d satisfying 2**(d - 1) <= a/b < 2**d d = a.bit_length() - b.bit_length() - if d >= 0 and a >= 2**d * b or d < 0 and a * 2**-d >= b: + if (d >= 0 and a >= 2**d * b) or (d < 0 and a * 2**-d >= b): d += 1 # compute 2**-exp * a / b for suitable exp @@ -79,7 +79,7 @@ def python_truediv(a, b): # round-half-to-even: fractional part is r/b, which is > 0.5 iff # 2*r > b, and == 0.5 iff 2*r == b. - if 2*r > b or 2*r == b and q % 2 == 1: + if 2*r > b or (2*r == b and q % 2 == 1): q += 1 result = math.ldexp(q, exp) @@ -118,7 +118,7 @@ def fmt_str(draw, types="bdoxXn"): align = draw(sampled_from(list("<^>="))) res += fill_char + align else: - align = draw(sampled_from([""] + list("<^>="))) + align = draw(sampled_from(["", *list("<^>=")])) if align: skip_0_padding = True res += align @@ -126,7 +126,7 @@ def fmt_str(draw, types="bdoxXn"): skip_0_padding = False # sign character - res += draw(sampled_from([""] + list("-+ "))) + res += draw(sampled_from(["", *list("-+ ")])) # alternate mode res += draw(sampled_from(["", "#"])) @@ -142,7 +142,7 @@ def fmt_str(draw, types="bdoxXn"): res += draw(sampled_from([""]*7 + list(map(str, range(1, 40))))) # grouping character (thousand_separators) - gchar = draw(sampled_from([""] + list(",_"))) + gchar = draw(sampled_from(["", *list(",_")])) if (gchar and not skip_thousand_separators and not (gchar == "," and type in ["b", "o", "x", "X"]) and type != "n"):