Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Daniël van Noord <[email protected]>
  • Loading branch information
CarliJoy and DanielNoord committed Jan 7, 2022
1 parent 7c0411a commit 415a13b
Show file tree
Hide file tree
Showing 30 changed files with 58 additions and 46 deletions.
9 changes: 5 additions & 4 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Release date: TBA

Closes #5588

* Add ``non-ascii-identifier`` as replacement ``non-ascii-name``
to ensure really __all__ Python names are ASCII.
Checker now checks properly the names of imports (``non-ascii-module-import``) as
well for as of file names (``non-ascii-file-name``).
* Renamed ``non-ascii-name`` to ``non-ascii-identifier`` and rewrote the checker.
It now ensures __all__ Python names are ASCII and also properly
checks the names of imports (``non-ascii-module-import``) as
well as file names (``non-ascii-file-name``) and emits their respective new warnings.

Non ASCII characters could be homoglyphs (look alike characters) and hard to
enter on a non specialized keyboard.
See `Confusable Characters in PEP 672 <https://www.python.org/dev/peps/pep-0672/#confusable-characters-in-identifiers>`_
Expand Down
11 changes: 6 additions & 5 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ New checkers

Closes #5460

* Add ``non-ascii-identifier`` as replacement ``non-ascii-name``
to ensure really __all__ Python names are ASCII.
Checker now checks properly the names of imports (``non-ascii-module-import``) as
well for as of file names (``non-ascii-file-name``).
* Renamed ``non-ascii-name`` to ``non-ascii-identifier`` and rewrote the checker.
It now ensures __all__ Python names are ASCII and also properly
checks the names of imports (``non-ascii-module-import``) as
well as file names (``non-ascii-file-name``) and emits their respective new warnings.

Non ASCII characters could be homoglyphs (look alike characters) and hard to
enter on a non specialized keyboard.
See `Confusable Characters in PEP 672 <https://www.python.org/dev/peps/pep-0672/#confusable-characters-in-identifiers>`_

Removed checkers
================
* ``non-ascii-name`` has replaced by ``non-ascii-identifier``
* ``non-ascii-name`` has been renamed to ``non-ascii-identifier``

