Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Sep 20, 2022

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 nhridoy September 20, 2022 05:20

def make_user_key(self, username):
return 'user_{}'.format(username)
return f'user_{username}'
Copy link
Author

Choose a reason for hiding this comment

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

Function UserResource.make_user_key refactored with the following changes:

users.append(self.conn.hgetall(self.make_user_key(user)))

return users
return [self.conn.hgetall(self.make_user_key(user)) for user in usernames]
Copy link
Author

Choose a reason for hiding this comment

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

Function UserResource.list refactored with the following changes:


def make_user_key(self, username):
return 'user_{}'.format(username)
return f'user_{username}'
Copy link
Author

Choose a reason for hiding this comment

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

Function UserResource.make_user_key refactored with the following changes:

users.append(self.conn.hgetall(self.make_user_key(user)))

return users
return [self.conn.hgetall(self.make_user_key(user)) for user in usernames]
Copy link
Author

Choose a reason for hiding this comment

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

Function UserResource.list refactored with the following changes:

Comment on lines -63 to +64
def as_list(self, *args, **kwargs):
return csrf_exempt(super(DjangoResource, self).as_list(*args, **kwargs))
def as_list(cls, *args, **kwargs):
return csrf_exempt(super(DjangoResource, cls).as_list(*args, **kwargs))
Copy link
Author

Choose a reason for hiding this comment

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

Function DjangoResource.as_list refactored with the following changes:

Comment on lines -273 to 277
# ``endpoint`` errors as well.
if not method in self.http_methods.get(endpoint, {}):
if method not in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
f"Unsupported method '{method}' for {endpoint} endpoint."
)

Copy link
Author

Choose a reason for hiding this comment

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

Function Resource.handle refactored with the following changes:

Comment on lines -343 to +341
if body:
return self.serializer.deserialize(body)

return []
return self.serializer.deserialize(body) if body 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 Resource.deserialize_list refactored with the following changes:

Comment on lines -357 to +352
if body:
return self.serializer.deserialize(body)

return {}
return self.serializer.deserialize(body) if body 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 Resource.deserialize_detail refactored with the following changes:

