Skip to content

Commit

Permalink
Fix non-idempotence the slow way
Browse files Browse the repository at this point in the history
I'm hoping to instead get this fixed upstream with PyCQA/autoflake#106 or PyCQA/isort#1913
  • Loading branch information
Zac-HD committed Mar 19, 2022
1 parent 66f9e59 commit a05685e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/shed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def shed(
if refactor:
source_code = _run_codemods(source_code, min_version=min_version)

before_import_fixes = source_code
source_code = autoflake.fix_code(
source_code,
expand_star_imports=True,
Expand All @@ -186,6 +187,20 @@ def shed(
except FileSkipComment:
pass

if source_code != before_import_fixes:
# isort, autoflake, and pyupgrade can each make changes that unlock the others.
# We therefore just re-run the whole stack if we've changed any imports,
# internally iterating to a fixpoint so that we're idempotent - but without
# paying that performance cost in the common case where we don't need to.
return shed(
source_code,
refactor=refactor,
first_party_imports=first_party_imports,
min_version=min_version,
_location=_location,
_remove_unused_imports=_remove_unused_imports,
)

if source_code != blackened:
source_code = black.format_str(source_code, mode=black_mode)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_shed.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def check(
)
@example(source_code=TEYIT_TWO_PASS, **example_kwargs)
@example(source_code="class A:\n\x0c pass\n", **example_kwargs)
@example(source_code="from.import(A)#", **example_kwargs)
@example(source_code="import a #", **example_kwargs)
@settings(suppress_health_check=HealthCheck.all(), deadline=None)
def test_shed_is_idempotent(source_code, refactor, provides, min_version):
check(source_code, refactor=refactor, min_version=min_version, provides=provides)
Expand Down

0 comments on commit a05685e

Please sign in to comment.