Skip to content

Commit 3023870

Browse files
Some code simplifications with the return statement
1 parent 719a14b commit 3023870

File tree

9 files changed

+30
-51
lines changed

9 files changed

+30
-51
lines changed

vdirsyncer/cli/config.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,9 @@ def _process_conflict_resolution_param(
263263
):
264264
if conflict_resolution in (None, "a wins", "b wins"):
265265
return conflict_resolution
266-
elif (
267-
isinstance(conflict_resolution, list)
268-
and len(conflict_resolution) > 1
269-
and conflict_resolution[0] == "command"
270-
):
271-
266+
if isinstance(conflict_resolution, list) and \
267+
len(conflict_resolution) > 1 and \
268+
conflict_resolution[0] == "command":
272269
def resolve(a, b):
273270
a_name = self.config_a["instance_name"]
274271
b_name = self.config_b["instance_name"]
@@ -277,8 +274,7 @@ def resolve(a, b):
277274
return _resolve_conflict_via_command(a, b, command, a_name, b_name)
278275

279276
return resolve
280-
else:
281-
raise ValueError("Invalid value for `conflict_resolution`.")
277+
raise ValueError("Invalid value for `conflict_resolution`.")
282278

283279
# The following parameters are lazily evaluated because evaluating
284280
# self.config_a would expand all `x.fetch` parameters. This is costly and

vdirsyncer/cli/discover.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ async def collections_for_pair(
6767
rv["collections"], pair.config_a, pair.config_b
6868
)
6969
)
70-
elif rv:
70+
if rv:
7171
raise exceptions.UserError(
7272
"Detected change in config file, "
7373
f"please run `vdirsyncer discover {pair.name}`."
7474
)
75-
else:
76-
raise exceptions.UserError(
77-
f"Please run `vdirsyncer discover {pair.name}` "
78-
" before synchronization."
79-
)
75+
raise exceptions.UserError(
76+
f"Please run `vdirsyncer discover {pair.name}` "
77+
" before synchronization."
78+
)
8079

8180
logger.info(f"Discovering collections for pair {pair.name}")
8281

vdirsyncer/cli/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ async def storage_instance_from_config(
299299
create=False,
300300
connector=connector,
301301
)
302-
else:
303-
raise
302+
raise
304303
except Exception:
305304
return handle_storage_init_error(cls, new_config)
306305

vdirsyncer/http.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def prepare_auth(auth, username, password):
4040
if username and password:
4141
if auth == "basic" or auth is None:
4242
return aiohttp.BasicAuth(username, password)
43-
elif auth == "digest":
43+
if auth == "digest":
4444
from requests.auth import HTTPDigestAuth
4545

4646
return HTTPDigestAuth(username, password)
47-
elif auth == "guess":
47+
if auth == "guess":
4848
try:
4949
from requests_toolbelt.auth.guess import GuessAuth
5050
except ImportError:
@@ -62,8 +62,6 @@ def prepare_auth(auth, username, password):
6262
f"You need to specify username and password for {auth} authentication."
6363
)
6464

65-
return None
66-
6765

6866
def prepare_verify(verify, verify_fingerprint):
6967
if isinstance(verify, str):

vdirsyncer/storage/dav.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def _parse_xml(content):
9898

9999
def _merge_xml(items):
100100
if not items:
101-
return None
101+
return
102102
rv = items[0]
103103
for item in items[1:]:
104104
rv.extend(item.iter())
@@ -114,9 +114,7 @@ def _fuzzy_matches_mimetype(strict, weak):
114114
return True
115115

116116
mediatype, subtype = strict.split("/")
117-
if subtype in weak:
118-
return True
119-
return False
117+
return subtype in weak
120118

121119

122120
class Discover:
@@ -237,7 +235,7 @@ def _check_collection_resource_type(self, response):
237235
return True
238236

239237
props = _merge_xml(response.findall("{DAV:}propstat/{DAV:}prop"))
240-
if props is None or not len(props):
238+
if props is None or not props:
241239
dav_logger.debug("Skipping, missing <prop>: %s", response)
242240
return False
243241
if props.find("{DAV:}resourcetype/" + self._resourcetype) is None:
@@ -628,11 +626,10 @@ def _parse_prop_responses(self, root, handled_hrefs=None):
628626
continue
629627

