ruff: enable UP ruleset for detecting python deprecations

This commit is contained in:
Dima Gerasimov 2024-08-27 23:12:57 +01:00 committed by karlicoss
parent d244c7cc4e
commit 118c2d4484
9 changed files with 27 additions and 22 deletions

View file

@ -16,7 +16,7 @@ from typing import Any, Callable, Iterable, List, Optional, Sequence, Type
import click import click
@functools.lru_cache() @functools.lru_cache
def mypy_cmd() -> Optional[Sequence[str]]: def mypy_cmd() -> Optional[Sequence[str]]:
try: try:
# preferably, use mypy from current python env # preferably, use mypy from current python env
@ -43,7 +43,7 @@ def run_mypy(cfg_path: Path) -> Optional[CompletedProcess]:
cmd = mypy_cmd() cmd = mypy_cmd()
if cmd is None: if cmd is None:
return None return None
mres = run([ mres = run([ # noqa: UP022
*cmd, *cmd,
'--namespace-packages', '--namespace-packages',
'--color-output', # not sure if works?? '--color-output', # not sure if works??

View file

@ -148,14 +148,8 @@ def kexists(path: PathIsh, subpath: str) -> bool:
import zipfile import zipfile
if sys.version_info[:2] >= (3, 8):
# meh... zipfile.Path is not available on 3.7 # meh... zipfile.Path is not available on 3.7
zipfile_Path = zipfile.Path zipfile_Path = zipfile.Path
else:
if typing.TYPE_CHECKING:
zipfile_Path = Any
else:
zipfile_Path = object
@total_ordering @total_ordering

View file

@ -707,7 +707,7 @@ def test_wrap_unsortable_with_error_and_warning() -> None:
res = list(select(_mixed_iter_errors(), order_value=lambda o: isinstance(o, datetime))) res = list(select(_mixed_iter_errors(), order_value=lambda o: isinstance(o, datetime)))
assert Counter(type(t).__name__ for t in res) == Counter({"_A": 4, "_B": 2, "Unsortable": 1}) assert Counter(type(t).__name__ for t in res) == Counter({"_A": 4, "_B": 2, "Unsortable": 1})
# compare the returned error wrapped in the Unsortable # compare the returned error wrapped in the Unsortable
returned_error = next((o for o in res if isinstance(o, Unsortable))).obj returned_error = next(o for o in res if isinstance(o, Unsortable)).obj
assert "Unhandled error!" == str(returned_error) assert "Unhandled error!" == str(returned_error)

View file

@ -526,9 +526,8 @@ def test_parse_timedelta_string() -> None:
def test_parse_datetime_float() -> None: def test_parse_datetime_float() -> None:
pnow = parse_datetime_float("now") pnow = parse_datetime_float("now")
sec_diff = abs((pnow - datetime.now().timestamp())) sec_diff = abs(pnow - datetime.now().timestamp())
# should probably never fail? could mock time.time # should probably never fail? could mock time.time
# but there seems to be issues with doing that use C-libraries (as time.time) does # but there seems to be issues with doing that use C-libraries (as time.time) does
# https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking # https://docs.python.org/3/library/unittest.mock-examples.html#partial-mocking

View file

@ -414,7 +414,9 @@ def test_stat_iterable() -> None:
dd = datetime.fromtimestamp(123, tz=timezone.utc) dd = datetime.fromtimestamp(123, tz=timezone.utc)
day = timedelta(days=3) day = timedelta(days=3)
X = NamedTuple('X', [('x', int), ('d', datetime)]) class X(NamedTuple):
x: int
d: datetime
def it(): def it():
yield RuntimeError('oops!') yield RuntimeError('oops!')
@ -452,9 +454,12 @@ def test_guess_datetime() -> None:
dd = fromisoformat('2021-02-01T12:34:56Z') dd = fromisoformat('2021-02-01T12:34:56Z')
# ugh.. https://github.com/python/mypy/issues/7281 class A(NamedTuple):
A = NamedTuple('A', [('x', int)]) x: int
B = NamedTuple('B', [('x', int), ('created', datetime)])
class B(NamedTuple):
x: int
created: datetime
assert _guess_datetime(A(x=4)) is None assert _guess_datetime(A(x=4)) is None
assert _guess_datetime(B(x=4, created=dd)) == dd assert _guess_datetime(B(x=4, created=dd)) == dd

View file

@ -91,8 +91,7 @@ def test_denylist(tmp_path: Path) -> None:
assert "59.40.113.87" not in [i.addr for i in filtered] assert "59.40.113.87" not in [i.addr for i in filtered]
with open(tf, "r") as f: data_json = json.loads(tf.read_text())
data_json = json.loads(f.read())
assert data_json == [ assert data_json == [
{ {

View file

@ -12,8 +12,7 @@ from .discovery_pure import HPIModule, _is_not_module_src, has_stats, ignored
def modules() -> Iterable[HPIModule]: def modules() -> Iterable[HPIModule]:
import my import my
for m in _iter_all_importables(my): yield from _iter_all_importables(my)
yield m
__NOT_HPI_MODULE__ = 'Import this to mark a python file as a helper, not an actual HPI module' __NOT_HPI_MODULE__ = 'Import this to mark a python file as a helper, not an actual HPI module'

View file

@ -218,7 +218,7 @@ def _iter_tz_depends_on() -> str:
day = str(date.today()) day = str(date.today())
hr = datetime.now().hour hr = datetime.now().hour
hr_truncated = hr // mod * mod hr_truncated = hr // mod * mod
return "{}_{}".format(day, hr_truncated) return f"{day}_{hr_truncated}"
# refresh _iter_tzs every few hours -- don't think a better depends_on is possible dynamically # refresh _iter_tzs every few hours -- don't think a better depends_on is possible dynamically

View file

@ -1,7 +1,10 @@
target-version = "py38" # NOTE: inferred from pyproject.toml if present
lint.extend-select = [ lint.extend-select = [
"F", # flakes rules -- default, but extend just in case "F", # flakes rules -- default, but extend just in case
"E", # pycodestyle -- default, but extend just in case "E", # pycodestyle -- default, but extend just in case
"C4", # flake8-comprehensions -- unnecessary list/map/dict calls "C4", # flake8-comprehensions -- unnecessary list/map/dict calls
"UP", # detect deprecated python stdlib stuff
] ]
lint.ignore = [ lint.ignore = [
@ -28,4 +31,10 @@ lint.ignore = [
"F841", # Local variable `count` is assigned to but never used "F841", # Local variable `count` is assigned to but never used
"F401", # imported but unused "F401", # imported but unused
### ###
### TODO should be fine to use these with from __future__ import annotations?
### there was some issue with cachew though... double check this?
"UP006", # use type instead of Type
"UP007", # use X | Y instead of Union
###
] ]