{"id":13771609,"url":"https://github.com/isidentical/refactor","last_synced_at":"2025-05-16T11:05:55.887Z","repository":{"id":40921996,"uuid":"388921656","full_name":"isidentical/refactor","owner":"isidentical","description":"AST-based fragmental source code refactoring toolkit for Python","archived":false,"fork":false,"pushed_at":"2023-12-30T17:03:25.000Z","size":301,"stargazers_count":444,"open_issues_count":26,"forks_count":17,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-05-10T10:37:56.858Z","etag":null,"topics":["ast","python","refactoring"],"latest_commit_sha":null,"homepage":"https://refactor.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/isidentical.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-07-23T20:42:14.000Z","updated_at":"2025-04-24T15:14:46.000Z","dependencies_parsed_at":"2024-01-12T21:19:59.718Z","dependency_job_id":"c78a4ecf-86a3-4135-b38b-e9244b47446e","html_url":"https://github.com/isidentical/refactor","commit_stats":{"total_commits":192,"total_committers":6,"mean_commits":32.0,"dds":"0.38541666666666663","last_synced_commit":"609a5b2011b25bcba177a0132865f2bb49e369d3"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isidentical%2Frefactor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isidentical%2Frefactor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isidentical%2Frefactor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isidentical%2Frefactor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isidentical","download_url":"https://codeload.github.com/isidentical/refactor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254518383,"owners_count":22084374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","python","refactoring"],"created_at":"2024-08-03T17:00:53.380Z","updated_at":"2025-05-16T11:05:50.878Z","avatar_url":"https://github.com/isidentical.png","language":"Python","readme":"# Refactor\n\n[![PyPI version](https://badge.fury.io/py/refactor.svg)](https://badge.fury.io/py/refactor)\n[![Documentation](https://img.shields.io/badge/%3D%3E-Documentation-brightgreen)](https://refactor.readthedocs.io)\n[![Try It](https://img.shields.io/badge/Try%20It-getrefactor.com-green?style=for-the-badge)](https://getrefactor.com)\n\nSimple, hassle-free, dependency-free, AST based fragmental source code refactoring\nand transformation toolkit.\n\n## Why?\n\nOur framework is primarily built on the principle of \"simple but effective\ntransformations\". We focus on refactorings that target a small span of\nsource code, and work our way out from it. What this enables for us is\nbeing able to operate directly on a single format for both analyses and\ntransformations. This is what we shine at compared to other similar tools.\n\n## How?\n\nLet's not get into too much details, but just to give a sneak peek we\ncan try to write a rule that would replace the identifier `placeholder`\nwith `42`.\n\n```py\nimport ast\nfrom refactor import Rule, Replace, run\n\n# Each refactor transformer inherits from \"refactor.Rule\"\nclass FillPlaceholders(Rule):\n\n    # And each rule implements a \"match()\" method, which would\n    # receive every node in the tree in a breadth-first order.\n    def match(self, node: ast.AST) -\u003e Replace:\n        # This is where things get interesting. Instead of just writing\n        # filters with if statements, you can use the following assert\n        # based approach (a contract of transformation).\n\n        # For this case, our contract is going to be: if the given node\n        # is an identifier with the name of \"placeholder\", it will be\n        # replaced with literal \"42\".\n        assert isinstance(node, ast.Name)\n        assert node.id == \"placeholder\"\n\n        # And this is where we choose what action we are taking for the\n        # given node (which we have verified with our contract). There\n        # are multiple transformation actions, but in this case what we\n        # need is something that replaces a node with another one.\n        replacement = ast.Constant(42)\n        return Replace(node, replacement)\n\nif __name__ == \"__main__\":\n    # And finally in here, we just use the default CLI that comes\n    # bundled with refactor. When provided with a bunch of rules,\n    # it creates a simple interface that can process given files\n    # show the diff for changes and even apply them.\n    run(rules=[FillPlaceholders])\n```\n\nIf we run the rule above on a file, we can see how it performs:\n\n```diff\n--- test_file.py\n+++ test_file.py\n\n@@ -1,11 +1,11 @@\n\ndef main():\n-    print(placeholder * 3 + 2)\n+    print(42 * 3 + 2)\n-    print(2 +               placeholder      + 3)\n+    print(2 +               42      + 3)\n     # some comments\n-    placeholder # maybe other comments\n+    42 # maybe other comments\n     if something:\n         other_thing\n-    print(placeholder)\n+    print(42)\n\nif __name__ == \"__main__\":\n     main()\n```\n\nFor learning more, check our [documentation](https://refactor.readthedocs.io/en/latest/) out!\n","funding_links":[],"categories":["Libraries and refactoring","Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisidentical%2Frefactor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisidentical%2Frefactor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisidentical%2Frefactor/lists"}