Skip to content

Commit

Permalink
Fix issues with Python 3.6.0 (#4446)
Browse files Browse the repository at this point in the history
* tox: Enable testing python 3.6.0

The requirements_test.txt includes black, pre-commit, and pyupgrade
which are not compatible with Python 3.6.0. Add python_full_version
markers so that `tox -e py36` works with 3.6.0.

* Fix typing.Counter import for Python 3.6.0

typing.Counter was added in Python 3.6.1. In the strings checker,
guard the import with TYPE_CHECKING and use a type annotation comment
to fix usage with Python 3.6.0.

* Skip typing.NoReturn tests on Python < 3.6.2

typing.NoReturn was introduced in Python 3.6.2. Move the tests for
issue #4122 to a separate file and skip when Python is too old.

* Update ChangeLog and whatsnew
  • Loading branch information
pkolbus authored May 12, 2021
1 parent 9b268ec commit 318f1af
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 60 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ modules are added.

* Don't emit ``import-error`` if import guarded behind ``if sys.version_info >= (x, x)``

* Fix incompatibility with Python 3.6.0 caused by ``typing.Counter`` and ``typing.NoReturn`` usage

Closes #4412


What's New in Pylint 2.8.2?
===========================
Expand Down
2 changes: 2 additions & 0 deletions doc/whatsnew/2.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Other Changes
* The output messages for ``arguments-differ`` error message have been customized based on the different error cases.

* New option ``--fail-on=<msg ids>`` to return non-zero exit codes regardless of ``fail-under`` value.

* Fix incompatibility with Python 3.6.0 caused by ``typing.Counter`` and ``typing.NoReturn`` usage
8 changes: 6 additions & 2 deletions pylint/checkers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@
import numbers
import re
import tokenize
from typing import Counter, Iterable
from typing import TYPE_CHECKING, Iterable

import astroid

from pylint.checkers import BaseChecker, BaseTokenChecker, utils
from pylint.checkers.utils import check_messages
from pylint.interfaces import IAstroidChecker, IRawChecker, ITokenChecker

if TYPE_CHECKING:
from typing import Counter # typing.Counter added in Python 3.6.1

_AST_NODE_STR_TYPES = ("__builtin__.unicode", "__builtin__.str", "builtins.str")
# Prefixes for both strings and bytes literals per
# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Expand Down Expand Up @@ -750,7 +753,8 @@ def check_for_consistent_string_delimiters(
Args:
tokens: The tokens to be checked against for consistent usage.
"""
string_delimiters: Counter[str] = collections.Counter()
# typing.Counter added in Python 3.6.1 so this type hint must be a comment
string_delimiters = collections.Counter() # type: Counter[str]

# First, figure out which quote character predominates in the module
for tok_type, token, _, _, _ in tokens:
Expand Down
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-r requirements_test_min.txt
coveralls~=3.0
coverage~=5.5
pre-commit~=2.12
pre-commit~=2.12;python_full_version>="3.6.2"
pyenchant~=3.2
pytest-cov~=2.11
pytest-profiling~=1.7
Expand Down
4 changes: 2 additions & 2 deletions requirements_test_pre_commit.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
autoflake==1.4
black==21.5b1
black==21.5b1;python_full_version>="3.6.2"
flake8==3.9.2
isort==5.8.0
mypy==0.812
pyupgrade==2.15.0
pyupgrade==2.15.0;python_full_version>="3.6.1"
black-disable-checker==1.0.1
53 changes: 0 additions & 53 deletions tests/functional/i/inconsistent/inconsistent_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,59 +336,6 @@ def bug_pylint_3873_2():
nothing_to_do()
return False

import typing # pylint: disable=wrong-import-position

def parser_error(msg) -> typing.NoReturn: #pylint:disable=unused-argument
sys.exit(1)

def parser_error_nortype(msg): #pylint:disable=unused-argument
sys.exit(2)


from typing import NoReturn # pylint: disable=wrong-import-position

def parser_error_name(msg) -> NoReturn: #pylint:disable=unused-argument
sys.exit(3)

def bug_pylint_4122(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error('parser error')

def bug_pylint_4122_wrong(s): # [inconsistent-return-statements]
"""
Every returns is not consistent because parser_error_nortype has no type hints
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_nortype('parser error')

def bug_pylint_4122_bis(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_name('parser error')


# https://github.com/PyCQA/pylint/issues/4019
def bug_pylint_4019(x):
"""
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/i/inconsistent/inconsistent_returns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ inconsistent-return-statements:262:8:bug_1794_inner_func_in_if_counter_example_3
inconsistent-return-statements:267:0:bug_3468:Either all return statements in a function should return an expression, or none of them should.
inconsistent-return-statements:277:0:bug_3468_variant:Either all return statements in a function should return an expression, or none of them should.
inconsistent-return-statements:322:0:bug_pylint_3873_1:Either all return statements in a function should return an expression, or none of them should.
inconsistent-return-statements:366:0:bug_pylint_4122_wrong:Either all return statements in a function should return an expression, or none of them should.
inconsistent-return-statements:402:0:bug_pylint_4019_wrong:Either all return statements in a function should return an expression, or none of them should.
inconsistent-return-statements:349:0:bug_pylint_4019_wrong:Either all return statements in a function should return an expression, or none of them should.
55 changes: 55 additions & 0 deletions tests/functional/i/inconsistent/inconsistent_returns_noreturn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Testing inconsistent returns involving typing.NoReturn annotations."""
# pylint: disable=missing-docstring, invalid-name

import sys
import typing

def parser_error(msg) -> typing.NoReturn: # pylint: disable=unused-argument
sys.exit(1)

def parser_error_nortype(msg): # pylint: disable=unused-argument
sys.exit(2)


from typing import NoReturn # pylint: disable=wrong-import-position

def parser_error_name(msg) -> NoReturn: # pylint: disable=unused-argument
sys.exit(3)

def bug_pylint_4122(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error('parser error')

def bug_pylint_4122_wrong(s): # [inconsistent-return-statements]
"""
Every returns is not consistent because parser_error_nortype has no type hints
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_nortype('parser error')

def bug_pylint_4122_bis(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_name('parser error')
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[testoptions]
min_pyver=3.6.2

[REFACTORING]
never-returning-functions=sys.exit,sys.getdefaultencoding
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inconsistent-return-statements:32:0:bug_pylint_4122_wrong:Either all return statements in a function should return an expression, or none of them should.

0 comments on commit 318f1af

Please sign in to comment.