Skip to content

Commit

Permalink
Merge pull request #820 from asottile/drop-py37
Browse files Browse the repository at this point in the history
drop python 3.7
  • Loading branch information
asottile committed May 6, 2023
2 parents 7c551d3 + d9001d7 commit 62b19b2
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 81 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -10,10 +10,10 @@ jobs:
main-windows:
uses: asottile/workflows/.github/workflows/tox.yml@v1.0.0
with:
env: '["py37"]'
env: '["py38"]'
os: windows-latest
main-linux:
uses: asottile/workflows/.github/workflows/tox.yml@v1.0.0
with:
env: '["py37", "py38", "py39"]'
env: '["py38", "py39", "py310"]'
os: ubuntu-latest
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -17,7 +17,7 @@ repos:
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: [--py37-plus, --add-import, 'from __future__ import annotations']
args: [--py38-plus, --add-import, 'from __future__ import annotations']
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
hooks:
Expand All @@ -27,7 +27,7 @@ repos:
rev: v3.3.2
hooks:
- id: pyupgrade
args: [--py37-plus]
args: [--py38-plus]
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.2
hooks:
Expand Down
5 changes: 1 addition & 4 deletions pyupgrade/_plugins/six_calls.py
Expand Up @@ -2,7 +2,6 @@

import ast
import functools
import sys
from typing import Iterable

from tokenize_rt import Offset
Expand All @@ -21,10 +20,8 @@

_EXPR_NEEDS_PARENS: tuple[type[ast.expr], ...] = (
ast.Await, ast.BinOp, ast.BoolOp, ast.Compare, ast.GeneratorExp, ast.IfExp,
ast.Lambda, ast.UnaryOp,
ast.Lambda, ast.UnaryOp, ast.NamedExpr,
)
if sys.version_info >= (3, 8): # pragma: >=3.8 cover
_EXPR_NEEDS_PARENS += (ast.NamedExpr,)

SIX_CALLS = {
'u': '{args[0]}',
Expand Down
3 changes: 1 addition & 2 deletions pyupgrade/_plugins/unpack_list_comprehension.py
Expand Up @@ -12,11 +12,10 @@
from pyupgrade._data import State
from pyupgrade._data import TokenFunc
from pyupgrade._token_helpers import find_closing_bracket
from pyupgrade._token_helpers import find_comprehension_opening_bracket


def _replace_list_comprehension(i: int, tokens: list[Token]) -> None:
start = find_comprehension_opening_bracket(i, tokens)
start = i
end = find_closing_bracket(tokens, start)
tokens[start] = tokens[start]._replace(src='(')
tokens[end] = tokens[end]._replace(src=')')
Expand Down
39 changes: 5 additions & 34 deletions pyupgrade/_token_helpers.py
Expand Up @@ -2,7 +2,6 @@

import ast
import keyword
import sys
from typing import NamedTuple
from typing import Sequence

Expand Down Expand Up @@ -64,28 +63,11 @@ def find_end(tokens: list[Token], i: int) -> int:
return i


if sys.version_info >= (3, 8): # pragma: >=3.8 cover
# python 3.8 fixed the offsets of generators / tuples
def _arg_token_index(tokens: list[Token], i: int, arg: ast.expr) -> int:
idx = _search_until(tokens, i, arg) + 1
while idx < len(tokens) and tokens[idx].name in NON_CODING_TOKENS:
idx += 1
return idx
else: # pragma: <3.8 cover
def _arg_token_index(tokens: list[Token], i: int, arg: ast.expr) -> int:
# lists containing non-tuples report the first element correctly
if isinstance(arg, ast.List):
# If the first element is a tuple, the ast lies to us about its col
# offset. We must find the first `(` token after the start of the
# list element.
if isinstance(arg.elts[0], ast.Tuple):
i = _search_until(tokens, i, arg)
return find_open_paren(tokens, i)
else:
return _search_until(tokens, i, arg.elts[0])
# others' start position points at their first child node already
else:
return _search_until(tokens, i, arg)
def _arg_token_index(tokens: list[Token], i: int, arg: ast.expr) -> int:
idx = _search_until(tokens, i, arg) + 1
while idx < len(tokens) and tokens[idx].name in NON_CODING_TOKENS:
idx += 1
return idx


def victims(
Expand Down Expand Up @@ -499,17 +481,6 @@ def replace_argument(
tokens[start_idx:end_idx] = [Token('SRC', new)]


def find_comprehension_opening_bracket(i: int, tokens: list[Token]) -> int:
"""Find opening bracket of comprehension given first argument."""
if sys.version_info < (3, 8): # pragma: <3.8 cover
i -= 1
while not (tokens[i].name == 'OP' and tokens[i].src == '[') and i:
i -= 1
return i
else: # pragma: >=3.8 cover
return i


def has_space_before(i: int, tokens: list[Token]) -> bool:
return i >= 1 and tokens[i - 1].name in {UNIMPORTANT_WS, 'INDENT'}

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -20,7 +20,7 @@ classifiers =
packages = find:
install_requires =
tokenize-rt>=3.2.0
python_requires = >=3.7
python_requires = >=3.8

[options.packages.find]
exclude =
Expand Down
15 changes: 1 addition & 14 deletions tests/features/six_test.py
@@ -1,7 +1,5 @@
from __future__ import annotations

import sys

import pytest

from pyupgrade._data import Settings
Expand Down Expand Up @@ -387,25 +385,14 @@ def test_fix_six_noop(s):
'x = map(str, ints)\n',
id='six.moves builtin attrs',
),
),
)
def test_fix_six(s, expected):
ret = _fix_plugins(s, settings=Settings())
assert ret == expected


