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
@functools.lru_cache()
@functools.lru_cache
def mypy_cmd() -> Optional[Sequence[str]]:
try:
# preferably, use mypy from current python env
@ -43,7 +43,7 @@ def run_mypy(cfg_path: Path) -> Optional[CompletedProcess]:
cmd = mypy_cmd()
if cmd is None:
return None
mres = run([
mres = run([ # noqa: UP022
*cmd,
'--namespace-packages',
'--color-output', # not sure if works??

View file

@ -148,14 +148,8 @@ def kexists(path: PathIsh, subpath: str) -> bool:
import zipfile
if sys.version_info[:2] >= (3, 8):
# meh... zipfile.Path is not available on 3.7
zipfile_Path = zipfile.Path
else:
if typing.TYPE_CHECKING:
zipfile_Path = Any
else:
zipfile_Path = object
# meh... zipfile.Path is not available on 3.7
zipfile_Path = zipfile.Path
@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)))
assert Counter(type(t).__name__ for t in res) == Counter({"_A": 4, "_B": 2, "Unsortable": 1})
# 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)

View file

@ -526,9 +526,8 @@ def test_parse_timedelta_string() -> None:
def test_parse_datetime_float() -> None:
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
# 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

View file

@ -414,7 +414,9 @@ def test_stat_iterable() -> None:
dd = datetime.fromtimestamp(123, tz=timezone.utc)
day = timedelta(days=3)
X = NamedTuple('X', [('x', int), ('d', datetime)])
class X(NamedTuple):
x: int
d: datetime
def it():
yield RuntimeError('oops!')
@ -452,9 +454,12 @@ def test_guess_datetime() -> None:
dd = fromisoformat('2021-02-01T12:34:56Z')
# ugh.. https://github.com/python/mypy/issues/7281
A = NamedTuple('A', [('x', int)])
B = NamedTuple('B', [('x', int), ('created', datetime)])
class A(NamedTuple):
x: int
class B(NamedTuple):
x: int
created: datetime
assert _guess_datetime(A(x=4)) is None
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]
with open(tf, "r") as f:
data_json = json.loads(f.read())
data_json = json.loads(tf.read_text())
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]:
import my
for m in _iter_all_importables(my):
yield m
yield from _iter_all_importables(my)
__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())
hr = datetime.now().hour
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

View file

@ -1,7 +1,10 @@
target-version = "py38" # NOTE: inferred from pyproject.toml if present
lint.extend-select = [
"F", # flakes rules -- default, but extend just in case
"E", # pycodestyle -- default, but extend just in case
"C4", # flake8-comprehensions -- unnecessary list/map/dict calls
"UP", # detect deprecated python stdlib stuff
]
lint.ignore = [
@ -28,4 +31,10 @@ lint.ignore = [
"F841", # Local variable `count` is assigned to but never used
"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
###
]