Extensions
==========
Expand Down
2 changes: 1 addition & 1 deletion pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,7 @@ def _check_name(
raise NotImplementedError

def _recursive_check_names(self, args: Iterable[nodes.AssignName]):
"""check names in a possibly recursive list <arg>"""
"""Check names in a possibly recursive list <arg>"""
for arg in args:
if isinstance(arg, nodes.AssignName):
self._check_name("argument", arg.name, arg)
Expand Down
18 changes: 9 additions & 9 deletions pylint/checkers/non_ascii_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,27 @@ class NonAsciiNamesChecker(

msgs = {
"C2401": (
'%s name "%s" contains a non-ASCII character, rename it.',
'%s name "%s" contains a non-ASCII character, consider renaming it.',
"non-ascii-identifier",
(
"Used when the name contains at least one non-ASCII unicode character."
"Used when the name contains at least one non-ASCII unicode character. "
"See https://www.python.org/dev/peps/pep-0672/#confusable-characters-in-identifiers"
" for a background why this could be bad."
),
{"old_names": [("C0144", "non-ascii-name")]},
),
# First %s will always be "file"
"W2402": (
'%s name "%s" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers!',
'%s name "%s" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers.',
"non-ascii-file-name",
(
# Some = PyCharm at the time of writing didn't display the non_ascii_name_loł
# files. Probably only a bug shows the problem quite good.
# That's also why this is a warning and not only a convention!
"Some editors don't support non-ASCII file names properly. "
"Even so Python supports UTF-8 files since Python 3.5 this isn't recommended for"
"interoperability. Further reading: \n"
"- https://www.python.org/dev/peps/pep-0489/#export-hook-name \n"
"Even so Python supports UTF-8 files since Python 3.5 this isn't recommended for "
"interoperability. Further reading:\n"
"- https://www.python.org/dev/peps/pep-0489/#export-hook-name\n"
"- https://www.python.org/dev/peps/pep-0672/#confusable-characters-in-identifiers\n"
"- https://bugs.python.org/issue20485\n"
),
Expand Down Expand Up @@ -173,17 +173,17 @@ def visit_functiondef(
arguments = node.args

# Check position only arguments
if arguments.posonlyargs is not None:
if arguments.posonlyargs:
for pos_only_arg in arguments.posonlyargs:
self._check_name("argument", pos_only_arg.name, pos_only_arg)

# Check "normal" arguments
args = arguments.args
if args is not None:
if args:
self._recursive_check_names(args)

# Check key word only arguments
if arguments.kwonlyargs is not None:
if arguments.kwonlyargs:
for kwarg in arguments.kwonlyargs:
self._check_name("argument", kwarg.name, kwarg)

Expand Down
2 changes: 1 addition & 1 deletion tests/checkers/unittest_non_ascii_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def name(
"""
)
assert isinstance(node, nodes.FunctionDef)
arguments: nodes.Arguments = node.args
arguments = node.args

posargs = list(arguments.posonlyargs)
args = list(arguments.args)
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/e/.#emacs_file_lock_redefined_conf.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
invalid-name:1:0:None:None::"Module name ""#emacs_file_lock_redefined_conf"" doesn't conform to snake_case naming style":HIGH
non-ascii-file-name:1:0:None:None::"File name ""#emacs_file_lock_redefined_conf"" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers!":HIGH
non-ascii-file-name:1:0:None:None::"File name ""#emacs_file_lock_redefined_conf"" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers.":HIGH
4 changes: 2 additions & 2 deletions tests/functional/n/non/non_ascii_name.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
non-ascii-identifier:3:0:3:10::"Variable name ""áéíóú"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:5:0:6:12:úóíéá:"Function name ""úóíéá"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:3:0:3:10::"Variable name ""áéíóú"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:5:0:6:12:úóíéá:"Function name ""úóíéá"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Tests for non-ascii-name checker working as expected """
""" Tests for non-ascii-name checker working as expected after a major refactor """
# pylint: disable=C0144, use-symbolic-message-instead

áéíóú = 4444
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/n/non/non_ascii_name_backward_test_msg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Tests for non-ascii-name checker working as expected """
""" Tests for non-ascii-name checker working as expected after a major refactor """
# pylint: disable=non-ascii-name

áéíóú = 4444
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-file-name:1:0:None:None::"File name ""non_ascii_name_loł"" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers!":HIGH
non-ascii-file-name:1:0:None:None::"File name ""non_ascii_name_loł"" contains a non-ASCII character. PEP 3131 only allows non-ascii identifiers.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:3:4:3:8::"Variable name ""loł"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:3:4:3:8::"Variable name ""loł"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:10:8:10:12:main:"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:10:8:10:12:main:"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:13:0:15:28:sayНello:"Function name ""sayНello"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:13:0:15:28:sayНello:"Function name ""sayНello"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
non ascii variable defined in a function
This test is only for 3.8 as the starting column is incorrect
"""


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""
non ascii variable defined in a function
This test is 3.9+ and not using 'min_pyver_end_position'
as the starting column is also incorrect on < 3.9
"""


Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
non-ascii-identifier:9:4:9:13:okay:"Argument name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:21:4:21:12::"Argument name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:12:4:12:13:okay:"Argument name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:24:4:24:12::"Argument name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:7:8:7:12::"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:7:8:7:12::"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
Defining non ASCII variables in a function call
This test is only for 3.8 as the starting column is incorrect
"""


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""
Defining non ASCII variables in a function call
This test is 3.9+ and not using 'min_pyver_end_position'
as the starting column is also incorrect on < 3.9
"""


Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:14:4:14:10::"Argument name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:17:4:17:10::"Argument name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
2 changes: 1 addition & 1 deletion tests/functional/n/non_ascii_name/non_ascii_name_local.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:6:4:6:8:okay:"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:6:4:6:8:okay:"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
non-ascii-identifier:9:4:9:17:name:"Argument name ""not_okay_łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:10:4:10:21:name:"Argument name ""not_okay_defaułt"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:13:4:13:21:name:"Argument name ""p_or_kw_not_økay"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:16:4:16:20:name:"Argument name ""kw_arg_not_økay"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:9:4:9:17:name:"Argument name ""not_okay_łol"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:10:4:10:21:name:"Argument name ""not_okay_defaułt"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:13:4:13:21:name:"Argument name ""p_or_kw_not_økay"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:16:4:16:20:name:"Argument name ""kw_arg_not_økay"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:11:4:13:19:OkayClass.umlaut_ä:"Function name ""umlaut_ä"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:11:4:13:19:OkayClass.umlaut_ä:"Function name ""umlaut_ä"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:9:0:11:14::"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:9:0:11:14::"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
4 changes: 2 additions & 2 deletions tests/functional/n/non_ascii_name/non_ascii_name_variable.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
non-ascii-identifier:7:0:7:4::"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:9:0:9:4::"Variable name ""łol"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:7:0:7:4::"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
non-ascii-identifier:9:0:9:4::"Variable name ""łol"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:5:0:10:19:НoldIt:"Class name ""НoldIt"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:5:0:10:19:НoldIt:"Class name ""НoldIt"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:12:8:12:22:OkayIsh.__init__:"Attribute name ""łoopback"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:12:8:12:22:OkayIsh.__init__:"Attribute name ""łoopback"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:6:4:6:13:OkayIsh:"Attribute name ""ŁOOPBACK"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:6:4:6:13:OkayIsh:"Attribute name ""ŁOOPBACK"" contains a non-ASCII character, consider renaming it.":HIGH
Original file line number Diff line number Diff line change
@@ -1 +1 @@
non-ascii-identifier:12:4:14:19:OkayClass.umlaut_ä:"Function name ""umlaut_ä"" contains a non-ASCII character, rename it.":HIGH
non-ascii-identifier:12:4:14:19:OkayClass.umlaut_ä:"Function name ""umlaut_ä"" contains a non-ASCII character, consider renaming it.":HIGH

0 comments on commit 415a13b

Please sign in to comment.