Skip to content

Commit

Permalink
correctly compute indent for typeddict after dedent
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Aug 2, 2021
1 parent 42be346 commit a9a451c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyupgrade/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ def _typed_class_replacement(
call: ast.Call,
types: Dict[str, ast.expr],
) -> Tuple[int, str]:
while i > 0 and tokens[i - 1].name == 'DEDENT':
i -= 1
if i > 0 and tokens[i - 1].name in {'INDENT', UNIMPORTANT_WS}:
indent = f'{tokens[i - 1].src}{" " * 4}'
else:
Expand Down
16 changes: 16 additions & 0 deletions tests/features/typing_typed_dict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ def test_typing_typed_dict_noop(s):
id='index unparse error',
),
pytest.param(
'import typing\n'
'if True:\n'
' if False:\n'
' pass\n'
' D = typing.TypedDict("D", a=int)\n',
'import typing\n'
'if True:\n'
' if False:\n'
' pass\n'
' class D(typing.TypedDict):\n'
' a: int\n',
id='right after a dedent',
),
),
)
def test_typing_typed_dict(s, expected):
Expand Down

0 comments on commit a9a451c

Please sign in to comment.