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

Added TooLongVariableNameViolation #291

Merged
merged 10 commits into from Nov 10, 2018
Merged

Conversation

Casva
Copy link
Contributor

@Casva Casva commented Oct 24, 2018

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

Related issues

Closes #288
Refs #288

Let me know if anything needs to be changed! 😄

@coveralls
Copy link

coveralls commented Oct 24, 2018

Pull Request Test Coverage Report for Build 681

  • 14 of 14 (100.0%) changed or added relevant lines in 6 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 680: 0.0%
Covered Lines: 1582
Relevant Lines: 1582

💛 - Coveralls

Copy link
Member

@sobolevn sobolevn 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 starting to work on this rule!

You can continue to implement the business logic for this rule, while we are releasing the 0.3.0 version.

Later, I will come back for the full review and we will merge it into 0.4.0.
Does this sound fine to you?

@Casva
Copy link
Contributor Author

Casva commented Oct 26, 2018

@sobolevn yup sure no problem!


# Correct:
totalPrice = 100
coursesEnrolled = 5
Copy link
Contributor

Choose a reason for hiding this comment

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

Just nitpicking, PEP8 does not recommend camelCase

Function and Variable Names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

we may not want the example to promote wrong notion, especially on a style guide :-)

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @nixphix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nixphix Noted! Just made the changes 👍

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thanks!

@Casva can you please also add a business logic and tests for your rule?

CHANGELOG.md Outdated Show resolved Hide resolved
wemake_python_styleguide/options/config.py Outdated Show resolved Hide resolved
wemake_python_styleguide/violations/naming.py Show resolved Hide resolved
wemake_python_styleguide/violations/naming.py Outdated Show resolved Hide resolved
@Casva
Copy link
Contributor Author

Casva commented Oct 31, 2018

@sobolevn sure not a problem!

I also noticed that the "TooShortVariableNameViolation" rule was changed to "TooShortNameViolation" and checks for modules, variables, attributes, functions, methods, and classes.

Did you want me to change TooLongVariableNameViolation to do the same thing?

@sobolevn
Copy link
Member

@Casva yes, I have unified logic for variables and modules. It seems reasonable to me, what do you think?

@Casva
Copy link
Contributor Author

Casva commented Nov 1, 2018

@sobolevn makes sense to me.
I'll do the same with the too long rule as well

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Awesome work! Thank you.

Just some documentation fixes and we are ready to go!

False

"""
return name != constants.UNUSED_VARIABLE and len(name) > max_length
Copy link
Member

Choose a reason for hiding this comment

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

We do not care about _ here. Since it is only one char long.

visitor.run()

assert_errors(visitor, [TooLongNameViolation])
assert_error_text(visitor, filename.replace('.py', ''))
Copy link
Member

Choose a reason for hiding this comment

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

Missing new line at the end of the file.

@@ -43,6 +43,9 @@ class Configuration(object):
- ``min-name-length`` - minimum number of chars to define a valid
variable and module name, defaults to
:str:`wemake_python_styleguide.options.defaults.MIN_NAME_LENGTH`
- ``max-name-length`` - maximum number of chars to define a valid
variable name, defaults to
Copy link
Member

Choose a reason for hiding this comment

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

variable and module name

_Option(
'--max-name-length',
defaults.MAX_NAME_LENGTH,
'Maximum possible length of the variable name.',
Copy link
Member

Choose a reason for hiding this comment

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

variable and module name

@@ -21,6 +21,9 @@
#: Minimum variable's name length.
MIN_NAME_LENGTH: Final = 2

#: Maximum variable's name length:
Copy link
Member

Choose a reason for hiding this comment

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

variable and module name length

@final
class TooLongNameViolation(ASTViolation):
"""
Forbids to have too long variable or module names.
Copy link
Member

Choose a reason for hiding this comment

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

variable and module names


.. versionadded:: 0.4.0

Note:
Copy link
Member

Choose a reason for hiding this comment

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

We do not use Note anymore, see this commit: e428aab


