Add transforms to remove unused imports, variables, definitions and unreachable code - #156
Open
SahilRajputdotexe wants to merge 1 commit into
Open
SahilRajputdotexe wants to merge 1 commit into
SahilRajputdotexe wants to merge 1 commit into
Conversation
…nreachable code Each is an option that is disabled by default. They are repeated until no more code is removed, as removing code can leave other code unused.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds four opt-in transforms that remove dead code:
remove_unused_imports(--remove-unused-imports): drops imported names that are never usedremove_unused_variables(--remove-unused-variables): dropsx = valuewhenxis never used and the value has no side effectsremove_unused_definitions(--remove-unused-definitions): drops functions and classes that are never usedremove_unreachable(--remove-unreachable): drops statements after areturn,raise,breakorcontinueThey are off by default, because they can break code that is imported by something else or that relies on import side effects. Modules that use
exec,eval,locals,globals,varsor a star import are left alone, and so are names in a literal__all__, dunder names, decorated definitions and class bodies.Removing code can leave more unused, so the transforms repeat until nothing changes. For example, removing an unused
helper()also removes theimport jsonthat only it used.Unreachable code is only removed if that doesn't change how names resolve. A
returnfollowed by ayieldstays, and so does a trailing assignment to a name that is read earlier in the function. Awithbody that only ends by raising doesn't count, because the context manager can swallow the exception.There is a docs page for each option, a changelog entry and the stub arguments. The new tests pass on 3.8, 3.9, 3.11, 3.12 and 3.14 locally along with the existing suite, and I ran all four options over the standard library to check the output still compiles. The 2.7 and 3.3+ jobs run on CI.