Skip to content

Commit

Permalink
Fix rename in place of first non-posonlyarg
Browse files Browse the repository at this point in the history
The first non-posonly arg in a method definition was mistakenly thought to be 'self' and eligible for renaming in place.
This change accounts for any posonly args when considering the 'first' argument to a method.

e.g arg will no longer be renamed in place:
```python
class MyClass
  def my_method(self, /, my_arg): pass
```
  • Loading branch information
dflook committed Oct 3, 2024
1 parent 176418b commit b57bf66
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions test/test_rename_locals.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,85 @@ def mymethod(C, arg, *A, **B): return A, B
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)


def test_rename_posargs_in_place():
if sys.version_info < (3, 8):
pytest.skip('No posonlyargs in python < 3.8')

source = '''
def test(arg, /, arg2): return arg, arg2
class TestClass():
def mymethod(self, arg, /, arg2): return arg, arg2
@classmethod
def mymethod(cls, arg, /, arg2): return arg, arg2
'''
expected = '''
def test(A,/,arg2):return A,arg2
class TestClass:
def mymethod(B,A,/,arg2):return A,arg2
@classmethod
def mymethod(B,A,/,arg2):return A,arg2
'''

expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)


def test_rename_kwargonlyargs_in_place():
if sys.version_info < (3, 0):
pytest.skip('No kwonlyargs in python < 3.0')

source = '''
def test(arg, arg2, *, arg3): return arg, arg2, arg3
class TestClass():
def mymethod(self, arg, arg2, *, arg3): return arg, arg2, arg3
@classmethod
def mymethod(cls, arg, arg2, *, arg3): return arg, arg2, arg3
'''
expected = '''
def test(arg,arg2,*,arg3):return arg,arg2,arg3
class TestClass:
def mymethod(A,arg,arg2,*,arg3):return arg,arg2,arg3
@classmethod
def mymethod(A,arg,arg2,*,arg3):return arg,arg2,arg3
'''

expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)


def test_rename_posargs_kwargonlyargs_in_place():
if sys.version_info < (3, 8):
pytest.skip('No posonlyargs in python < 3.8')

source = '''
def test(arg, /, arg2, *, arg3): return arg, arg2, arg3
class TestClass():
def mymethod(self, arg, /, arg2, *, arg3): return arg, arg2, arg3
@classmethod
def mymethod(cls, arg, /, arg2, *, arg3): return arg, arg2, arg3
'''
expected = '''
def test(A,/,arg2,*,arg3):return A,arg2,arg3
class TestClass:
def mymethod(B,A,/,arg2,*,arg3):return A,arg2,arg3
@classmethod
def mymethod(B,A,/,arg2,*,arg3):return A,arg2,arg3
'''

expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)


def test_no_rename_long_arg():
source = '''
def f(this_is_my_long_argument_name):
Expand Down

0 comments on commit b57bf66

Please sign in to comment.