diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3b225b7..804dc78 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,10 +10,10 @@ jobs: main-windows: uses: asottile/workflows/.github/workflows/tox.yml@v1.6.0 with: - env: '["py38"]' + env: '["py39"]' os: windows-latest main-linux: uses: asottile/workflows/.github/workflows/tox.yml@v1.6.0 with: - env: '["py38", "py39", "py310", "py311", "py312"]' + env: '["py39", "py310", "py311", "py312"]' os: ubuntu-latest diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0fde7e9..e623da2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,7 +17,7 @@ repos: rev: v3.13.0 hooks: - id: reorder-python-imports - args: [--py38-plus, --add-import, 'from __future__ import annotations'] + args: [--py39-plus, --add-import, 'from __future__ import annotations'] - repo: https://github.com/asottile/add-trailing-comma rev: v3.1.0 hooks: @@ -26,7 +26,7 @@ repos: rev: v3.16.0 hooks: - id: pyupgrade - args: [--py38-plus] + args: [--py39-plus] - repo: https://github.com/hhatto/autopep8 rev: v2.3.1 hooks: diff --git a/add_trailing_comma/_ast_helpers.py b/add_trailing_comma/_ast_helpers.py index 4fbcb27..09759ee 100644 --- a/add_trailing_comma/_ast_helpers.py +++ b/add_trailing_comma/_ast_helpers.py @@ -13,13 +13,4 @@ def ast_parse(contents_text: str) -> ast.Module: def ast_to_offset(node: ast.AST) -> Offset: - candidates = [node] - while candidates: - candidate = candidates.pop() - if hasattr(candidate, 'lineno'): - return Offset(candidate.lineno, candidate.col_offset) - elif hasattr(candidate, '_fields'): # pragma: <3.9 cover - for field in reversed(candidate._fields): - candidates.append(getattr(candidate, field)) - else: - raise AssertionError(node) + return Offset(node.lineno, node.col_offset) diff --git a/add_trailing_comma/_data.py b/add_trailing_comma/_data.py index ea70efe..85cb82b 100644 --- a/add_trailing_comma/_data.py +++ b/add_trailing_comma/_data.py @@ -3,12 +3,10 @@ import ast import collections import pkgutil +from collections.abc import Iterable from typing import Callable -from typing import Iterable -from typing import List from typing import NamedTuple from typing import Protocol -from typing import Tuple from typing import TypeVar from tokenize_rt import Offset @@ -22,8 +20,8 @@ class State(NamedTuple): AST_T = TypeVar('AST_T', bound=ast.AST) -TokenFunc = Callable[[int, List[Token]], None] -ASTFunc = Callable[[State, AST_T], Iterable[Tuple[Offset, TokenFunc]]] +TokenFunc = Callable[[int, list[Token]], None] +ASTFunc = Callable[[State, AST_T], Iterable[tuple[Offset, TokenFunc]]] FUNCS = collections.defaultdict(list) diff --git a/add_trailing_comma/_main.py b/add_trailing_comma/_main.py index b09cd1b..f50ed8f 100644 --- a/add_trailing_comma/_main.py +++ b/add_trailing_comma/_main.py @@ -2,8 +2,8 @@ import argparse import sys -from typing import Iterable -from typing import Sequence +from collections.abc import Iterable +from collections.abc import Sequence from tokenize_rt import src_to_tokens from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/_with.py b/add_trailing_comma/_plugins/_with.py index ffbaf81..f673d73 100644 --- a/add_trailing_comma/_plugins/_with.py +++ b/add_trailing_comma/_plugins/_with.py @@ -1,8 +1,7 @@ from __future__ import annotations import ast -import sys -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token @@ -15,20 +14,20 @@ from add_trailing_comma._token_helpers import fix_brace -if sys.version_info >= (3, 9): # pragma: >=3.9 cover - def _fix_with(i: int, tokens: list[Token]) -> None: +def _fix_with(i: int, tokens: list[Token]) -> None: + i += 1 + if tokens[i].name == 'UNIMPORTANT_WS': i += 1 - if tokens[i].name == 'UNIMPORTANT_WS': - i += 1 - if tokens[i].src == '(': - fix = find_simple(i, tokens) - # only fix if outer parens are for the with items (next is ':') - if fix is not None and tokens[fix.braces[-1] + 1].src == ':': - fix_brace(tokens, fix, add_comma=True, remove_comma=True) + if tokens[i].src == '(': + fix = find_simple(i, tokens) + # only fix if outer parens are for the with items (next is ':') + if fix is not None and tokens[fix.braces[-1] + 1].src == ':': + fix_brace(tokens, fix, add_comma=True, remove_comma=True) - @register(ast.With) - def visit_With( - state: State, - node: ast.With, - ) -> Iterable[tuple[Offset, TokenFunc]]: - yield ast_to_offset(node), _fix_with + +@register(ast.With) +def visit_With( + state: State, + node: ast.With, +) -> Iterable[tuple[Offset, TokenFunc]]: + yield ast_to_offset(node), _fix_with diff --git a/add_trailing_comma/_plugins/calls.py b/add_trailing_comma/_plugins/calls.py index 933daf9..bd707dd 100644 --- a/add_trailing_comma/_plugins/calls.py +++ b/add_trailing_comma/_plugins/calls.py @@ -2,7 +2,7 @@ import ast import functools -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/classes.py b/add_trailing_comma/_plugins/classes.py index ac54804..0d423e9 100644 --- a/add_trailing_comma/_plugins/classes.py +++ b/add_trailing_comma/_plugins/classes.py @@ -2,7 +2,7 @@ import ast import functools -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/functions.py b/add_trailing_comma/_plugins/functions.py index 827a2ce..b6be398 100644 --- a/add_trailing_comma/_plugins/functions.py +++ b/add_trailing_comma/_plugins/functions.py @@ -2,7 +2,7 @@ import ast import functools -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/imports.py b/add_trailing_comma/_plugins/imports.py index f7ff9a9..ecfa69a 100644 --- a/add_trailing_comma/_plugins/imports.py +++ b/add_trailing_comma/_plugins/imports.py @@ -1,7 +1,7 @@ from __future__ import annotations import ast -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/literals.py b/add_trailing_comma/_plugins/literals.py index 1106461..5f07885 100644 --- a/add_trailing_comma/_plugins/literals.py +++ b/add_trailing_comma/_plugins/literals.py @@ -2,7 +2,7 @@ import ast import functools -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import NON_CODING_TOKENS from tokenize_rt import Offset @@ -91,7 +91,7 @@ def _fix_tuple_py38( tokens: list[Token], *, one_el_tuple: bool, -) -> None: # pragma: >=3.8 cover +) -> None: fix = find_simple(i, tokens) # for tuples we *must* find a comma, otherwise it is not a tuple diff --git a/add_trailing_comma/_plugins/match.py b/add_trailing_comma/_plugins/match.py index e35ed95..a439016 100644 --- a/add_trailing_comma/_plugins/match.py +++ b/add_trailing_comma/_plugins/match.py @@ -3,7 +3,7 @@ import ast import functools import sys -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/add_trailing_comma/_plugins/pep695.py b/add_trailing_comma/_plugins/pep695.py index 46fc639..0f4f170 100644 --- a/add_trailing_comma/_plugins/pep695.py +++ b/add_trailing_comma/_plugins/pep695.py @@ -2,7 +2,7 @@ import ast import sys -from typing import Iterable +from collections.abc import Iterable from tokenize_rt import Offset from tokenize_rt import Token diff --git a/setup.cfg b/setup.cfg index ecad5b0..c495f24 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,7 +20,7 @@ classifiers = packages = find: install_requires = tokenize-rt>=3.0.1 -python_requires = >=3.8 +python_requires = >=3.9 [options.packages.find] exclude =