Skip to content

Commit

Permalink
Move black, mypy + ise pyupgrade to be baselined on >= python 3.11 (#…
Browse files Browse the repository at this point in the history
…1734)

- Run pre-commit
  - `pre-commit run -a`
- Apply all suggested formatting + upgrade recommendations

Test:
- See unit tests stay passing via local tox run + CI
  • Loading branch information
cooperlees authored May 26, 2024
1 parent 0a36e6a commit 2ebfd6e
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ repos:
rev: 24.4.2
hooks:
- id: black
args: [--target-version, py38, --preview]
args: [--target-version, py311, --preview]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py310-plus]
args: [--py311-plus]

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Initial support for python 3.12 (PR #1728)
- Move Docker images to 3.12 (PR #1733)
- Removing swift builds due to lack or assistance - Happy to bring back if you're willing to help maintian
- Move black, mypy + pyupgrade to >= 3.11 codebase (PR #1734)

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.10
python_version = 3.11
check_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_defs = True
Expand Down
2 changes: 1 addition & 1 deletion src/bandersnatch/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ async def rpc(self, method_name: str, serial: int = 0) -> Any:
if serial:
return await method(serial)
return await method()
except asyncio.TimeoutError as te:
except TimeoutError as te:
logger.error(f"Call to {method_name} @ {self.xmlrpc_url} timed out: {te}")

async def all_packages(self) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion src/bandersnatch/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def update_metadata(self, master: "Master", attempts: int = 3) -> None:
except PackageNotFound as e:
logger.info(str(e))
raise
except (StalePage, asyncio.TimeoutError) as e:
except (StalePage, TimeoutError) as e:
error_name, error_class = (
("Stale serial", StaleMetadata)
if isinstance(e, StalePage)
Expand Down
8 changes: 1 addition & 7 deletions src/bandersnatch/simple.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import html
import json
import logging
import sys
from enum import Enum, auto
from enum import Enum, StrEnum, auto
from pathlib import Path
from typing import TYPE_CHECKING, Any, NamedTuple
from urllib.parse import urlparse
Expand All @@ -12,11 +11,6 @@
if TYPE_CHECKING:
from .storage import Storage

if sys.version_info >= (3, 11):
from enum import StrEnum
else:
from .utils import StrEnum


class SimpleFormats(NamedTuple):
html: str
Expand Down
4 changes: 2 additions & 2 deletions src/bandersnatch/tests/plugins/test_storage_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def iter_dir(

def get_swift_object_date(date: datetime.datetime) -> str:
return (
date.astimezone(datetime.timezone.utc)
date.astimezone(datetime.UTC)
.strftime("%a, %d %b %Y %H:%M:%S %Z")
.replace("UTC", "GMT")
)


def get_swift_date(date: datetime.datetime) -> str:
return date.astimezone(datetime.timezone.utc).isoformat()
return date.astimezone(datetime.UTC).isoformat()


class MockConnection:
Expand Down
7 changes: 4 additions & 3 deletions src/bandersnatch/tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,10 @@ async def test_cleanup_non_pep_503_paths(mirror: BandersnatchMirror) -> None:
touch_files([mirror.webdir / "simple" / raw_package_name / "index.html"])

mirror.cleanup = True
with mock.patch("bandersnatch.mirror.Path.unlink") as mocked_unlink, mock.patch(
"bandersnatch.mirror.Path.rmdir"
) as mocked_rmdir:
with (
mock.patch("bandersnatch.mirror.Path.unlink") as mocked_unlink,
mock.patch("bandersnatch.mirror.Path.rmdir") as mocked_rmdir,
):
await mirror.cleanup_non_pep_503_paths(package)
assert mocked_unlink.call_count == 1 # number you expect
assert mocked_rmdir.call_count == 1 # Or number you expect here
Expand Down
4 changes: 1 addition & 3 deletions src/bandersnatch_storage_plugins/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ def get_file_size(self, path: PATH_TYPES) -> int:
def get_upload_time(self, path: PATH_TYPES) -> datetime.datetime:
if not isinstance(path, pathlib.Path):
path = pathlib.Path(path)
return datetime.datetime.fromtimestamp(
path.stat().st_mtime, datetime.timezone.utc
)
return datetime.datetime.fromtimestamp(path.stat().st_mtime, datetime.UTC)

def set_upload_time(self, path: PATH_TYPES, time: datetime.datetime) -> None:
"""Set the upload time of a given **path**"""
Expand Down
2 changes: 1 addition & 1 deletion src/bandersnatch_storage_plugins/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def get_upload_time(self, path: PATH_TYPES) -> datetime.datetime:
ts = s3object.metadata.get(self.UPLOAD_TIME_METADATA_KEY, 0)
if not isinstance(ts, int):
ts = int(float(ts))
return datetime.datetime.fromtimestamp(ts, datetime.timezone.utc)
return datetime.datetime.fromtimestamp(ts, datetime.UTC)

def set_upload_time(self, path: PATH_TYPES, time: datetime.datetime) -> None:
if not isinstance(path, self.PATH_BACKEND):
Expand Down
2 changes: 1 addition & 1 deletion src/bandersnatch_storage_plugins/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def get_upload_time(self, path: PATH_TYPES) -> datetime.datetime:
str(path),
)
ts = int(headers.get("x-object-meta-upload", "0"))
return datetime.datetime.fromtimestamp(ts, datetime.timezone.utc)
return datetime.datetime.fromtimestamp(ts, datetime.UTC)

def set_upload_time(self, path: PATH_TYPES, time: datetime.datetime) -> None:
"""Set the upload time of a given **path**"""
Expand Down

0 comments on commit 2ebfd6e

Please sign in to comment.