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

refactor(util.uri): refactor dict comprehension #1783

Merged
merged 14 commits into from
Dec 19, 2020
Merged

refactor(util.uri): refactor dict comprehension #1783

merged 14 commits into from
Dec 19, 2020

Conversation

withshubh
Copy link
Contributor

@withshubh withshubh commented Nov 16, 2020

This PR fixes some of the quality issues raised by DeepSource on my fork of this repository.

Some of them are:

  • Performance Issues: 4
  • Bug Risks: 14
  • Anti-Patterns: 24
  • Security: 6

Take a quick look at all the issues caught by DeepSource for this repository here

Summary of Changes

  • Change methods not using its bound instance to staticmethods
  • use literal syntax instead of function calls to create data structure
  • remove assert statement from non-test files
  • refactor unnecessary 'else' / 'elif' when 'if' block has a 'return' statement
  • replace ternary syntax with if expression
  • remove methods with unnecessary super delegation
  • remove unnecessary generator

You can also have a look at the configuration file I used for DeepSource Analysis.

If any of these issues that the DeepSource analysis found are critical for the codebase I'd suggest you to activate the analysis. You can trigger analysis on each PR once you integrate DeepSource in the repo. ⚡

Feel free to merge this PR if you wish to fix the issues.✨

@vytas7
Copy link
Member

vytas7 commented Nov 16, 2020

Hi, and thanks for this!

I'm, however, unsure if we want to refactor everything to @staticmethod. In some places, methods semantically belong to a class instance, even if they do not use self. In others, it probably does not warrant a change.

@kgriffs @CaselIT thoughts?

@CaselIT
Copy link
Member

CaselIT commented Nov 16, 2020

I'm definitely -1 on the static method thing.

@vytas7
Copy link
Member

vytas7 commented Nov 16, 2020

Another comment unrelated to the use of @staticmethod.

Sorry to be blunt, but refactoring assert statements to manually raising AssertionError makes no sense to me.
The assert statement is the canonical way of writing tests with pytest. Manually raising an AssertionError in a conditional branch may even interfere with pytest's ability to infer useful failure messages.

@@ -49,9 +49,9 @@
_HEX_DIGITS = '0123456789ABCDEFabcdef'

