Skip to content

Commit 2f548e0

Browse files
dilyanpalauzovWhyNotHugo
authored andcommitted
Some code simplifications with the return statement
1 parent 5d34326 commit 2f548e0

File tree

7 files changed

+22
-33
lines changed

7 files changed

+22
-33
lines changed

vdirsyncer/cli/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def _process_conflict_resolution_param(
257257
):
258258
if conflict_resolution in (None, "a wins", "b wins"):
259259
return conflict_resolution
260-
elif (
260+
if (
261261
isinstance(conflict_resolution, list)
262262
and len(conflict_resolution) > 1
263263
and conflict_resolution[0] == "command"
@@ -271,8 +271,7 @@ def resolve(a, b):
271271
return _resolve_conflict_via_command(a, b, command, a_name, b_name)
272272

273273
return resolve
274-
else:
275-
raise ValueError("Invalid value for `conflict_resolution`.")
274+
raise ValueError("Invalid value for `conflict_resolution`.")
276275

277276
# The following parameters are lazily evaluated because evaluating
278277
# 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
@@ -59,21 +59,20 @@ async def collections_for_pair(
5959
cache_key = _get_collections_cache_key(pair)
6060
if from_cache:
6161
rv = load_status(status_path, pair.name, data_type="collections")
62-
if rv.get("cache_key", None) == cache_key:
62+
if rv and rv.get("cache_key", None) == cache_key:
6363
return list(
6464
_expand_collections_cache(
6565
rv["collections"], pair.config_a, pair.config_b
6666
)
6767
)
68-
elif rv:
68+
if rv:
6969
raise exceptions.UserError(
7070
"Detected change in config file, "
7171
f"please run `vdirsyncer discover {pair.name}`."
7272
)
73-
else:
74-
raise exceptions.UserError(
75-
f"Please run `vdirsyncer discover {pair.name}` before synchronization."
76-
)
73+
raise exceptions.UserError(
74+
f"Please run `vdirsyncer discover {pair.name}` before synchronization."
75+
)
7776

7877
logger.info(f"Discovering collections for pair {pair.name}")
7978

vdirsyncer/cli/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ async def storage_instance_from_config(
294294
create=False,
295295
connector=connector,
296296
)
297-
else:
298-
raise
297+
raise
299298
except Exception:
300299
return handle_storage_init_error(cls, new_config)
301300

vdirsyncer/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ def prepare_auth(auth, username, password):
100100
if username and password:
101101
if auth == "basic" or auth is None:
102102
return BasicAuthMethod(username, password)
103-
elif auth == "digest":
103+
if auth == "digest":
104104
return DigestAuthMethod(username, password)
105-
elif auth == "guess":
105+
if auth == "guess":
106106
raise exceptions.UserError(
107107
"'Guess' authentication is not supported in this version of vdirsyncer. \n"
108108
"Please explicitly specify either 'basic' or 'digest' auth instead. \n"

vdirsyncer/storage/dav.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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:
@@ -626,7 +624,7 @@ def _parse_prop_responses(self, root, handled_hrefs=None):
626624
continue
627625

628626
props = response.findall("{DAV:}propstat/{DAV:}prop")
629-
if props is None or not len(props):
627+
if props is None or not props:
630628
dav_logger.debug(f"Skipping {href!r}, properties are missing.")
631629
continue
632630
else:

vdirsyncer/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
def expand_path(p: str) -> str:
2525
"""Expand $HOME in a path and normalise slashes."""
2626
p = os.path.expanduser(p)
27-
p = os.path.normpath(p)
28-
return p
27+
return os.path.normpath(p)
2928

3029

3130
def split_dict(d: dict, f: Callable):
@@ -177,8 +176,7 @@ def generate_href(ident=None, safe=SAFE_UID_CHARS):
177176
"""
178177
if not ident or not href_safe(ident, safe):
179178
return str(uuid.uuid4())
180-
else:
181-
return ident
179+
return ident
182180

183181

184182
def synchronized(lock=None):

vdirsyncer/vobject.py

Lines changed: 7 additions & 11 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

@@ -347,10 +344,9 @@ def __contains__(self, obj):
347344
return obj not in self.subcomponents and not any(
348345
obj in x for x in self.subcomponents
349346
)
350-
elif isinstance(obj, str):
347+
if isinstance(obj, str):
351348
return self.get(obj, None) is not None
352-
else:
353-
raise ValueError(obj)
349+
raise ValueError(obj)
354350

355351
def __getitem__(self, key):
356352
prefix_without_params = f"{key}:"
@@ -360,7 +356,7 @@ def __getitem__(self, key):
360356
if line.startswith(prefix_without_params):
361357
rv = line[len(prefix_without_params) :]
362358
break
363-
elif line.startswith(prefix_with_params):
359+
if line.startswith(prefix_with_params):
364360
rv = line[len(prefix_with_params) :].split(":", 1)[-1]
365361
break
366362
else:

0 commit comments

Comments
 (0)