@pytest.mark.xfail(sys.version_info < (3, 8), reason='walrus')
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'for _ in six.itervalues(x := y): pass',
'for _ in (x := y).values(): pass',
id='needs parenthesizing for NamedExpr',
),
),
)
def test_fix_six_py38_plus(s, expected):
def test_fix_six(s, expected):
ret = _fix_plugins(s, settings=Settings())
assert ret == expected

Expand Down
32 changes: 11 additions & 21 deletions tests/features/typing_pep563_test.py
@@ -1,7 +1,5 @@
from __future__ import annotations

import sys

import pytest

from pyupgrade._data import Settings
Expand Down Expand Up @@ -338,27 +336,19 @@ def test_fix_typing_pep563_noop(s):
id='NamedTuple with no args (invalid syntax)',
),
pytest.param(
'from __future__ import annotations\n'
'def foo(var0, /, var1: "MyClass") -> "MyClass":\n'
' x: "MyClass"\n',
'from __future__ import annotations\n'
'def foo(var0, /, var1: MyClass) -> MyClass:\n'
' x: MyClass\n',
id='posonly args',
),
),
)
def test_fix_typing_pep563(s, expected):
ret = _fix_plugins(s, settings=Settings(min_version=(3, 7)))
assert ret == expected


@pytest.mark.xfail(
sys.version_info < (3, 8),
reason='posonly args not available in Python3.7',
)
def test_fix_typing_pep563_posonlyargs():
s = (
'from __future__ import annotations\n'
'def foo(var0, /, var1: "MyClass") -> "MyClass":\n'
' x: "MyClass"\n'
)
expected = (
'from __future__ import annotations\n'
'def foo(var0, /, var1: MyClass) -> MyClass:\n'
' x: MyClass\n'
)
ret = _fix_plugins(s, settings=Settings(min_version=(3, 8)))
assert ret == expected
2 changes: 1 addition & 1 deletion tox.ini
@@ -1,5 +1,5 @@
[tox]
envlist = py37,py38,py39,pypy3,pre-commit
envlist = py,pypy3,pre-commit

[testenv]
deps = -rrequirements-dev.txt
Expand Down

0 comments on commit 62b19b2

Please sign in to comment.