630628
props = response.findall("{DAV:}propstat/{DAV:}prop")
631-
if props is None or not len(props):
629+
if props is None or not props:
632630
dav_logger.debug(f"Skipping {href!r}, properties are missing.")
633631
continue
634-
else:
635-
props = _merge_xml(props)
632+
props = _merge_xml(props)
636633

637634
if props.find("{DAV:}resourcetype/{DAV:}collection") is not None:
638635
dav_logger.debug(f"Skipping {href!r}, is collection.")
@@ -713,7 +710,6 @@ async def get_meta(self, key) -> str | None:
713710
text = normalize_meta_value(getattr(prop, "text", None))
714711
if text:
715712
return text
716-
return None
717713

718714
async def set_meta(self, key, value):
719715
try:

vdirsyncer/storage/filesystem.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ async def get_meta(self, key):
205205
with open(fpath, "rb") as f:
206206
return normalize_meta_value(f.read().decode(self.encoding))
207207
except OSError as e:
208-
if e.errno == errno.ENOENT:
209-
return None
210-
else:
208+
if e.errno != errno.ENOENT:
211209
raise
212210

213211
async def set_meta(self, key, value):

vdirsyncer/sync/status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ def _get_impl(self, ident, side, table):
256256
(ident,),
257257
).fetchone()
258258
if res is None:
259-
return None
259+
return
260260

261261
if res["hash"] is None: # FIXME: Implement as constraint in db
262262
assert res["href"] is None
263263
assert res["etag"] is None
264-
return None
264+
return
265265

266266
res = dict(res)
267267
return ItemMetadata(**res)

vdirsyncer/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
def expand_path(p: str) -> str:
2323
"""Expand $HOME in a path and normalise slashes."""
2424
p = os.path.expanduser(p)
25-
p = os.path.normpath(p)
26-
return p
25+
return os.path.normpath(p)
2726

2827

2928
def split_dict(d: dict, f: Callable):
@@ -174,8 +173,7 @@ def generate_href(ident=None, safe=SAFE_UID_CHARS):
174173
"""
175174
if not ident or not href_safe(ident, safe):
176175
return str(uuid.uuid4())
177-
else:
178-
return ident
176+
return ident
179177

180178

181179
def synchronized(lock=None):

vdirsyncer/vobject.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ def _get_item_type(components, wrappers):
231231

232232
if not i:
233233
return None, None
234-
else:
235-
raise ValueError("Not sure how to join components.")
234+
raise ValueError("Not sure how to join components.")
236235

237236

238237
class _Component:
@@ -303,10 +302,9 @@ def parse(cls, lines, multiple=False):
303302

304303
if multiple:
305304
return rv
306-
elif len(rv) != 1:
305+
if len(rv) != 1:
307306
raise ValueError(f"Found {len(rv)} components, expected one.")
308-
else:
309-
return rv[0]
307+
return rv[0]
310308

311309
def dump_lines(self):
312310
yield f"BEGIN:{self.name}"
@@ -323,8 +321,7 @@ def __delitem__(self, key):
323321
for line in lineiter:
324322
if line.startswith(prefix):
325323
break
326-
else:
327-
new_lines.append(line)
324+
new_lines.append(line)
328325
else:
329326
break
330327

@@ -345,12 +342,10 @@ def __setitem__(self, key, val):
345342
def __contains__(self, obj):
346343
if isinstance(obj, type(self)):
347344
return obj not in self.subcomponents and not any(
348-
obj in x for x in self.subcomponents
349-
)
350-
elif isinstance(obj, str):
345+
obj in x for x in self.subcomponents)
346+
if isinstance(obj, str):
351347
return self.get(obj, None) is not None
352-
else:
353-
raise ValueError(obj)
348+
raise ValueError(obj)
354349

355350
def __getitem__(self, key):
356351
prefix_without_params = f"{key}:"
@@ -360,7 +355,7 @@ def __getitem__(self, key):
360355
if line.startswith(prefix_without_params):
361356
rv = line[len(prefix_without_params) :]
362357
break
363-
elif line.startswith(prefix_with_params):
358+
if line.startswith(prefix_with_params):
364359
rv = line[len(prefix_with_params) :].split(":", 1)[-1]
365360
break
366361
else:

0 commit comments

Comments
 (0)