Table of Contents
Fetching ...

Refactoring to Pythonic Idioms: A Hybrid Knowledge-Driven Approach Leveraging Large Language Models

Zejun Zhang, Zhenchang Xing, Xiaoxue Ren, Qinghua Lu, Xiwei Xu

TL;DR

This work tackles the challenge of refactoring non-idiomatic Python code into Pythonic idioms by proposing a hybrid, knowledge-driven approach that leverages Analytic Rule Interfaces (ARIs) in tandem with Large Language Models (LLMs). The architecture comprises a Knowledge Module (idiom library and three-element model: ASTscenario, ASTcomponent, Condition), an Extraction Module (ARIs to detect candidate code patterns), and an Idiomatization Module (three-step process: abstract, idiomatize, rewrite). Empirical evaluation against RIdiom and Prompt-LLM on nine established idioms shows superior accuracy, F1-score, and recall, with precision on par with the rule-based baseline; four new idioms demonstrate strong scalability with all metrics above 90%. The replication package and scalability results suggest the method generalizes beyond the initial idioms, enabling robust, extensible Pythonic idiom refactoring in practice and offering a pathway for further integration of rule-based rigor with adaptive LLM reasoning.

Abstract

Pythonic idioms are highly valued and widely used in the Python programming community. However, many Python users find it challenging to use Pythonic idioms. Adopting a rule-based approach or LLM-only approach is not sufficient to overcome three persistent challenges of code idiomatization including code miss, wrong detection and wrong refactoring. Motivated by the determinism of rules and adaptability of LLMs, we propose a hybrid approach consisting of three modules. We not only write prompts to instruct LLMs to complete tasks, but we also invoke Analytic Rule Interfaces (ARIs) to accomplish tasks. The ARIs are Python code generated by prompting LLMs to generate code. We first construct a knowledge module with three elements including ASTscenario, ASTcomponent and Condition, and prompt LLMs to generate Python code for incorporation into an ARI library for subsequent use. After that, for any syntax-error-free Python code, we invoke ARIs from the ARI library to extract ASTcomponent from the ASTscenario, and then filter out ASTcomponent that does not meet the condition. Finally, we design prompts to instruct LLMs to abstract and idiomatize code, and then invoke ARIs from the ARI library to rewrite non-idiomatic code into the idiomatic code. Next, we conduct a comprehensive evaluation of our approach, RIdiom, and Prompt-LLM on nine established Pythonic idioms in RIdiom. Our approach exhibits superior accuracy, F1-score, and recall, while maintaining precision levels comparable to RIdiom, all of which consistently exceed or come close to 90% for each metric of each idiom. Lastly, we extend our evaluation to encompass four new Pythonic idioms. Our approach consistently outperforms Prompt-LLM, achieving metrics with values consistently exceeding 90% for accuracy, F1-score, precision, and recall.

Refactoring to Pythonic Idioms: A Hybrid Knowledge-Driven Approach Leveraging Large Language Models

TL;DR

This work tackles the challenge of refactoring non-idiomatic Python code into Pythonic idioms by proposing a hybrid, knowledge-driven approach that leverages Analytic Rule Interfaces (ARIs) in tandem with Large Language Models (LLMs). The architecture comprises a Knowledge Module (idiom library and three-element model: ASTscenario, ASTcomponent, Condition), an Extraction Module (ARIs to detect candidate code patterns), and an Idiomatization Module (three-step process: abstract, idiomatize, rewrite). Empirical evaluation against RIdiom and Prompt-LLM on nine established idioms shows superior accuracy, F1-score, and recall, with precision on par with the rule-based baseline; four new idioms demonstrate strong scalability with all metrics above 90%. The replication package and scalability results suggest the method generalizes beyond the initial idioms, enabling robust, extensible Pythonic idiom refactoring in practice and offering a pathway for further integration of rule-based rigor with adaptive LLM reasoning.

Abstract

Pythonic idioms are highly valued and widely used in the Python programming community. However, many Python users find it challenging to use Pythonic idioms. Adopting a rule-based approach or LLM-only approach is not sufficient to overcome three persistent challenges of code idiomatization including code miss, wrong detection and wrong refactoring. Motivated by the determinism of rules and adaptability of LLMs, we propose a hybrid approach consisting of three modules. We not only write prompts to instruct LLMs to complete tasks, but we also invoke Analytic Rule Interfaces (ARIs) to accomplish tasks. The ARIs are Python code generated by prompting LLMs to generate code. We first construct a knowledge module with three elements including ASTscenario, ASTcomponent and Condition, and prompt LLMs to generate Python code for incorporation into an ARI library for subsequent use. After that, for any syntax-error-free Python code, we invoke ARIs from the ARI library to extract ASTcomponent from the ASTscenario, and then filter out ASTcomponent that does not meet the condition. Finally, we design prompts to instruct LLMs to abstract and idiomatize code, and then invoke ARIs from the ARI library to rewrite non-idiomatic code into the idiomatic code. Next, we conduct a comprehensive evaluation of our approach, RIdiom, and Prompt-LLM on nine established Pythonic idioms in RIdiom. Our approach exhibits superior accuracy, F1-score, and recall, while maintaining precision levels comparable to RIdiom, all of which consistently exceed or come close to 90% for each metric of each idiom. Lastly, we extend our evaluation to encompass four new Pythonic idioms. Our approach consistently outperforms Prompt-LLM, achieving metrics with values consistently exceeding 90% for accuracy, F1-score, precision, and recall.
Paper Structure (27 sections, 2 equations, 8 figures, 4 tables)

This paper contains 27 sections, 2 equations, 8 figures, 4 tables.

Figures (8)

  • Figure 1: Motivating examples
  • Figure 2: Approach overview
  • Figure 3: ARI library built by prompting LLMs to generate code
  • Figure 4: Examples of extraction module
  • Figure 5: Examples of idiomatization module
  • ...and 3 more figures