Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

python: Use isinstance with a tuple for testing multiple types.

Anders Kaseorg8667e632c83f
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index be12c3677da..636d3d8ac4e 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -179,8 +179,7 @@ def catch_stripe_errors(func: CallableT) -> CallableT:
if isinstance(e, stripe.error.CardError):
# TODO: Look into i18n for this
raise StripeCardError('card error', err.get('message'))
- if isinstance(e, stripe.error.RateLimitError) or \
- isinstance(e, stripe.error.APIConnectionError): # nocoverage TODO
+ if isinstance(e, (stripe.error.RateLimitError, stripe.error.APIConnectionError)): # nocoverage TODO
raise StripeConnectionError(
'stripe connection error',
_("Something went wrong. Please wait a few seconds and try again."))
diff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.py
index 3e2556816c4..d33fd218484 100644
--- a/zerver/lib/test_runner.py
+++ b/zerver/lib/test_runner.py
@@ -102,7 +102,7 @@ def enforce_timely_test_completion(test_method: Callable[..., ReturnT], test_nam
else:
max_delay = 0.4 # seconds
- assert isinstance(result, TextTestResult) or isinstance(result, RemoteTestResult)
+ assert isinstance(result, (TextTestResult, RemoteTestResult))
if delay > max_delay:
msg = f'** Test is TOO slow: {test_name} ({delay:.3f} s)\n'
diff --git a/zerver/lib/type_debug.py b/zerver/lib/type_debug.py
index fed24fc977c..a47219fd3f0 100644
--- a/zerver/lib/type_debug.py
+++ b/zerver/lib/type_debug.py
@@ -43,7 +43,7 @@ def get_sequence_type_str(x: Sequence[Any]) -> str:
else:
return f'{container_type}([{elem_type}, ...])'
-expansion_blacklist = [str, bytes]
+expansion_blacklist = (str, bytes)
def get_type_str(x: Any) -> str:
if x is None:
@@ -58,7 +58,7 @@ def get_type_str(x: Any) -> str:
return '(' + ', '.join(types) + ')'
elif isinstance(x, Mapping):
return get_mapping_type_str(x)
- elif isinstance(x, Sequence) and not any(isinstance(x, t) for t in expansion_blacklist):
+ elif isinstance(x, Sequence) and not isinstance(x, expansion_blacklist):
return get_sequence_type_str(x)
else:
return type(x).__name__
diff --git a/zerver/lib/validator.py b/zerver/lib/validator.py
index 2e2bdb2549f..4d5486fcb7f 100644
--- a/zerver/lib/validator.py
+++ b/zerver/lib/validator.py
@@ -397,7 +397,7 @@ def check_string_or_int_list(var_name: str, val: object) -> Union[str, List[int]
return check_list(check_int)(var_name, val)
def check_string_or_int(var_name: str, val: object) -> Union[str, int]:
- if isinstance(val, str) or isinstance(val, int):
+ if isinstance(val, (str, int)):
return val
raise ValidationError(_('{var_name} is not a string or integer').format(var_name=var_name))