# This map construction is based on urllib's implementation
_HEX_TO_BYTE = dict(((a + b).encode(), bytes([int(a + b, 16)]))
_HEX_TO_BYTE = {(a + b).encode(): bytes([int(a + b, 16)])
Copy link
Member

Choose a reason for hiding this comment

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

I guess this change we could actually keep. The old form probably could not use dict comprehension because it had to support Python 2.6.

@@ -23,8 +24,7 @@ def __call__(self, req, resp, **kwargs):


class SinkAsync(Sink):
async def __call__(self, req, resp, **kwargs):
Copy link
Member

@vytas7 vytas7 Nov 16, 2020

Choose a reason for hiding this comment

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

Reimplementing __call__ is intended here (note the change from sync to async).

Tbh, this looks like a bug in DeepSource if the above is marked for refactoring.

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Thank you once again for this initiative to improve our code quality.

However, as discussed in the above comments, we do not want to indiscriminately refactor the following:

  • Functions not making use of self ➡️ @staticmethod. @staticmethod may at times semantically make sense, but more often than not, it is not the case in our codebase. Moreover, it does not offer any speed improvement (it actually may even incur a slight penalty).
  • assert ➡️ manually raising an AssertionError in a branch statement -- we simply do not want this.
  • elif after return or raise ➡️ if. While I have nothing against the pattern itself, and actually use it, we need to discuss whether it is worth the effort (e.g., how do these alternatives compare performance wise?) refactoring. I.e., I'm actually not opposed to this one, but it is better to discuss this one separately.

I know it feels like harsh feedback... 👿 But could you roll back refactoring of the 3 above patterns, and we could take a look at what's left. As noted on the PR, there are some good changes that we could actually be willing to keep.

Notwithstanding potential improvements, you also have to get our CI to pass in order for a PR to be considered. Check how to run tox or our mintest.sh wrapper here: CONTRIBUTING.md. Right now, it seems that some tests got broken because of refactoring, including the simplest of smoke tests.

Needless to say, @withshubh, You are also very welcome to discuss these changes with us on our Gitter channels!

@codecov
Copy link

codecov bot commented Nov 18, 2020

Codecov Report

Merging #1783 (e6d4681) into master (c7ab8da) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #1783   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           54        54           
  Lines         5128      5128           
  Branches       823       823           
=========================================
  Hits          5128      5128           
Impacted Files Coverage Δ
falcon/util/uri.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c7ab8da...e6d4681. Read the comment docs.

@vytas7
Copy link
Member

vytas7 commented Dec 10, 2020

@withshubh just checking, if you are planning to continue work on this PR?

And I'm sorry to be blunt (again), but an en masse autopep8 refactoring is not something we want to accept as a side effect of this PR. We are looking into adopting black, autopep8 or other alternatives, but that needs to be a separate, informed decision by the maintainer team. You'll have to revert those changes from your branch in order to proceed.

@withshubh
Copy link
Contributor Author

Hi @vytas7 👋

It's okay for being blunt(again) 😛 I'll fix this and update the PR. ✨

@withshubh
Copy link
Contributor Author

Hi @vytas7

Please have a look. 👀

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Please see inline comments.

.deepsource.toml Outdated
@@ -0,0 +1,10 @@
version = 1
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this file.
We have not decided on using DeepSource yet.

@@ -61,7 +61,7 @@ class CaseInsensitiveDict(MutableMapping): # pragma: no cover

"""
def __init__(self, data=None, **kwargs):
self._store = dict()
self._store = {}
Copy link
Member

Choose a reason for hiding this comment

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

Please read the comment on line 33 to understand why, and revert this change.

@@ -98,7 +98,7 @@ def quality_and_fitness_parsed(mime_type, parsed_ranges):
if type_match and subtype_match:

# 100 points if the type matches w/o a wildcard
fitness = type == target_type and 100 or 0
fitness = 100 if type == target_type else 0
Copy link
Member

Choose a reason for hiding this comment

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

Let us revert these changes. Sensible they may be, we have vendored this module from https://github.com/dbtsai/python-mimeparse, and do not want to make any modifications unless absolutely necessary.

@@ -181,7 +181,7 @@ def best_match(supported, header):
pos += 1
weighted_matches.sort()

return weighted_matches[-1][0][0] and weighted_matches[-1][2] or ''
return weighted_matches[-1][2] if weighted_matches[-1][0][0] else ''
Copy link
Member

Choose a reason for hiding this comment

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

Let us revert these changes. Sensible they may be, we have vendored this module from https://github.com/dbtsai/python-mimeparse, and do not want to make any modifications unless absolutely necessary.

@withshubh
Copy link
Contributor Author

Hi @vytas7

Now this PR contains just one fix:

  • remove unnecessary generator

I'd like to know what are your thoughts regarding integrating the DeepSource.

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Let us please address PEP8 violations before merging this PR:

./falcon/util/uri.py:53:21: E127 continuation line over-indented for visual indent
./falcon/util/uri.py:54:21: E127 continuation line over-indented for visual indent

@vytas7 vytas7 changed the title Potential bug-fixes using DeepSource refactor(util.uri): refactor dict comprehension Dec 16, 2020
@withshubh
Copy link
Contributor Author

Let us please address PEP8 violations before merging this PR:

./falcon/util/uri.py:53:21: E127 continuation line over-indented for visual indent
./falcon/util/uri.py:54:21: E127 continuation line over-indented for visual indent

Done @vytas7

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

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

Thanks for this!

Regarding DeepSource, my impression is that it is likely a very useful tool for a business project, or even a new open source library if DeepSource is agreed upon in advance.
However, for a highly specialized framework Falcon is, as you may have noticed, many "quality issues" exist there for a reason ("vendored" code, explicit test scenarios, deprecation strategies etc).
Moreover, another drawback is that DeepSource is not open, so we cannot easily integrate it into our offline development routines.

@vytas7 vytas7 merged commit ba78a52 into falconry:master Dec 19, 2020
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.

4 participants