Skip to content

Commit

Permalink
Restrict unicode names #267 (#319)
Browse files Browse the repository at this point in the history
* Restrict unicode names #267

* Restrict unicode names #267 review

* Adds unicode violation, closes #267

* Removes unused fixture from noqa
  • Loading branch information
nsimonoff authored and sobolevn committed Nov 10, 2018
1 parent 22ea20c commit edf198b
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 192 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ for tests. We also now covering more cases and testing violation texts.
- **Breaking**: renames `--min-variable-name-length` into `--min-name-length`
- Dependencies: updates `flake8` version to `3.6`
- Dependencies: removes `pycodestyle` pinned version
- Restrict unicode names

### Bugfixes

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def nested(): # noqa: Z430
cls = 5 # noqa: Z117
__author__ = 'Nikita Sobolev' # noqa: Z410
extremely_long_name_that_needs_to_be_shortened_to_work_fine = 2 # noqa: Z118
привет_по_русски = 'Hello, world!' # noqa: Z119

some._execute() # noqa: Z441

Expand Down
1 change: 1 addition & 0 deletions tests/test_checker/test_noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_noqa_fixture_disabled(absolute_path, all_violations):
'Z116': 1,
'Z117': 1,
'Z118': 1,
'Z119': 1,

'Z200': 0,
'Z201': 0,
Expand Down
162 changes: 0 additions & 162 deletions tests/test_visitors/test_ast/test_naming/test_naming.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_long_variable_name(
default_options,
mode,
):
"""Ensures that short names are not allowed."""
"""Ensures that long names are not allowed."""
long_name = 'incredibly_long_name_that_should_not_pass_the_long_name_test'
tree = parse_ast_tree(mode(naming_template.format(long_name)))

Expand All @@ -21,3 +21,22 @@ def test_long_variable_name(

assert_errors(visitor, [TooLongNameViolation])
assert_error_text(visitor, long_name)


def test_long_variable_name_config(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
options,
mode,
):
"""Ensures that it is possible to configure `max_name_length`."""
long_name = 'incredibly_long_name_that_should_not_pass_the_long_name_test'
tree = parse_ast_tree(mode(naming_template.format(long_name)))

option_values = options(max_name_length=len(long_name) + 1)
visitor = WrongNameVisitor(option_values, tree=tree)
visitor.run()

assert_errors(visitor, [])
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-

import pytest

from wemake_python_styleguide.violations.naming import UnicodeNameViolation
from wemake_python_styleguide.visitors.ast.naming import WrongNameVisitor


@pytest.mark.parametrize('wrong_name', [
'тестовое_имя',
'test_имя',
'сос', # written with identical to ASCII russian chars
'some_變量',
'имя2',
])
def test_wrong_unicode(
assert_errors,
assert_error_text,
parse_ast_tree,
naming_template,
default_options,
mode,
wrong_name,
):
"""Test names with unicode."""
tree = parse_ast_tree(mode(naming_template.format(wrong_name)))

visitor = WrongNameVisitor(default_options, tree=tree)
visitor.run()

assert_errors(visitor, [UnicodeNameViolation])
assert_error_text(visitor, wrong_name)
52 changes: 26 additions & 26 deletions wemake_python_styleguide/logics/naming/logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ def is_too_short_name(
return name != constants.UNUSED_VARIABLE and len(name) < min_length


def is_too_long_name(
name: str,
max_length: int = defaults.MAX_NAME_LENGTH,
) -> bool:
"""
Checks for too long variable names.
>>> is_too_long_name('test')
False
>>> is_too_long_name('this_is_twentynine_characters')
False
>>> is_too_long_name('this_should_fail', max_length=10)
True
>>> is_too_long_name('this_is_thirty_characters_long', max_length=30)
False
>>> is_too_long_name('this_is_thirty_characters_long', max_length=29)
True
"""
return len(name) > max_length


def does_contain_underscored_number(name: str) -> bool:
"""
Checks for variable names with underscored number.
Expand Down Expand Up @@ -160,29 +186,3 @@ def does_contain_consecutive_underscores(name: str) -> bool:
return True

return False


def is_too_long_name(
name: str,
max_length: int = defaults.MAX_NAME_LENGTH,
) -> bool:
"""
Checks for too long variable names.
>>> is_too_long_name('test')
False
>>> is_too_long_name('this_is_twentynine_characters')
False
>>> is_too_long_name('this_should_fail', max_length=10)
True
>>> is_too_long_name('this_is_thirty_characters_long', max_length=30)
False
>>> is_too_long_name('this_is_thirty_characters_long', max_length=29)
True
"""
return len(name) > max_length
35 changes: 32 additions & 3 deletions wemake_python_styleguide/violations/naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
ConsecutiveUnderscoresInNameViolation
ReservedArgumentNameViolation
TooLongNameViolation
UnicodeNameViolation
Module names
------------
Expand All @@ -142,6 +143,7 @@
.. autoclass:: ConsecutiveUnderscoresInNameViolation
.. autoclass:: ReservedArgumentNameViolation
.. autoclass:: TooLongNameViolation
.. autoclass:: UnicodeNameViolation
"""

Expand Down Expand Up @@ -530,7 +532,7 @@ class TooLongNameViolation(MaybeASTViolation):
maybe it may require some documentation.
Solution:
Think of another name. Give more context to it.
Think of another name. Give less context to it.
This rule checks: modules, variables, attributes,
functions, methods, and classes.
Expand All @@ -550,10 +552,37 @@ class TooLongNameViolation(MaybeASTViolation):
Default:
:str:`wemake_python_styleguide.options.defaults.MAX_NAME_LENGTH`
.. versionadded:: 0.1.0
.. versionchanged:: 0.4.0
.. versionadded:: 0.5.0
"""

error_template = 'Found too long name: {0}'
code = 118


@final
class UnicodeNameViolation(ASTViolation):
"""
Restrict unicode names.
Reasoning:
This should be forbidden for sanity, readability, and writability.
Solution:
Rename your entities so that they contain only ASCII symbols.
Example::
# Correct:
some_variable = 'Text with russian: русский язык'
# Wrong:
переменная = 42
some_變量 = ''
.. versionadded:: 0.5.0
"""

error_template = 'Found unicode name: {0}'
code = 119
Loading

0 comments on commit edf198b

Please sign in to comment.