Comment on lines -405 to +401
if not getattr(data, 'should_prepare', True):
prepped_data = data.value
else:
prepped_data = [self.prepare(item) for item in data]
prepped_data = (
[self.prepare(item) for item in data]
if getattr(data, 'should_prepare', True)
else data.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 Resource.serialize_list refactored with the following changes:

Comment on lines -428 to +425
if not getattr(data, 'should_prepare', True):
prepped_data = data.value
else:
prepped_data = self.prepare(data)
prepped_data = (
self.prepare(data)
if getattr(data, 'should_prepare', True)
else data.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 Resource.serialize_detail refactored with the following changes:

Comment on lines -482 to +476
if self.request_method() == 'GET':
return True

return False
return self.request_method() == 'GET'
Copy link
Author

Choose a reason for hiding this comment

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

Function Resource.is_authenticated refactored with the following changes:

else:
FUTURES = (Future, futures.Future)

FUTURES = Future if futures is None else (Future, futures.Future)
Copy link
Author

Choose a reason for hiding this comment

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

Lines 27-31 refactored with the following changes:

Comment on lines -103 to 111
new_cls = type(
cls.__name__ + '_' + _BridgeMixin.__name__ + '_restless',
(_BridgeMixin, cls._request_handler_base_,),
f'{cls.__name__}_{_BridgeMixin.__name__}_restless',
(
_BridgeMixin,
cls._request_handler_base_,
),
dict(
__resource_cls__=cls,
__resource_args__=init_args,
__resource_kwargs__=init_kwargs,
__resource_view_type__=view_type)
__resource_view_type__=view_type,
),
)

Copy link
Author

Choose a reason for hiding this comment

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

Function TornadoResource.as_view refactored with the following changes:

Comment on lines -132 to +134
if status == NO_CONTENT:
# Avoid crashing the client when it tries to parse nonexisting JSON.
content_type = 'text/plain'
else:
content_type = 'application/json'
self.ref_rh.set_header("Content-Type", "{}; charset=UTF-8"
.format(content_type))
content_type = 'text/plain' if status == NO_CONTENT else 'application/json'
self.ref_rh.set_header("Content-Type", f"{content_type}; charset=UTF-8")
Copy link
Author

Choose a reason for hiding this comment

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

Function TornadoResource.build_response refactored with the following changes:

This removes the following comments ( why? ):

# Avoid crashing the client when it tries to parse nonexisting JSON.

try:
if not method in self.http_methods.get(endpoint, {}):
if method not in self.http_methods.get(endpoint, {}):
raise MethodNotImplemented(
"Unsupported method '{}' for {} endpoint.".format(
method,
endpoint
)
f"Unsupported method '{method}' for {endpoint} endpoint."
)

Copy link
Author

Choose a reason for hiding this comment

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

Function TornadoResource.handle refactored with the following changes:

Comment on lines -83 to +80
raise ObjectDoesNotExist("Model with pk {} not found.".format(pk))
raise ObjectDoesNotExist(f"Model with pk {pk} not found.")
Copy link
Author

Choose a reason for hiding this comment

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

Function DjTestResource.detail refactored with the following changes:

Comment on lines -157 to +154
raise Http404("Model with pk {} not found.".format(pk))
raise Http404(f"Model with pk {pk} not found.")
Copy link
Author

Choose a reason for hiding this comment

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

Function DjTestResourceHttp404Handling.detail refactored with the following changes:

return False

return True
return self.request_method() != 'DELETE'
Copy link
Author

Choose a reason for hiding this comment

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

Function PyrTestResource.is_authenticated refactored with the following changes:

Comment on lines -22 to +25
for i in six.moves.xrange(min(len(v), len(version_info))):
if v[i] != version_info[i]:
return False
return True
return all(
v[i] == version_info[i]
for i in six.moves.xrange(min(len(v), len(version_info)))
)
Copy link
Author

Choose a reason for hiding this comment

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

Function _equal_ refactored with the following changes:

if item['id'] == pk:
return item
return None
return next((item for item in self.fake_db if item['id'] == pk), None)
Copy link
Author

Choose a reason for hiding this comment

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

Function TndBasicTestResource.detail refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Sep 20, 2022

Sourcery Code Quality Report

❌  Merging this PR will decrease code quality in the affected files by 0.25%.

Quality metrics Before After Change
Complexity 1.24 ⭐ 1.09 ⭐ -0.15 👍
Method Length 42.86 ⭐ 42.44 ⭐ -0.42 👍
Working memory 4.62 ⭐ 4.70 ⭐ 0.08 👎
Quality 85.51% 85.26% -0.25% 👎
Other metrics Before After Change
Lines 1955 1891 -64
Changed files Quality Before Quality After Quality Change
examples/flask/app.py 93.29% ⭐ 93.93% ⭐ 0.64% 👍
examples/pyramid/app.py 93.07% ⭐ 93.65% ⭐ 0.58% 👍
restless/dj.py 80.84% ⭐ 79.89% ⭐ -0.95% 👎
restless/fl.py 89.26% ⭐ 88.69% ⭐ -0.57% 👎
restless/preparers.py 80.29% ⭐ 80.67% ⭐ 0.38% 👍
restless/pyr.py 85.53% ⭐ 84.58% ⭐ -0.95% 👎
restless/resources.py 88.22% ⭐ 87.57% ⭐ -0.65% 👎
restless/tnd.py 77.61% ⭐ 77.10% ⭐ -0.51% 👎
restless/utils.py 82.54% ⭐ 82.76% ⭐ 0.22% 👍
tests/test_dj.py 83.25% ⭐ 83.24% ⭐ -0.01% 👎
tests/test_pyr.py 88.88% ⭐ 88.81% ⭐ -0.07% 👎
tests/test_tnd.py 88.64% ⭐ 88.53% ⭐ -0.11% 👎

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

File Function Complexity Length Working Memory Quality Recommendation
restless/tnd.py TornadoResource.handle 4 ⭐ 149 😞 9 🙂 61.10% 🙂 Try splitting into smaller methods
restless/resources.py Resource.handle 3 ⭐ 134 😞 9 🙂 63.76% 🙂 Try splitting into smaller methods
tests/test_dj.py DjangoResourceTestCase.test_handle_not_authenticated 0 ⭐ 191 😞 6 ⭐ 68.19% 🙂 Try splitting into smaller methods

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!

Copy link

@code-review-doctor code-review-doctor bot left a comment

Choose a reason for hiding this comment

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

Worth considering. View full project report here.

def as_list(self, *args, **kwargs):
return csrf_exempt(super(DjangoResource, self).as_list(*args, **kwargs))
def as_list(cls, *args, **kwargs):
return csrf_exempt(super(DjangoResource, cls).as_list(*args, **kwargs))

Choose a reason for hiding this comment

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

Suggested change
return csrf_exempt(super(DjangoResource, cls).as_list(*args, **kwargs))
return csrf_exempt(super().as_list(*args, **kwargs))

It's unnecessary to use arguments when calling super for the parent class. Read more.

def as_detail(self, *args, **kwargs):
return csrf_exempt(super(DjangoResource, self).as_detail(*args, **kwargs))
def as_detail(cls, *args, **kwargs):
return csrf_exempt(super(DjangoResource, cls).as_detail(*args, **kwargs))

Choose a reason for hiding this comment

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

Suggested change
return csrf_exempt(super(DjangoResource, cls).as_detail(*args, **kwargs))
return csrf_exempt(super().as_detail(*args, **kwargs))

As above, These super arguments are unnecessary.

@nhridoy nhridoy merged commit aa3b9a5 into master Sep 20, 2022
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.

2 participants