diff --git a/README.md b/README.md index fc0fc23c..df2693ff 100644 --- a/README.md +++ b/README.md @@ -665,19 +665,6 @@ Availability: ``` -### remove unnecessary abspath - -Availability: -- `--py39-plus` is passed on the commandline. - -```diff - from os.path import abspath - --abspath(__file__) -+__file__ -``` - - ### pep 604 typing rewrites Availability: diff --git a/pyupgrade/_data.py b/pyupgrade/_data.py index 65d345d0..ff396381 100644 --- a/pyupgrade/_data.py +++ b/pyupgrade/_data.py @@ -43,7 +43,6 @@ class State(NamedTuple): RECORD_FROM_IMPORTS = frozenset(( '__future__', - 'os.path', 'functools', 'mmap', 'select', diff --git a/pyupgrade/_plugins/abspath_file.py b/pyupgrade/_plugins/abspath_file.py deleted file mode 100644 index 52316340..00000000 --- a/pyupgrade/_plugins/abspath_file.py +++ /dev/null @@ -1,53 +0,0 @@ -from __future__ import annotations - -import ast -from typing import Iterable - -from tokenize_rt import Offset -from tokenize_rt import Token - -from pyupgrade._ast_helpers import ast_to_offset -from pyupgrade._data import register -from pyupgrade._data import State -from pyupgrade._data import TokenFunc -from pyupgrade._token_helpers import find_closing_bracket -from pyupgrade._token_helpers import find_open_paren - - -def _remove_abspath(i: int, tokens: list[Token]) -> None: - paren_start = find_open_paren(tokens, i + 1) - paren_end = find_closing_bracket(tokens, paren_start) - while i <= paren_start: - tokens[i] = Token('PLACEHOLDER', '') - i += 1 - tokens[paren_end] = Token('PLACEHOLDER', '') - - -@register(ast.Call) -def visit_Call( - state: State, - node: ast.Call, - parent: ast.AST, -) -> Iterable[tuple[Offset, TokenFunc]]: - if ( - state.settings.min_version >= (3, 9) and - ( - ( - isinstance(node.func, ast.Name) and - node.func.id == 'abspath' and - node.func.id in state.from_imports['os.path'] - ) or - ( - isinstance(node.func, ast.Attribute) and - isinstance(node.func.value, ast.Attribute) and - isinstance(node.func.value.value, ast.Name) and - node.func.value.value.id == 'os' and - node.func.value.attr == 'path' and - node.func.attr == 'abspath' - ) - ) and - len(node.args) == 1 and - isinstance(node.args[0], ast.Name) and - node.args[0].id == '__file__' - ): - yield ast_to_offset(node), _remove_abspath diff --git a/tests/features/abspath_file_test.py b/tests/features/abspath_file_test.py deleted file mode 100644 index cd007067..00000000 --- a/tests/features/abspath_file_test.py +++ /dev/null @@ -1,54 +0,0 @@ -from __future__ import annotations - -import pytest - -from pyupgrade._data import Settings -from pyupgrade._main import _fix_plugins - - -@pytest.mark.parametrize( - ('s', 'expected'), - ( - pytest.param( - 'from os.path import abspath\n' - 'abspath(__file__)', - 'from os.path import abspath\n' - '__file__', - id='abspath', - ), - pytest.param( - 'import os\n' - 'os.path.abspath(__file__)', - 'import os\n' - '__file__', - id='os.path.abspath', - ), - ), -) -def test_fix_abspath_file(s, expected): - ret = _fix_plugins(s, settings=Settings(min_version=(3, 9))) - assert ret == expected - - -@pytest.mark.parametrize( - 's, min_version', - ( - pytest.param( - 'abspath(__file__)', - (3, 8), - id='Not Python3.9+', - ), - pytest.param( - 'os.path.abspath(file)', - (3, 9), - id='Abspath of not-__file__', - ), - pytest.param( - 'os.path.abspath(file, foo)', - (3, 9), - id='Garbage (don\'t rewrite)', - ), - ), -) -def test_fix_abspath_file_noop(s, min_version): - assert _fix_plugins(s, settings=Settings(min_version=min_version)) == s