X Tutup
Skip to content

Sourcery refactored main branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main
Open

Sourcery refactored main branch#1
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Jul 16, 2022

Branch main 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 main branch, then run:

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

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from taljaards July 16, 2022 08:53
'starkbank-ecdsa>=1.0.0'
]
return deps
return ['python_http_client>=3.2.1', 'starkbank-ecdsa>=1.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 getRequires refactored with the following changes:

Comment on lines -51 to -56
mock_pers = dict()
mock_pers = {
'to_list': [
To("test1@example.com", "Example User"),
To("test2@example.com", "Example User"),
]
}

mock_pers['to_list'] = [To("test1@example.com",
"Example User"),
To("test2@example.com",
"Example User")]
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_mock_personalization_dict refactored with the following changes:

self.impersonate_subuser = impersonate_subuser
self.version = __version__
self.useragent = 'sendgrid/{};python'.format(self.version)
self.useragent = f'sendgrid/{self.version};python'
Copy link
Author

Choose a reason for hiding this comment

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

Function BaseInterface.__init__ refactored with the following changes:

"""
self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
auth = 'Bearer {}'.format(self.api_key)
auth = f'Bearer {self.api_key}'
Copy link
Author

Choose a reason for hiding this comment

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

Function SendGridAPIClient.__init__ refactored with the following changes:

Comment on lines -71 to +75
auth = 'Basic ' + b64encode('{}:{}'.format(self.username, self.password).encode()).decode()
auth = (
'Basic '
+ b64encode(f'{self.username}:{self.password}'.encode()).decode()
)

Copy link
Author

Choose a reason for hiding this comment

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

Function TwilioEmailAPIClient.__init__ refactored with the following changes:

:type dicts: list(dict)
"""
d = dict()
d = {}
Copy link
Author

Choose a reason for hiding this comment

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

Function Mail._flatten_dicts refactored with the following changes:

Comment on lines +484 to +489
elif isinstance(header, dict):
(k, v) = list(header.items())[0]
self._headers = self._ensure_append(
Header(k, v), self._headers)
else:
if isinstance(header, dict):
(k, v) = list(header.items())[0]
self._headers = self._ensure_append(
Header(k, v), self._headers)
else:
self._headers = self._ensure_append(header, self._headers)
self._headers = self._ensure_append(header, self._headers)
Copy link
Author

Choose a reason for hiding this comment

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

Function Mail.add_header refactored with the following changes:

Comment on lines +527 to +533
elif isinstance(substitution, list):
for s in substitution:
for p in self.personalizations:
p.add_substitution(substitution)
p.add_substitution(s)
else:
for p in self.personalizations:
p.add_substitution(substitution)
Copy link
Author

Choose a reason for hiding this comment

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

Function Mail.add_substitution refactored with the following changes:

Comment on lines +584 to +590
elif isinstance(custom_arg, dict):
(k, v) = list(custom_arg.items())[0]
self._custom_args = self._ensure_append(
CustomArg(k, v), self._custom_args)
else:
if isinstance(custom_arg, dict):
(k, v) = list(custom_arg.items())[0]
self._custom_args = self._ensure_append(
CustomArg(k, v), self._custom_args)
else:
self._custom_args = self._ensure_append(
custom_arg, self._custom_args)
self._custom_args = self._ensure_append(
custom_arg, self._custom_args)
Copy link
Author

Choose a reason for hiding this comment

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

Function Mail.add_custom_arg refactored with the following changes:

Comment on lines -751 to +748
if self._contents:
index = len(self._contents)
else:
index = 0
index = len(self._contents) if self._contents else 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 Mail.add_content refactored with the following changes:

Comment on lines -235 to +246
value = getattr(self, key)
if value:
if value := getattr(self, key):
personalization[key[:-1]] = value

for key in ['subject', 'send_at', 'dynamic_template_data']:
value = getattr(self, key)
if value:
if value := getattr(self, key):
personalization[key] = value

for prop_name in ['headers', 'substitutions', 'custom_args']:
prop = getattr(self, prop_name)
if prop:
if prop := getattr(self, prop_name):
obj = {}
for key in prop:
obj.update(key)
obj |= key
Copy link
Author

Choose a reason for hiding this comment

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

Function Personalization.get refactored with the following changes:

self._post_to_url = value
else:
self._post_to_url = SpamUrl(value)
self._post_to_url = value if isinstance(value, SpamUrl) else SpamUrl(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 SpamCheck.post_to_url refactored with the following changes:

Comment on lines -45 to +57
# Default param
elif isinstance(request_body, dict):

contents = request_body.get("content", list())

for content in contents:
if content is not None:
if (content.get("type") == "text/html" or
isinstance(content.get("value"), str)):
message_text = content.get("value", "")
self.validate_message_text(message_text)
if content is not None and (
(
content.get("type") == "text/html"
or isinstance(content.get("value"), str)
)
):
message_text = content.get("value", "")
self.validate_message_text(message_text)
Copy link
Author

Choose a reason for hiding this comment

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

Function ValidateApiKey.validate_message_dict refactored with the following changes:

This removes the following comments ( why? ):

# Default param

def test_init_environment_should_not_set_env_if_format_is_invalid(self):
config_file = sendgrid.helpers.inbound.config.__file__
env_file_path = os.path.abspath(os.path.dirname(config_file)) + '/.env'
env_file_path = f'{os.path.abspath(os.path.dirname(config_file))}/.env'
Copy link
Author

Choose a reason for hiding this comment

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

Function UnitTests.test_init_environment_should_not_set_env_if_format_is_invalid refactored with the following changes:

name = "SomeName"
address = "test@example.com"
name_address = "{} <{}>".format(name, address)
name_address = f"{name} <{address}>"
Copy link
Author

Choose a reason for hiding this comment

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

Function TestEmailObject.test_add_rfc_email refactored with the following changes:


def test_useragent(self):
useragent = '{}{}{}'.format('sendgrid/', sendgrid.__version__, ';python')
useragent = f'sendgrid/{sendgrid.__version__};python'
Copy link
Author

Choose a reason for hiding this comment

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

Function UnitTests.test_useragent refactored with the following changes:

def get_all_ip():
ret_val = json.loads(ret_json)
return ret_val
return json.loads(ret_json)
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_all_ip refactored with the following changes:

Comment on lines -65 to +64
data = set()
data.add("208.115.214.23")
data.add("208.115.214.22")
return data
return {"208.115.214.23", "208.115.214.22"}
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_data refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jul 16, 2022

Sourcery Code Quality Report

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

Quality metrics Before After Change
Complexity 2.48 ⭐ 2.34 ⭐ -0.14 👍
Method Length 36.58 ⭐ 36.43 ⭐ -0.15 👍
Working memory 7.59 🙂 7.61 🙂 0.02 👎
Quality 82.79% 82.92% 0.13% 👍
Other metrics Before After Change
Lines 4675 4636 -39
Changed files Quality Before Quality After Quality Change
setup.py 87.39% ⭐ 85.81% ⭐ -1.58% 👎
examples/helpers/mail_example.py 55.23% 🙂 55.03% 🙂 -0.20% 👎
sendgrid/base_interface.py 89.81% ⭐ 89.74% ⭐ -0.07% 👎
sendgrid/sendgrid.py 83.40% ⭐ 83.40% ⭐ 0.00%
sendgrid/twilio_email.py 68.71% 🙂 70.45% 🙂 1.74% 👍
sendgrid/helpers/endpoints/ip/unassigned.py 82.36% ⭐ 82.29% ⭐ -0.07% 👎
sendgrid/helpers/inbound/parse.py 82.06% ⭐ 82.54% ⭐ 0.48% 👍
sendgrid/helpers/mail/asm.py 89.98% ⭐ 90.10% ⭐ 0.12% 👍
sendgrid/helpers/mail/attachment.py 83.58% ⭐ 83.01% ⭐ -0.57% 👎
sendgrid/helpers/mail/ganalytics.py 87.06% ⭐ 88.00% ⭐ 0.94% 👍
sendgrid/helpers/mail/mail.py 70.29% 🙂 70.84% 🙂 0.55% 👍
sendgrid/helpers/mail/personalization.py 86.28% ⭐ 86.86% ⭐ 0.58% 👍
sendgrid/helpers/mail/spam_check.py 88.08% ⭐ 88.05% ⭐ -0.03% 👎
sendgrid/helpers/mail/validators.py 75.67% ⭐ 77.12% ⭐ 1.45% 👍
test/test_config.py 84.97% ⭐ 84.92% ⭐ -0.05% 👎
test/test_email.py 92.30% ⭐ 92.26% ⭐ -0.04% 👎
test/test_sendgrid.py 88.30% ⭐ 88.29% ⭐ -0.01% 👎
test/test_unassigned.py 95.96% ⭐ 95.59% ⭐ -0.37% 👎

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

File Function Complexity Length Working Memory Quality Recommendation
sendgrid/helpers/mail/mail.py Mail._set_emails 47 ⛔ 156 😞 12 😞 29.08% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
examples/helpers/mail_example.py build_kitchen_sink 0 ⭐ 864 ⛔ 60 ⛔ 33.35% 😞 Try splitting into smaller methods. Extract out complex expressions
sendgrid/helpers/mail/mail.py Mail.add_content 17 🙂 105 🙂 10 😞 52.66% 🙂 Extract out complex expressions
sendgrid/helpers/mail/mail.py Mail.__init__ 6 ⭐ 158 😞 11 😞 54.26% 🙂 Try splitting into smaller methods. Extract out complex expressions
sendgrid/helpers/mail/mail.py Mail.add_header 10 🙂 110 🙂 11 😞 56.06% 🙂 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

X Tutup