"""

#: Error message shown to the user.
Copy link
Member

Choose a reason for hiding this comment

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

We do not use this comment anymore, see this commit e428aab

@Casva
Copy link
Contributor Author

Casva commented Nov 2, 2018

@sobolevn Alright! Everything should be good to go now! 😄

@Casva
Copy link
Contributor Author

Casva commented Nov 2, 2018

There's a lot of names in the repo that are longer than the default maximum length (29 characters).
Should I change the default length to 40 characters instead?

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Just a single typo left!

return name != len(name) > max_length
Copy link
Member

Choose a reason for hiding this comment

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

there's an error here. please, remove name !=

@Casva
Copy link
Contributor Author

Casva commented Nov 2, 2018

@sobolevn Aand done! 👍

@sobolevn
Copy link
Member

sobolevn commented Nov 2, 2018

@Casva yes, please change it to 40 for now.
I will play with this value afterwards.

Also, please take care of some build issues.
It may be easier to run tests to see what's wrong.

@Casva
Copy link
Contributor Author

Casva commented Nov 3, 2018

@sobolevn after some mass cleaning up it's almost perfect!
There are a few things I wanted to point out though:

  • I had to change the MAX_NAME_LENGTH to 55 to accommodate the longest name in the repo (which i believe was 53 characters)
  • I also had to increase to max-complexity by 1 since I added a check for max_name_length under the _check_name method
  • under logical.py the line exceeds the allowed maximum length but backslash is not allowed for line breaks, is there another option I could use?

Thanks!

setup.cfg Outdated
@@ -11,7 +11,7 @@ branch = True


[flake8]
max-complexity = 6
max-complexity = 7
Copy link
Member

Choose a reason for hiding this comment

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

Please, do not change this value.

@sobolevn
Copy link
Member

sobolevn commented Nov 3, 2018

@Casva thanks for your work! We are almost there!

  1. Yes, seems fine. I will double check it later
  2. Please, do not do that. Instead we need to refactor the code if it gets too complex. That's the point why we use this check
  3. You can just ignore the doctest for this function, no big deal

👍

@Casva
Copy link
Contributor Author

Casva commented Nov 3, 2018

@sobolevn Got it, I changed max-complexity back to its original value

I'm at a loss for refactoring: I've tried grouping the length testers into another method and incorporating that in the original _check_name method but that exceeds to max_methods for the class.
Aaand making a brand new class to contain some of the tests doesn't seem like the best idea either. 😢

@sobolevn
Copy link
Member

sobolevn commented Nov 3, 2018

@Casva ok, leave this method as noqa for now.
I will handle that, since I am currently refactoring a lot of modules anyway.

P.S. Have you tried this project on your own codebase? What are the results? Do you find it useful? Any feedback is appreciated 🙂

@Casva
Copy link
Contributor Author

Casva commented Nov 4, 2018

@sobolevn alright done! Thanks for all your patience!

And yes I tried using it and I realized how poorly I code 😆
I'm still in school at the moment and this was a great tool to give me an idea of best practices to use while coding 👍

@sobolevn
Copy link
Member

sobolevn commented Nov 4, 2018

@Casva it was still failing.

Can you please rebase your changes and fix the tests?
I have implemented a new logic for WrongNameVisitor. So, it would be easy.
I have also refactored how tests are located.

@Casva Casva force-pushed the issue-288 branch 2 times, most recently from 9bcaf66 to 65f5ef8 Compare November 7, 2018 08:48
@Casva Casva force-pushed the issue-288 branch 6 times, most recently from 7fe9b05 to 3d3bb8a Compare November 7, 2018 09:37
@Casva
Copy link
Contributor Author

Casva commented Nov 7, 2018

Sorry for the delay @sobolevn, been busy with school work.
Everything should be good to go!

Let me know if there's anything else you need changed! 👍

@sobolevn
Copy link
Member

sobolevn commented Nov 9, 2018

@Casva can you please resolve the merge conflicts once again please?
And I will merge it asap.

Thanks!

…gVariableNameViolation rule

Updated CHANGELOG.md
…nd associated tests

Fixed config MAX_VARIABLE_LENGTH -> MAX_NAME_LENGTH, updated types.py to contain max_name_length

added rule integration into noqa and test_noqa

Fixed documentation (mainly variable -> variable and module)

Forgot extra line at the end of logical.py

Reordered imports to match tests

Fixed line 188 in logical.py

Changed default max-length to 40, changed to tests to reflect this as well
ASTViolation -> MaybeASTViolation in naming.py | fixed multi-line string error (added \) in logical.py | other syntax fixes

Removed WrongModuleNamePatternViolation in test_module_name_length.py on line 56 | replaced backslashe line break with indent)
…_check_name under naming.py

Removed WrongModuleNamePatternViolation in test_module_name_length.py on line 56 | replaced backslashe line break with indent)

Changed max-complexity from 6 to 7 to accomodate new if statement in _check_name under naming.py
Refactored _check_name to include new function _check_name_length; reduces complexity

Erased variable assignment in _check_name instead

Reverted _check_name back to original
Also removed test in test_naming until _check_name is refactored

Removed instances of previous TooLongNameViolation tests in naming.py and test_naming.py
Fixed imports

Fixed whitspace issue

Workaround for line limit

a

a

a

a
@Casva
Copy link
Contributor Author

Casva commented Nov 10, 2018

@sobolevn rebased and fixed!

@sobolevn
Copy link
Member

sobolevn commented Nov 10, 2018

@Casva you have made an amazing job! Thank you very much!

@sobolevn sobolevn merged commit 22ea20c into wemake-services:master Nov 10, 2018
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.

Restrict too long variable names
4 participants