Skip to content

Commit

Permalink
Enable some additional bugbear rules
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyNotHugo committed Aug 14, 2023
1 parent 7dd3a94 commit be79e8e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions payments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def get_payment_model():
"""
try:
app_label, model_name = settings.PAYMENT_MODEL.split(".")
except (ValueError, AttributeError):
except (ValueError, AttributeError) as e:
raise ImproperlyConfigured(
"PAYMENT_MODEL must be of the form " '"app_label.model_name"'
)
) from e
payment_model = apps.get_model(app_label, model_name)
if payment_model is None:
msg = (
Expand Down
2 changes: 1 addition & 1 deletion payments/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CreditCardPaymentForm(PaymentForm):
)

def __init__(self, *args, **kwargs):
super().__init__(hidden_inputs=False, *args, **kwargs)
super().__init__(*args, hidden_inputs=False, **kwargs)
if hasattr(self, "VALID_TYPES"):
self.fields["number"].valid_types = self.VALID_TYPES

Expand Down
2 changes: 1 addition & 1 deletion payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __getattr__(self, item):
try:
return data[item]
except KeyError as e:
raise AttributeError(*e.args)
raise AttributeError(*e.args) from e

def __setattr__(self, key, value):
if key == "_payment":
Expand Down
4 changes: 2 additions & 2 deletions payments/sofort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def get_form(self, payment, data=None):
if response.status_code == 200:
try:
raise RedirectNeeded(doc["new_transaction"]["payment_url"])
except KeyError:
except KeyError as e:
raise PaymentError(
"Error in {}: {}".format(
doc["errors"]["error"]["field"],
doc["errors"]["error"]["message"],
)
)
) from e

def process_data(self, payment, request):
if "trans" not in request.GET:
Expand Down
4 changes: 2 additions & 2 deletions payments/stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def capture(self, payment, amount=None):
charge = stripe.Charge.retrieve(payment.transaction_id)
try:
charge.capture(amount=amount)
except stripe.InvalidRequestError:
except stripe.InvalidRequestError as e:
payment.change_status(PaymentStatus.REFUNDED)
raise PaymentError("Payment already refunded")
raise PaymentError("Payment already refunded") from e
payment.attrs.capture = json.dumps(charge)
return Decimal(amount) / 100

Expand Down
2 changes: 1 addition & 1 deletion payments/stripe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def save(self):

class ModalPaymentForm(StripeFormMixin, BasePaymentForm):
def __init__(self, *args, **kwargs):
super(StripeFormMixin, self).__init__(hidden_inputs=False, *args, **kwargs)
super(StripeFormMixin, self).__init__(*args, hidden_inputs=False, **kwargs)
widget = StripeCheckoutWidget(provider=self.provider, payment=self.payment)
self.fields["stripeToken"] = forms.CharField(widget=widget)
if self.is_bound and not self.data.get("stripeToken"):
Expand Down
18 changes: 9 additions & 9 deletions payments/stripe/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_form(self, payment, data=None):
session = self.create_session(payment)
except PaymentError as pe:
payment.change_status(PaymentStatus.ERROR, str(pe))
raise PaymentError(pe)
raise pe
else:
payment.attrs.session = session
payment.transaction_id = session.get("id", None)
Expand Down Expand Up @@ -140,7 +140,7 @@ def create_session(self, payment):
return stripe.checkout.Session.create(**session_data)
except stripe.error.StripeError as e:
# Payment has been declined by Stripe, check Stripe Dashboard
raise PaymentError(e)
raise PaymentError(e) from e
else:
raise PaymentError("This payment has already been processed.")

Expand All @@ -149,8 +149,8 @@ def refund(self, payment, amount=None) -> int:
to_refund = amount or payment.total
try:
payment_intent = payment.attrs.session["payment_intent"]
except Exception:
raise PaymentError("Can't Refund, payment_intent does not exist")
except Exception as e:
raise PaymentError("Can't Refund, payment_intent does not exist") from e

stripe.api_key = self.api_key
try:
Expand All @@ -160,7 +160,7 @@ def refund(self, payment, amount=None) -> int:
reason="requested_by_customer",
)
except stripe.error.StripeError as e:
raise PaymentError(e)
raise PaymentError(e) from e
else:
payment.attrs.refund = json.dumps(refund)
payment.save()
Expand Down Expand Up @@ -231,11 +231,11 @@ def get_token_from_request(self, payment, request) -> str:

try:
return event["data"]["object"]["client_reference_id"]
except Exception:
except Exception as e:
raise PaymentError(
code=400,
message="client_reference_id is not present, check Stripe Dashboard.",
)
) from e

def process_data(self, payment, request):
"""Processes the event sent by stripe.
Expand All @@ -246,10 +246,10 @@ def process_data(self, payment, request):
if event.get("type") in stripe_enabled_events:
try:
session_info = event["data"]["object"]
except Exception:
except Exception as e:
raise PaymentError(
code=400, message="session not present, check Stripe Dashboard"
)
) from e

if session_info["status"] == "expired":
# Expired Order
Expand Down
8 changes: 4 additions & 4 deletions payments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def process_data(request, token, provider=None):
if not provider:
try:
provider = provider_factory(payment.variant, payment)
except ValueError:
raise Http404("No such payment")
except ValueError as e:
raise Http404("No such payment") from e
return provider.process_data(payment, request)


Expand All @@ -36,8 +36,8 @@ def process_data(request, token, provider=None):
def static_callback(request, variant):
try:
provider = provider_factory(variant)
except ValueError:
raise Http404("No such provider")
except ValueError as e:
raise Http404("No such provider") from e

token = provider.get_token_from_request(request=request, payment=None)
if not token:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ select = [
"C4",
]
target-version = "py38"
ignore = ["E501", "B904", "B026"]
ignore = ["E501"]

[tool.ruff.isort]
force-single-line = true

0 comments on commit be79e8e

Please sign in to comment.