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

Fix issues with Python 3.6.0 #4446

Merged
merged 4 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
pkolbus marked this conversation as resolved.
Show resolved Hide resolved

# 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"
Copy link
Member

@cdce8p cdce8p May 12, 2021

Choose a reason for hiding this comment

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

Why not use just python_version? Same in requirements_test_pre_commit.txt.
https://www.python.org/dev/peps/pep-0496/#version-numbers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

platform.python_version() is a dotted string, so according to that PEP python_version doesn’t include the micro part.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, didn't know that

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.