From 9677cf9812569a90638663742650b1c398f0f2b8 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Fri, 29 Aug 2025 10:05:18 +0200 Subject: [PATCH] Simplify some statements --- pyproject.toml | 1 + tests/storage/dav/test_caldav.py | 7 +++---- tests/storage/servers/fastmail/__init__.py | 12 +++++++----- tests/storage/test_http_with_singlefile.py | 2 +- tests/unit/sync/test_sync.py | 5 +---- vdirsyncer/http.py | 2 +- vdirsyncer/storage/dav.py | 5 ++--- vdirsyncer/storage/filesystem.py | 9 +++------ 8 files changed, 19 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7cc3af9..3f2f6db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ extend-select = [ "E", "I", "RSE", + "SIM", "TID", "UP", "W", diff --git a/tests/storage/dav/test_caldav.py b/tests/storage/dav/test_caldav.py index ef534a0..37c7576 100644 --- a/tests/storage/dav/test_caldav.py +++ b/tests/storage/dav/test_caldav.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import datetime from textwrap import dedent @@ -30,11 +31,9 @@ class TestCalDAVStorage(DAVStorageTests): async def test_doesnt_accept_vcard(self, item_type, get_storage_args): s = self.storage_class(item_types=(item_type,), **await get_storage_args()) - try: + # Most storages hard-fail, but xandikos doesn't. + with contextlib.suppress(exceptions.Error, aiohttp.ClientResponseError): await s.upload(format_item(VCARD_TEMPLATE)) - except (exceptions.Error, aiohttp.ClientResponseError): - # Most storages hard-fail, but xandikos doesn't. - pass assert not await aiostream.stream.list(s.list()) diff --git a/tests/storage/servers/fastmail/__init__.py b/tests/storage/servers/fastmail/__init__.py index 6b6608f..3b2d771 100644 --- a/tests/storage/servers/fastmail/__init__.py +++ b/tests/storage/servers/fastmail/__init__.py @@ -8,11 +8,13 @@ import pytest class ServerMixin: @pytest.fixture def get_storage_args(self, slow_create_collection, aio_connector, request): - if "item_type" in request.fixturenames: - if request.getfixturevalue("item_type") == "VTODO": - # Fastmail has non-standard support for TODOs - # See https://github.com/pimutils/vdirsyncer/issues/824 - pytest.skip("Fastmail has non-standard VTODO support.") + if ( + "item_type" in request.fixturenames + and request.getfixturevalue("item_type") == "VTODO" + ): + # Fastmail has non-standard support for TODOs + # See https://github.com/pimutils/vdirsyncer/issues/824 + pytest.skip("Fastmail has non-standard VTODO support.") async def inner(collection="test"): args = { diff --git a/tests/storage/test_http_with_singlefile.py b/tests/storage/test_http_with_singlefile.py index 5d29eae..e5ff598 100644 --- a/tests/storage/test_http_with_singlefile.py +++ b/tests/storage/test_http_with_singlefile.py @@ -20,7 +20,7 @@ class CombinedStorage(Storage): storage_name = "http_and_singlefile" def __init__(self, url, path, *, connector, **kwargs): - if kwargs.get("collection", None) is not None: + if kwargs.get("collection") is not None: raise ValueError super().__init__(**kwargs) diff --git a/tests/unit/sync/test_sync.py b/tests/unit/sync/test_sync.py index ea328c1..261780d 100644 --- a/tests/unit/sync/test_sync.py +++ b/tests/unit/sync/test_sync.py @@ -642,10 +642,7 @@ class SyncMachine(RuleBasedStateMachine): errors = [] - if with_error_callback: - error_callback = errors.append - else: - error_callback = None + error_callback = errors.append if with_error_callback else None try: # If one storage is read-only, double-sync because changes don't diff --git a/vdirsyncer/http.py b/vdirsyncer/http.py index d045e17..7b152dd 100644 --- a/vdirsyncer/http.py +++ b/vdirsyncer/http.py @@ -181,7 +181,7 @@ async def request( logger.debug("=" * 20) logger.debug(f"{method} {url}") logger.debug(kwargs.get("headers", {})) - logger.debug(kwargs.get("data", None)) + logger.debug(kwargs.get("data")) logger.debug("Sending request...") assert isinstance(kwargs.get("data", b""), bytes) diff --git a/vdirsyncer/storage/dav.py b/vdirsyncer/storage/dav.py index 4defb47..be25cc7 100644 --- a/vdirsyncer/storage/dav.py +++ b/vdirsyncer/storage/dav.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import datetime import logging import urllib.parse as urlparse @@ -217,10 +218,8 @@ class Discover: async def find_collections(self): rv = None - try: + with contextlib.suppress(aiohttp.ClientResponseError, exceptions.Error): rv = await aiostream.stream.list(self._find_collections_impl("")) - except (aiohttp.ClientResponseError, exceptions.Error): - pass if rv: return rv diff --git a/vdirsyncer/storage/filesystem.py b/vdirsyncer/storage/filesystem.py index bb91641..3ab5113 100644 --- a/vdirsyncer/storage/filesystem.py +++ b/vdirsyncer/storage/filesystem.py @@ -1,5 +1,6 @@ from __future__ import annotations +import contextlib import errno import logging import os @@ -65,9 +66,7 @@ class FilesystemStorage(Storage): def _validate_collection(cls, path): if not os.path.isdir(path): return False - if os.path.basename(path).startswith("."): - return False - return True + return not os.path.basename(path).startswith(".") @classmethod async def create_collection(cls, collection, **kwargs): @@ -205,10 +204,8 @@ class FilesystemStorage(Storage): fpath = os.path.join(self.path, key) if value is None: - try: + with contextlib.suppress(OSError): os.remove(fpath) - except OSError: - pass else: with atomic_write(fpath, mode="wb", overwrite=True) as f: f.write(value.encode(self.encoding))