Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Dec 1, 2021

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from MattFisher December 1, 2021 02:15
Comment on lines -63 to +64
else:
redis_conn.delete(redis_key)
return "RESULT: {}".format(result)
redis_conn.delete(redis_key)
return "RESULT: {}".format(result)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function wait refactored with the following changes:

if callable(self.bytes):
payload = self.bytes()
else:
payload = self.bytes
payload = self.bytes() if callable(self.bytes) else self.bytes
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Attachment.to_envelope_item refactored with the following changes:

Comment on lines -217 to +223
for errcls in self.options["ignore_errors"]:
# String types are matched against the type name in the
# exception only
if isinstance(errcls, string_types):
if errcls == full_name or errcls == type_name:
return True
else:
if issubclass(exc_info[0], errcls):
return True

return False
return any(
isinstance(errcls, string_types)
and errcls in [full_name, type_name]
or not isinstance(errcls, string_types)
and issubclass(exc_info[0], errcls)
for errcls in self.options["ignore_errors"]
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _Client._is_ignored_error refactored with the following changes:

This removes the following comments ( why? ):

# String types are matched against the type name in the
# exception only

Comment on lines -252 to +248
if self._is_ignored_error(event, hint):
return False

return True
return not self._is_ignored_error(event, hint)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _Client._should_capture refactored with the following changes:

if items is None:
items = []
else:
items = list(items)
items = [] if items is None else list(items)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Envelope.__init__ refactored with the following changes:

Comment on lines -756 to +742
for item in set or ():
if item == name or name.startswith(item + "."):
return True
return False
return any(item == name or name.startswith(item + ".") for item in set or ())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_lines_from_file.get_source_context.filename_for_module.serialize_frame.handle_in_app_impl._module_in_set refactored with the following changes:

  • Use any() instead of for loop (use-any)

Comment on lines -795 to +778
version_tuple = tuple([int(part) for part in gevent.__version__.split(".")[:2]])
version_tuple = tuple(int(part) for part in gevent.__version__.split(".")[:2])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_lines_from_file.get_source_context.filename_for_module.serialize_frame.handle_in_app_impl._is_contextvars_broken refactored with the following changes:

This removes the following comments ( why? ):

# Gevent 20.9.0+
# Gevent 20.5.0+ or Python < 3.7

Comment on lines -856 to +845
# aiocontextvars is a PyPI package that ensures that the contextvars
# backport (also a PyPI package) works with asyncio under Python 3.6
#
# Import it if available.
if sys.version_info < (3, 7):
# `aiocontextvars` is absolutely required for functional
# contextvars on Python 3.6.
try:
try:
if sys.version_info < (3, 7):
from aiocontextvars import ContextVar # noqa

return True, ContextVar
except ImportError:
pass
else:
# On Python 3.7 contextvars are functional.
try:
else:
from contextvars import ContextVar

return True, ContextVar
except ImportError:
pass

return True, ContextVar
except ImportError:
pass
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_lines_from_file.get_source_context.filename_for_module.serialize_frame.handle_in_app_impl._get_contextvars refactored with the following changes:

This removes the following comments ( why? ):

# On Python 3.7 contextvars are functional.
# backport (also a PyPI package) works with asyncio under Python 3.6
# aiocontextvars is a PyPI package that ensures that the contextvars
#
# Import it if available.

Comment on lines -966 to +934
integer_configured_timeout = integer_configured_timeout + 1
integer_configured_timeout += 1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_lines_from_file.get_source_context.filename_for_module.serialize_frame.handle_in_app_impl.TimeoutThread.run refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

Comment on lines -95 to +99
integrations = dict(
(integration.identifier, integration) for integration in integrations or ()
)
integrations = {
integration.identifier: integration
for integration in integrations or ()
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _generate_default_integrations_iterator.iter_default_integrations.setup_integrations refactored with the following changes:

if _looks_like_asgi3(app):
self.__call__ = self._run_asgi3 # type: Callable[..., Any]
else:
self.__call__ = self._run_asgi2
self.__call__ = self._run_asgi3 if _looks_like_asgi3(app) else self._run_asgi2
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SentryAsgiMiddleware.__init__ refactored with the following changes:

This removes the following comments ( why? ):

# type: Callable[..., Any]

Comment on lines -255 to +252
if key in headers:
headers[key] = headers[key] + ", " + value
else:
headers[key] = value
headers[key] = headers[key] + ", " + value if key in headers else value
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SentryAsgiMiddleware._get_headers refactored with the following changes:

Comment on lines -363 to +366
else:
if aws_event.get("body", None):
# Unfortunately couldn't find a way to get structured body from AWS
# event. Meaning every body is unstructured to us.
request["data"] = AnnotatedValue("", {"rem": [["!raw", "x", 0, 0]]})
elif aws_event.get("body", None):
# Unfortunately couldn't find a way to get structured body from AWS
# event. Meaning every body is unstructured to us.
request["data"] = AnnotatedValue("", {"rem": [["!raw", "x", 0, 0]]})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _make_request_event_processor.event_processor refactored with the following changes:

Comment on lines -405 to +404
url = (
return (
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _get_cloudwatch_logs_url refactored with the following changes:

Comment on lines -98 to -118
if integration is not None and integration.propagate_traces:
with hub.start_span(op="celery.submit", description=args[0].name) as span:
with capture_internal_exceptions():
headers = dict(hub.iter_trace_propagation_headers(span))

if headers:
# Note: kwargs can contain headers=None, so no setdefault!
# Unsure which backend though.
kwarg_headers = kwargs.get("headers") or {}
kwarg_headers.update(headers)

# https://github.com/celery/celery/issues/4875
#
# Need to setdefault the inner headers too since other
# tracing tools (dd-trace-py) also employ this exact
# workaround and we don't want to break them.
kwarg_headers.setdefault("headers", {}).update(headers)
kwargs["headers"] = kwarg_headers
if integration is None or not integration.propagate_traces:
return f(*args, **kwargs)
with hub.start_span(op="celery.submit", description=args[0].name) as span:
with capture_internal_exceptions():
headers = dict(hub.iter_trace_propagation_headers(span))

if headers:
# Note: kwargs can contain headers=None, so no setdefault!
# Unsure which backend though.
kwarg_headers = kwargs.get("headers") or {}
kwarg_headers.update(headers)

# https://github.com/celery/celery/issues/4875
#
# Need to setdefault the inner headers too since other
# tracing tools (dd-trace-py) also employ this exact
# workaround and we don't want to break them.
kwarg_headers.setdefault("headers", {}).update(headers)
kwargs["headers"] = kwarg_headers

return f(*args, **kwargs)
else:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _wrap_apply_async.apply_async refactored with the following changes:

Comment on lines -46 to +49
# type: (str) -> str
if isinstance(template_name, (list, tuple)):
if template_name:
return "[{}, ...]".format(template_name[0])
else:
if not isinstance(template_name, (list, tuple)):
return template_name
if template_name:
return "[{}, ...]".format(template_name[0])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _get_template_name_description refactored with the following changes:

This removes the following comments ( why? ):

# type: (str) -> str

Comment on lines -47 to -56
rv = []

# On Exception worker will call sys.exit(-1), so we can ignore SystemExit and similar errors
for exc_type, exc_value, tb in walk_exception_chain(exc_info):
if exc_type not in (SystemExit, EOFError, ConnectionResetError):
rv.append(
single_exception_from_error_tuple(
exc_type, exc_value, tb, client_options, mechanism
)
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _capture_exception refactored with the following changes:

This removes the following comments ( why? ):

# On Exception worker will call sys.exit(-1), so we can ignore SystemExit and similar errors

Comment on lines -149 to +150
assert not any(item.data_category == "error" for item in envelope.items)
assert not any(item.get_event() is not None for item in envelope.items)
assert all(item.data_category != "error" for item in envelope.items)
assert all(item.get_event() is None for item in envelope.items)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function monkeypatch_test_transport.check_envelope refactored with the following changes:

Comment on lines -507 to +508
if self.type:
if not isinstance(test_obj, self.type):
return False
if self.type and not isinstance(test_obj, self.type):
return False
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function object_described_by_matcher.ObjectDescribedBy.__eq__ refactored with the following changes:

Comment on lines -12 to +14
if hint:
if "exc_info" in hint:
error = hint["exc_info"][1]
errors.add(error)
if hint and "exc_info" in hint:
error = hint["exc_info"][1]
errors.add(error)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function capture_exceptions.inner.capture_event refactored with the following changes:

response = get_response(request)
return response
return get_response(request)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function simple_middleware.middleware refactored with the following changes:

Comment on lines -46 to +49
assert not any(
crumb["message"] == "LOL" for crumb in event["breadcrumbs"]["values"]
assert all(
crumb["message"] != "LOL" for crumb in event["breadcrumbs"]["values"]
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_logging_defaults refactored with the following changes:

result = MockJobResult()
return result
return MockJobResult()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_sentry_listener_on_job_end.MockJobEnd.jobResult refactored with the following changes:

Comment on lines -121 to +120
stageinf = StageInfo()
return stageinf
return StageInfo()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_sentry_listener_on_stage_submitted.MockStageSubmitted.stageInfo refactored with the following changes:

assert event["request"] == {
assert request == {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_basic refactored with the following changes:

Comment on lines -155 to +154
assert server_tx["request"] == {
assert request == {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_transactions refactored with the following changes:

Comment on lines -100 to +102
else:
event_id = last_event_id()
data = TrytondUserError(str(event_id), str(e))
return app.make_response(request, data)
event_id = last_event_id()
data = TrytondUserError(str(event_id), str(e))
return app.make_response(request, data)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_rpc_error_page._ refactored with the following changes:

Comment on lines -93 to -97
assert trace1["transaction"] == "hi"
else:
trace1, message, trace2 = events

assert trace1["transaction"] == "hi"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test_continue_from_headers refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Dec 1, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.25%.

Quality metrics Before After Change
Complexity 11.55 🙂 11.44 🙂 -0.11 👍
Method Length 47.72 ⭐ 47.48 ⭐ -0.24 👍
Working memory 7.16 🙂 7.13 🙂 -0.03 👍
Quality 70.88% 🙂 71.13% 🙂 0.25% 👍
Other metrics Before After Change
Lines 8644 8529 -115
Changed files Quality Before Quality After Quality Change
examples/tracing/tracing.py 91.39% ⭐ 92.11% ⭐ 0.72% 👍
sentry_sdk/attachments.py 74.59% 🙂 74.37% 🙂 -0.22% 👎
sentry_sdk/client.py 59.77% 🙂 59.60% 🙂 -0.17% 👎
sentry_sdk/envelope.py 84.78% ⭐ 85.30% ⭐ 0.52% 👍
sentry_sdk/hub.py 80.81% ⭐ 81.14% ⭐ 0.33% 👍
sentry_sdk/serializer.py 33.84% 😞 33.86% 😞 0.02% 👍
sentry_sdk/utils.py 77.74% ⭐ 78.09% ⭐ 0.35% 👍
sentry_sdk/integrations/init.py 66.35% 🙂 66.77% 🙂 0.42% 👍
sentry_sdk/integrations/asgi.py 72.00% 🙂 71.73% 🙂 -0.27% 👎
sentry_sdk/integrations/aws_lambda.py 55.05% 🙂 55.57% 🙂 0.52% 👍
sentry_sdk/integrations/celery.py 74.25% 🙂 75.66% ⭐ 1.41% 👍
sentry_sdk/integrations/falcon.py 82.44% ⭐ 83.38% ⭐ 0.94% 👍
sentry_sdk/integrations/gcp.py 49.60% 😞 50.53% 🙂 0.93% 👍
sentry_sdk/integrations/logging.py 80.26% ⭐ 80.21% ⭐ -0.05% 👎
sentry_sdk/integrations/threading.py 77.22% ⭐ 78.27% ⭐ 1.05% 👍
sentry_sdk/integrations/tornado.py 75.87% ⭐ 75.93% ⭐ 0.06% 👍
sentry_sdk/integrations/wsgi.py 77.06% ⭐ 77.06% ⭐ 0.00%
sentry_sdk/integrations/django/init.py 69.82% 🙂 69.74% 🙂 -0.08% 👎
sentry_sdk/integrations/django/templates.py 68.64% 🙂 68.87% 🙂 0.23% 👍
sentry_sdk/integrations/spark/spark_worker.py 68.56% 🙂 69.06% 🙂 0.50% 👍
tests/conftest.py 77.62% ⭐ 77.79% ⭐ 0.17% 👍
tests/integrations/conftest.py 77.35% ⭐ 79.45% ⭐ 2.10% 👍
tests/integrations/django/myapp/middleware.py 93.14% ⭐ 93.29% ⭐ 0.15% 👍
tests/integrations/logging/test_logging.py 84.81% ⭐ 84.83% ⭐ 0.02% 👍
tests/integrations/spark/test_spark.py 88.40% ⭐ 88.41% ⭐ 0.01% 👍
tests/integrations/tornado/test_tornado.py 78.21% ⭐ 78.25% ⭐ 0.04% 👍
tests/integrations/trytond/test_trytond.py 78.16% ⭐ 78.83% ⭐ 0.67% 👍
tests/tracing/test_integration_tests.py 64.56% 🙂 64.92% 🙂 0.36% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
sentry_sdk/serializer.py serialize 145 ⛔ 911 ⛔ 13 😞 11.22% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
sentry_sdk/serializer.py serialize._serialize_node_impl 57 ⛔ 360 ⛔ 13 😞 15.26% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
sentry_sdk/integrations/django/init.py DjangoIntegration.setup_once 32 😞 270 ⛔ 10 😞 29.53% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
sentry_sdk/integrations/aws_lambda.py _wrap_handler 18 🙂 241 ⛔ 14 😞 32.71% 😞 Try splitting into smaller methods. Extract out complex expressions
sentry_sdk/integrations/gcp.py _wrap_func 13 🙂 248 ⛔ 14 😞 36.39% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants