https://github.com/isidentical/refactor Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Issues - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} isidentical / refactor * Notifications * Star 83 * Fork 1 Simple python source refactoring toolkit based on AST refactor.readthedocs.io View license 83 stars 1 fork Star Notifications * Code * Issues 5 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 3 branches 3 tags Code Clone HTTPS GitHub CLI [https://github.com/i] Use Git or checkout with SVN using the web URL. [gh repo clone isiden] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @isidentical isidentical context: prevent recursions in dependency resolution ... 6a10a3a Aug 1, 2021 context: prevent recursions in dependency resolution 6a10a3a Git stats * 63 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows gha: install dependencies on pre-commit hook Jul 31, 2021 docs tests: add complete rule tests Jul 31, 2021 examples examples: support overwrites for type literals example Jul 31, 2021 refactor context: prevent recursions in dependency resolution Aug 1, 2021 tests context: prevent recursions in dependency resolution Aug 1, 2021 .gitignore project: initial commit Jul 23, 2021 .pre-commit-config.yaml runner: introduce refactor hooks for pre-commit Jul 31, 2021 .pre-commit-hooks.yaml runner: introduce refactor hooks for pre-commit Jul 31, 2021 CODE_OF_CONDUCT.md project: add code of conduct Jul 31, 2021 LICENSE.txt project: add the basic layout Jul 23, 2021 README.md project: add pypi badge Jul 31, 2021 pyproject.toml core: implement the basic functionality Jul 23, 2021 refactors.py core: preserve indentation for multiline statements Jul 31, 2021 requirements-dev.txt tests: add tests about various components Jul 29, 2021 setup.cfg setup: release 0.3.0 Jul 31, 2021 setup.py project: enable --disallow-incomplete-defs for mypy Jul 24, 2021 View code Refactor Why? How? README.md Refactor PyPI version Simple, hassle-free, dependency-free, AST based source code refactoring toolkit. Why? How? refactor is an end-to-end refactoring framework that is built on top of the 'simple but effective refactorings' assumption. It is much easier to write a simple script with it rather than trying to figure out what sort of a regex you need in order to replace a pattern (if it is even matchable with regexes). Every refactoring rule offers a single entrypoint, match(), where they accept an AST node (from the ast module in the standard library) and respond with either returning an action to refactor or nothing. If the rule succeeds on the input, then the returned action will build a replacement node and refactor will simply replace the code segment that belong to the input with the new version. Here is a complete script that will replace every placeholder access with 42 (not the definitions) on the given list of files: import ast from refactor import Rule, ReplacementAction, run class Replace(Rule): def match(self, node): assert isinstance(node, ast.Name) assert node.id == 'placeholder' replacement = ast.Constant(42) return ReplacementAction(node, replacement) if __name__ == "__main__": run(rules=[Replace]) If we run this on a file, refactor will print the diff by default; --- test_file.py +++ test_file.py @@ -1,11 +1,11 @@ def main(): - print(placeholder * 3 + 2) - print(2 + placeholder + 3) + print(42 * 3 + 2) + print(2 + 42 + 3) # some commments - placeholder # maybe other comments + 42 # maybe other comments if something: other_thing - print(placeholder) + print(42) if __name__ == "__main__": main() As stated above, refactor's scope is usually small stuff, so if you want to do full program transformations we highly advise you to look at CST-based solutions like parso, LibCST and Fixit About Simple python source refactoring toolkit based on AST refactor.readthedocs.io Topics refactoring python ast Resources Readme License View license Releases 3 tags Packages 0 No packages published Contributors 2 * @isidentical isidentical Batuhan Taskaya * @sobolevn sobolevn Nikita Sobolev Languages * Python 100.0% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.