A training and inference method for python repository-level complex type inference

By constructing types using semantic graphs and multi-round refined training, combined with reinforcement learning and self-consistent decoding, the problems of local misjudgment and consistency in complex type inference at the Python repository level are solved, achieving more accurate and consistent complex type inference.

CN122285012APending Publication Date: 2026-06-26NANJING UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
NANJING UNIV
Filing Date
2026-04-10
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

Existing technologies struggle to fully utilize inter-procedural call relationships and cross-module object state information in Python repository-level complex type inference, lack repository-level consistency constraints, and single-round generation and single-sample greedy decoding are prone to local misjudgments and error propagation.

Method used

By constructing local semantic contexts using semantic graphs based on types, and combining supervised fine-tuning, verifiable structured reward reinforcement learning, and hierarchical multi-round refinement training, the model's ability to utilize semantic evidence and make structured inferences for complex types is improved. Warehouse-level scheduling verification and type space self-consistent decoding are adopted to form a closed loop of generation-verification-regeneration.

Benefits of technology

It significantly improves the accuracy and engineering-level consistency of complex type inference, reduces the difficulty of credit allocation in training of deeply nested types, improves the consistency and deployability of type completion, and reduces the impact of single sampling errors on the results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122285012A_ABST
    Figure CN122285012A_ABST
Patent Text Reader

Abstract

This invention discloses a training and inference method for complex type inference at the Python repository level. It constructs a semantic graph of types; organizes type semantic contexts and constructs single-round type inference training samples, performing supervised fine-tuning on a large language model to obtain a single-round type inference base model; represents the predicted type and reference type as a type tree, constructing verifiable structured rewards; constructs multi-round refinement objectives according to the depth of the reference type, aggregating round-by-round refinement rewards, termination penalties, and final result rewards to obtain a hierarchical reinforcement learning model; constructs a repository-level scheduling order; for the multi-round refinement process, in each round, multiple candidate outputs are mapped to a depth-constrained abstract type space, and the stage decoding result is obtained through type space self-consistency aggregation, outputting the final predicted type. This invention can simultaneously enhance the cross-process semantic utilization capability, deep structure inference capability, and repository-level type consistency in complex type inference.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to a training and inference method for complex type inference at the Python repository level, belonging to the fields of code intelligence, program analysis, large language models, and machine learning. Background Technology

[0002] Python type inference aims to automatically complete the type information of variables, function parameters, function return values, and class attributes in the event of missing type annotations, ensuring that the information conforms to the semantics of the program. As large open-source repositories continue to grow in size, type inference problems in real-world projects are no longer limited to local statement-level pattern matching, but increasingly rely on repository-level call relationships, inter-procedural data flow, object state usage, and constraint propagation between complex generic structures.

[0003] Existing methods suffer from the following main shortcomings. First, many methods rely solely on file-level local context, making it difficult to fully utilize inter-procedural call relationships and cross-module object state information. Therefore, they are prone to failure in scenarios involving parameter types, return value types, and deeply nested complex types. Second, while existing generative methods can directly output type expressions from open vocabularies, they often lack repository-level consistency constraints, easily leading to locally valid but globally conflicting predictions. Third, when relying solely on supervised fine-tuning, the model primarily learns the surface form of the reference answer and existing inference trajectories, lacking direct optimization signals for layer-by-layer inference of the recursive structure within complex types. Fourth, when dealing with complex nested types such as Dict[str, List[User]], single-round generation and single-sample greedy decoding are prone to local misjudgments at a certain level, continuously propagating errors into subsequent inference processes. Summary of the Invention

[0004] Purpose of the Invention: Based on the problems and shortcomings of existing technologies, there is an urgent need for a unified method that can simultaneously cover repository-level semantic modeling, structured and verifiable training, hierarchical reinforcement learning, and multi-round self-consistent inference, thereby improving the accuracy, structural inference capability, and engineering-level consistency of Python complex type inference. This invention provides a training and inference method for Python repository-level complex type inference. On the one hand, this method improves the semantic evidence utilization capability and engineering-level consistency of complex type inference through type usage semantic graphs, target slot local semantic context construction, and static type checking feedback mechanisms. On the other hand, it enhances the model's structured inference capability for deep complex types through supervised fine-tuning, verifiable structured reward reinforcement learning, hierarchical multi-round refinement training, and type space self-consistent decoding.

[0005] Technical solution: A training and inference method for complex type inference at the Python repository level, including the following steps: A training and inference method for Python repository-level complex type inference, characterized by the following steps: 1) Obtain the code repository to be processed, identify the parameter, return, and variable type slots, and construct a type semantic graph; 2) Around the target type slot, retrieve the consumer-side semantic evidence and dependency-side semantic evidence of the target element from the semantic graph of the type to form the local semantic context of the target slot; 3) During the training phase, single-type inference training samples are constructed based on the local semantic context, and the large language model is fine-tuned under supervision to obtain the single-type inference base model; 4) Represent the predicted type and reference type as a type tree, construct a verifiable structured reward that includes the correctness of the type structure and the correctness of the output format, and perform reinforcement learning training on the single type inference base model based on GRPO; 5) Model complex type inference as a multi-round refinement process according to the hierarchical depth of the reference type, construct the phased target type, the refinement reward for each round, the termination penalty and the final result reward, and conduct hierarchical reinforcement learning training based on GRPO; 6) During the inference phase, a warehouse-level scheduling order is constructed based on the dependencies between the elements to be inferred, and iterative scheduling, candidate generation, local verification, global verification, rollback and feedback updates are performed on unresolved slots; 7) For each round of the multi-round refinement inference process, multiple candidate outputs are sampled in parallel from the current strategy, the candidate outputs are mapped to a depth-limited abstract type space, and the stage decoding result is obtained through type space self-consistency aggregation until the termination condition is met and the final predicted type is output.

[0006] In step 1), lexical, syntactic, and symbolic level parsing is performed on the code repository to be processed to identify program entities such as modules, functions, classes, local variables, parameters, return values, class attributes, and object states, and the positions of missing type annotations are registered as slots to be inferred; a type usage semantic graph is constructed based on cross-file call relationships, parameter passing relationships, return value usage relationships, object state propagation relationships between member functions, protocol method matching relationships, and object shape dependency relationships, and entity categories, modules to which they belong, scopes, associated code positions, and deterministic markers are recorded on nodes and edges to form a repository-level semantic carrier that can be used for constraint propagation, evidence retrieval, and subsequent scheduling.

[0007] In step 2), for each slot to be inferred, consumer-side semantic evidence and dependency-side semantic evidence are retrieved from the type semantic graph, centered on the element containing the slot. Consumer-side semantic evidence describes which downstream code will consume the predicted type of the slot, while dependency-side semantic evidence describes the interface, state, and auxiliary logic constraints that the element to which the slot belongs needs to satisfy during implementation. Subsequently, candidate evidence is filtered based on evidence certainty, graph distance to the target slot, whether it is located in the same module or the same call path, and the input budget. The retained evidence is organized into a structured local semantic context in the order of lead segment, dependency segment, target body segment, and consumer segment, thereby prioritizing the retention of repository-level semantic information most relevant to complex type inference within a limited input length.

[0008] In step 3), the local semantic context, target slot identifier, and reference type answer are organized into single-time type inference training samples and constructed into an instruction-following task that requires the large language model to output structured type results. The large language model is trained using supervised fine-tuning so that it learns to generate reference type answers for the target slot under the given warehouse-level local semantic context, thus obtaining a single-time type inference base model and simultaneously acquiring basic type inference capabilities, structured output capabilities, and step-by-step reasoning capabilities for complex types.

[0009] In step 4), the reference type and the model-predicted type are represented as type trees, respectively. The maximum matching depth and overall depth deviation between the two in the depth-truncated representation are calculated, and a continuous type-level matching score is constructed accordingly. Combined with checks on the completeness of the structured output fields, the parsability of the type string, and the satisfaction of format constraints, a verifiable structured reward for a single type inference is formed. During training, multiple candidate outputs are sampled in parallel for the same input. Based on the reward corresponding to each candidate output, a relative advantage within the group is constructed, and a group-relative strategy is used to optimize and update the model parameters to improve the probability of candidate outputs with more correct structure and more stable format.

[0010] In step 5), for the complex nested types recognized in the field, the inference process is decomposed into multiple rounds of refinement tasks according to the reference type depth. In each round, a phased target type is constructed, and the structural depth of the confirmed inference is maintained. The refinement reward for each round is calculated based on whether the current round prediction has achieved the phased target, whether effective hierarchical advancement has occurred, and whether stagnation or cross-layer jump has occurred. Then, a termination penalty is constructed based on the difference between the actual number of termination rounds and the ideal number of termination rounds. Combined with the structured reward of the final round, the total reward of the multi-round trajectory is obtained, which is used to optimize the model's ability to expand, correct, and terminate complex types layer by layer.

[0011] In step 6), for slots that are still unresolved during the inference phase, a repository-level scheduling order is constructed based on the call dependencies, object state dependencies, protocol dependencies, and return value propagation dependencies between the elements to which the unresolved slots belong. After compression of strongly connected components, the inference priorities at the component level, element level, and slot level are determined. After generating candidate types, the currently visible prediction results are written back to the temporary type signature environment, and the static type checker is called to sequentially perform local verification within the vicinity of the target element and global verification within the entire repository. If a new diagnosis occurs, the affected slot set is located, the relevant slots are rolled back, and the diagnosis summary is written to the feedback field to drive the next round of regeneration. If the verification passes, the slot status is promoted and the stable slots are frozen.

[0012] In step 7), for each round of multi-round refinement, multiple candidate outputs are sampled in parallel under the same context. The prediction type obtained by parsing the candidate outputs is mapped to the abstract type space with limited depth in the current round. An empirical distribution is formed on the abstract type space, and the mode of the empirical distribution is used as the decoding result of the current round stage. When the proportion of termination markers in the candidate outputs reaches a threshold, the decoding result of the current round stage no longer produces effective depth advancement relative to the previous round, or the current round reaches the maximum number of rounds, the multi-round refinement process is terminated, and self-consistent aggregation is performed on multiple candidate prediction types in the complete type space to output the final prediction type.

[0013] In the above steps, steps 1) and 2) are used to construct a warehouse-level semantic input foundation shared by training and inference; steps 3) to 5) are used to complete the phased training of the model and enhance its ability to infer complex type structures; and steps 6) and 7) are used to execute inference and aggregation decisions constrained by static verification in a real warehouse environment. By unifying warehouse-level semantic modeling, structured verifiable training, hierarchical reinforcement learning, and self-consistent inference into the same technical chain, this invention can simultaneously improve the semantic evidence utilization capability, deep structure inference capability, and warehouse-level consistency of complex type inference.

[0014] As a second aspect of this application, a training and inference system for Python repository-level complex type inference is also disclosed, including a semantic graph construction module, a context construction module, a base training module, a verifiable reward training module, a hierarchical refinement training module, a repository-level scheduling verification module, and a type space self-consistency decoding module.

[0015] The semantic graph construction module performs lexical, syntactic, and symbolic-level analysis on the code repository to be processed, identifies program entities such as modules, functions, classes, local variables, parameters, return values, class attributes, and object states, and registers the positions of missing type annotations as slots to be inferred; it constructs a type usage semantic graph based on cross-file call relationships, parameter passing relationships, return value usage relationships, object state propagation relationships between member functions, protocol method matching relationships, and object shape dependency relationships, and records entity categories, modules, scopes, associated code positions, and deterministic markers on nodes and edges to form a repository-level semantic carrier that can be used for constraint propagation, evidence retrieval, and subsequent scheduling; The context building module retrieves consumer-side semantic evidence and dependency-side semantic evidence around the target type slot, and organizes them into a type semantic context consisting of a lead segment, a dependency segment, a target subject segment, and a consumer segment. The base training module organizes the local semantic context, target slot identifier and reference type answer into single-time type inference training samples, performs supervised fine-tuning on the large language model, and obtains a single-time type inference base model. The verifiable reward training module represents the predicted type and the reference type as a type tree, constructs a verifiable structured reward that includes the correctness of the type structure and the correctness of the output format, and performs reinforcement learning training based on group relative policy optimization. The hierarchical refinement training module constructs multiple rounds of refinement objectives according to the reference type depth, and aggregates the round-by-round refinement rewards, termination penalties and final result rewards to obtain a hierarchical reinforcement learning model. The warehouse-level scheduling verification module constructs a warehouse-level scheduling order based on the dependencies between the elements to be inferred, and performs iterative scheduling, candidate generation, local verification, global verification, rollback and feedback update for unresolved slots; The type space self-consistent decoding module, for the multi-round refinement process of complex types, maps multiple candidate outputs to a depth-limited abstract type space in each round, and obtains the stage decoding result through type space self-consistent aggregation, until the final predicted type is output after the termination condition is met.

[0016] The system implementation process and method are the same and will not be described again.

[0017] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the training and inference method for Python repository-level complex type inference as described above.

[0018] A computer-readable storage medium storing a computer program that performs the training and inference method for Python repository-level complex type inference as described above.

[0019] Compared with the prior art, the present invention has at least the following beneficial effects: 1) This invention uses semantic graphs to uniformly represent repository-level function calls, return value propagation, object state reading and writing, and protocol dependencies, which can significantly improve the ability to utilize cross-process semantic evidence required for complex type inference.

[0020] 2) This invention constructs verifiable structured rewards through type tree hierarchical matching, deep calibration, and structured format constraints, enabling the model optimization objective to shift from simply imitating the reference answer to directly aligning with the correctness of complex type structures.

[0021] 3) This invention decomposes complex type inference into a layer-by-layer inference process through hierarchical multi-round refinement reinforcement learning, which can reduce the difficulty of credit allocation in training deep complex types and enhance the model's ability to progressively infer the relationship between nested type parameters.

[0022] 4) This invention forms a generation-verification-regeneration closed loop through warehouse-level scheduling, static type checking, rollback and feedback, which can significantly improve the consistency and deployability of warehouse-level type completion.

[0023] 5) This invention performs multi-sample aggregation on the abstract type space through type space self-consistency decoding, which can reduce the impact of single sampling error and local greedy misjudgment on the inference results of complex types. Attached Figure Description

[0024] Figure 1 This is a schematic diagram of the overall framework of the training and inference method in this embodiment of the invention; Figure 2 This is a schematic diagram of the warehouse-level scheduling and verification framework in an embodiment of the present invention. Detailed Implementation

[0025] The present invention will be further illustrated below with reference to specific embodiments. It should be understood that these embodiments are for illustrative purposes only and are not intended to limit the scope of the invention. After reading the present invention, any modifications of the present invention in various equivalent forms by those skilled in the art will fall within the scope defined by the appended claims.

[0026] like Figure 1As shown in the figure, this embodiment presents a training and inference method for complex type inference at the Python repository level. It generally includes six parts: repository-level semantic modeling, single-round type inference base training, verifiable structured reward reinforcement learning, hierarchical multi-round refinement reinforcement learning, repository-level scheduling verification, and type space self-consistent decoding. This method covers both offline model training and online or offline batch inference processes in real repositories.

[0027] In the offline phase, the code repository to be processed is first parsed to identify parameter slots, return value slots, local variable slots, and class attribute slots for all missing type annotations. Then, functions, variables, class attributes, and object states in the repository are uniformly modeled to construct a semantic graph of type usage. Next, the most relevant consumer-side and dependency-side evidence is extracted around each target slot to construct the local semantic context required for the input of the large language model. Subsequently, a single-pass type inference base model is trained on the basis of the local semantic context, and the complex type inference capability of the model is further optimized through verifiable structured reward reinforcement learning and hierarchical multi-round refinement reinforcement learning.

[0028] During the inference phase, for target slots in the warehouse that have not yet been filled, it is preferable to first establish a warehouse-level scheduling order based on the dependencies between elements, and then select the target slots with the highest current priority in batches under the constraints of the scheduling graph to perform candidate generation, static verification, rollback and feedback update. For complex types with deep structures or high uncertainty, type space self-consistency decoding is used to aggregate the candidate results from multiple rounds, thereby outputting a final predicted type that is more structurally complete and more consistent within the warehouse.

[0029] Therefore, the overall technical approach of this invention unifies "warehouse-level semantic modeling", "structured training optimization", "verification feedback-driven inference closed loop" and "complex type multi-round aggregation decision-making" into the same framework to improve the usability and stability of complex type inference in real engineering scenarios.

[0030] The specific implementation process is as follows: a training and inference method for Python repository-level complex type inference, including the following steps: 1) Construct a type usage semantic graph. Perform lexical, syntactic, and symbolic-level analysis on the code repository to be processed to identify program entities such as modules, functions, classes, local variables, parameters, return values, class attributes, and object states. Register the locations of missing type annotations in the type usage semantic graph as slots to be inferred. Construct a type usage semantic graph based on cross-file call relationships, parameter passing relationships, return value usage relationships, object state propagation relationships between member functions, protocol method matching relationships, and object shape dependency relationships. Record entity categories, modules, scopes, associated code locations, and deterministic markers on nodes and edges to form a repository-level semantic carrier that can be used for constraint propagation, evidence retrieval, and subsequent scheduling.

[0031] 2) Constructing a local semantic context for the target slot. For each slot to be inferred, centered on the element containing the slot, consumer-side semantic evidence and dependency-side semantic evidence are retrieved from the type usage semantic graph. Consumer-side semantic evidence describes which downstream code will consume the predicted type of the slot, while dependency-side semantic evidence describes the interface, state, and auxiliary logic constraints that the element containing the slot needs to satisfy during implementation. Subsequently, candidate evidence is filtered based on evidence certainty, graph distance to the target slot, whether it is located in the same module or the same call path, and the input budget. The retained evidence is organized into a structured local semantic context in the order of lead segment, dependency segment, target body segment, and consumer segment, thus prioritizing the retention of repository-level semantic information most relevant to complex type inference within a limited input length.

[0032] 3) Perform supervised fine-tuning training of the single-shot type inference base model. Organize the local semantic context, target slot identifier, and reference type answer into single-shot type inference training samples, and construct an instruction-following task that requires the large language model to output structured type results; train the large language model using supervised fine-tuning, so that the large language model learns to generate reference type answers for the target slot under the given warehouse-level local semantic context, and obtain the single-shot type inference base model, and simultaneously obtain basic type inference ability, structured output ability, and step-by-step reasoning ability for complex types.

[0033] 4) Perform verifiable structured reward reinforcement learning for complex types. Represent the reference type and the model-predicted type as type trees, respectively. Calculate the maximum matching depth and overall depth deviation between the two in the deep truncation representation, and construct continuous type-level matching scores accordingly. Combine this with checks on the completeness of structured output fields, the parsability of type strings, and the satisfaction of format constraints to form a verifiable structured reward for a single type inference. During training, multiple candidate outputs are sampled in parallel for the same input. Based on the reward corresponding to each candidate output, construct a relative advantage within the group, and use a group-based strategy to optimize and update model parameters to improve the probability of candidate outputs with more correct structure and more stable format.

[0034] 5) Perform hierarchical multi-round refinement reinforcement learning. For deep and complex types, the inference process is broken down into multi-round refinement tasks according to the reference type depth. In each round, a phased target type is constructed, and the structural depth of the confirmed inference is maintained. The refinement reward for each round is calculated based on whether the current round prediction has achieved the phased target, whether effective hierarchical progress has been generated, and whether there has been stagnation or cross-layer jump. Then, a termination penalty is constructed based on the difference between the actual number of termination rounds and the ideal number of termination rounds. Combined with the structured reward of the final round, the total reward of the multi-round trajectory is obtained, which is used to optimize the model's ability to expand, correct, and terminate reasonably for complex types layer by layer.

[0035] 6) Perform repository-level scheduling, verification, and rollback. For slots that remain unresolved during the inference phase, construct a repository-level scheduling order based on the call dependencies, object state dependencies, protocol dependencies, and return value propagation dependencies between the elements to which the unresolved slots belong. After compression of strongly connected components, determine the inference priorities at the component, element, and slot levels. After generating candidate types, write the currently visible prediction results back to the temporary type signature environment, call the static type checker to sequentially perform local verification within the vicinity of the target element and global verification across the entire repository. If a new diagnosis occurs, locate the set of affected slots, perform a rollback on the relevant slots, and write the diagnosis summary to the feedback field to drive the next round of regeneration. If the verification passes, promote the slot status and freeze stable slots.

[0036] 7) Perform type space self-consistent decoding. For each round of multi-round refinement, multiple candidate outputs are sampled in parallel under the same context. The predicted type obtained from parsing the candidate outputs is mapped to the abstract type space with limited depth in the current round. An empirical distribution is formed on this abstract type space, and the mode of the empirical distribution is used as the decoding result of the current round. When the proportion of termination markers in the candidate outputs reaches a threshold, the decoding result of the current round no longer produces effective depth advancement relative to the previous round, or the current round reaches the maximum number of rounds, the multi-round refinement process is terminated, and self-consistent aggregation is performed on multiple candidate predicted types in the complete type space to output the final predicted type.

[0037] In this embodiment, the type is represented using a semantic graph as follows: in, For a set of nodes, Let be the set of edges. and These represent node attribute mapping and edge attribute mapping, respectively. Preferably, the node set... Includes function nodes, local variable nodes, parameter slot nodes, return value slot nodes, class attribute slot nodes, and object state nodes; edge collection. This includes call edges, parameter passing edges, return value flow edges, object state read / write edges, protocol dependency edges, and object shape dependency edges. Node attributes can at least record the entity category, code location, module, scope, and whether it is a slot to be inferred; edge attributes can at least record the relationship type, direction, cross-module information, and deterministic marker. To control noise propagation, both nodes and edges can have deterministic markers to distinguish between explicit syntactic constraints and weakly deterministic relationships obtained by name, pattern, or fallback rules.

[0038] In the actual construction process, abstract syntax tree parsing and symbol binding can be performed on the code repository first to identify function definitions, class definitions, variable assignments, property accesses, function calls, and return statements. Then, using these basic parsing results as input, parameter passing relationships, return value consumption relationships, and object state flow relationships can be extracted. For code locations with dynamic characteristics, supplementary dependencies can be established by combining naming conventions, protocol interfaces, and object shape similarities. In this way, type constraints that were originally scattered across different files, functions, and objects can be uniformly mapped to a single graph structure within the repository.

[0039] For example, for function parameter type inference tasks, call edges can be used to track which callers passed what kind of object to the parameter; for return value type inference tasks, return value flow edges can be used to track how downstream code consumes the return value; for class attribute type inference tasks, object state read / write edges and object shape dependency edges can be used to track the writing, reading, and further refinement of the attribute between member functions. Thus, complex types no longer rely solely on local code snippets but can propagate along semantic constraint paths across the entire repository, providing a unified semantic foundation for subsequent context building, training, and scheduling verification.

[0040] In the construction of the local semantic context of the target slot, the target slot is surrounded by... In this embodiment, two types of evidence are retrieved from the semantic graph: one type is consumer-side evidence of the target consumption element, denoted as... Another type is dependency-side evidence derived from the target element and its selected consumers, denoted as... Consumer-side evidence is mainly used to answer "how the predicted type of this slot will be used", while dependency-side evidence is mainly used to answer "which upstream interfaces and state constraints the element to which this slot belongs needs to satisfy during implementation".

[0041] These two types of evidence, along with the main code of the target element, are then organized into a four-part context. in, This is a leading section used to provide environmental information such as import statements, type aliases, protocol declarations, and lightweight declaration skeletons; This is a dependency section, used to provide auxiliary functions called by the target element, object state constraints, and key construction logic; This is the target body segment, used to provide the main implementation semantics of the function, method, or variable definition where the slot to be inferred is located; For the consumer segment, it provides semantic evidence of how external modules, callers, or downstream processing logic consume the target element.

[0042] During the screening process, candidate evidence is preferably sorted first based on evidence certainty, graph distance, association type with the target slot, and code granularity. Then, truncation is performed using context budget to avoid directly inputting a large amount of low-relevance implementation details into the model. For nodes in the dependency segment, a hierarchical rendering mode can also be adopted: when a dependency node has a weak relationship with the target slot or insufficient budget, only its function signature is retained; when the relationship is strong, the function signature and constructor are retained; when the relationship is strongest and the budget allows, the complete function body is expanded. This strategy prioritizes the retention of the most critical semantic evidence for the target slot under limited input budget, while maintaining a stable, reusable, and batch-generated context structure.

[0043] In the training of the single-type inference base, single-type inference samples are constructed based on the local semantic context. in This is the local semantic context organized around the target slot. For the target slot, This is a reference type answer and its reasoning process.

[0044] Preferably, the samples are organized as structured instruction-following tasks. On the input side, the file containing the target slot, the target element identifier, the target slot position, and the local semantic context are explicitly provided. On the output side, the model is required to return a structured response containing a target type field, and may include step-by-step inference descriptions or auxiliary explanation fields to enhance the interpretability and structural stability of complex type generation. The supervised fine-tuning objective is written as... In terms of training data organization, it is preferable to include both complex type inference samples and general inference samples simultaneously, so that the model retains structured response capabilities and relatively stable intermediate inference capabilities while learning target type generation. For the same type of target slot, a template can be used to unify the input format, thereby reducing cue distribution drift during training. After this step, a single-type inference base model with basic complex type inference capabilities, structured output capabilities, and a certain level of complex type decomposition capabilities can be obtained, providing a stable initialization strategy for subsequent reinforcement learning optimization.

[0045] In verifiable structured reward reinforcement learning for complex types, this embodiment will refer to the type in order to further optimize the model around the real target of complex types. and prediction type These are represented as type trees. Type depth is defined. Define the height of the type tree and define a depth-truncated representation. Used to preserve type trees at depth Hierarchical structures within a given hierarchy. Representing types as type trees allows complex types with nested hierarchies, such as List[int], Dict[str, User], and Dict[str, List[User]], to be compared within a unified structural space.

[0046] For prediction type and reference type, denote To type Cut off to depth The resulting type tree is then used to calculate its maximum matching depth. And construct type-level matching scores based on the deviation between the maximum matching depth and the overall depth. in Used to emphasize deep structure inference, for Based on whether the predicted depth is too shallow or too deep relative to the reference depth, a segmented approach is adopted: considering that "overly shallow prediction" and "overly deep prediction" have different properties in complex type inference, this method sets... Segmentation: Use a smaller segmentation method when the prediction depth is less than the true type depth. To more strongly penalize predictions that remain at shallow structures; to use a larger penalty when the prediction depth is greater than the true type depth. This design allows for a relatively lenient approach to predictions that continue to refine their understanding but are not yet entirely accurate. With this design, a model can still receive limited continuous rewards when it only infers the outer container and fails to infer the relationships between internal parameters; however, as the model progressively infers deeper parameter structures, the score increases with the matching depth.

[0047] Therefore, the verifiable structured reward for a single type inference is defined as follows: Furthermore, constructing a reward format This function checks whether structured fields are complete, type strings are parsable, and output formats meet preset constraints. When structured fields are missing, type strings cannot be parsed, or the output deviates from the preset response format, a negative formatting penalty is applied directly. This suppresses outputs that are "structurally correct but not consumable". The total reward is written as... During training, for the same input Multiple candidate outputs are sampled, and structured parsing, type tree comparison, format checking, and total reward calculation are performed sequentially. Then, the relative advantage within the group is calculated based on the total reward of each candidate output, and the model parameters are updated using Group Relative Policy Optimization (GRPO). In this way, the model no longer simply learns to reproduce the surface token sequence of the reference answer, but directly optimizes around the two objectives of "whether the type structure is closer to the real answer" and "whether the output can be stably consumed by the subsequent system." This allows the single-time type inference capability to gradually transition from imitation learning to result-oriented policy optimization.

[0048] In hierarchical multi-round refinement reinforcement learning, for deep and complex types, it is still difficult to identify the specific level at which errors occur using only the single-round reward. Therefore, this embodiment models the inference of complex types as a multi-round refinement process according to the depth of the reference type. Let the reference type be... Its depth is , remember to truncate it to depth The type obtained is The maximum number of refinement rounds is Then the first The phased goal type of the wheel is defined as follows: Preferably, in the first The round-robin model takes into account the current historical interaction records and control prompts. The first few rounds use the "Continue to refine" control prompt, while the last round or rounds before the termination condition is met use the "Output final answer" control prompt. After each round is generated, the prediction type for that round is parsed to obtain the result. And whether to declare a completed termination marker, and maintain the confirmed maximum inferred structural depth. Subsequently, based on whether the current round of prediction aligns with the type of phased objective, whether it creates a new level, and whether it spans multiple levels, the refined rewards for each round are calculated. At the same time, based on the actual number of rounds terminated. With the ideal number of termination rounds The difference in constructing the termination penalty in express , These represent the penalty coefficients for delay and early termination, respectively.

[0049] In the final round, a single-type inference structured reward is invoked. The final reward is obtained. Therefore, the total reward for the entire multi-round trajectory is written as... Subsequently, multiple trajectories from multiple rounds are sampled for the same input. The relative advantage within the group is calculated based on the total reward of the trajectory, and the model parameters are updated using GRPO. As a result, the model learns not only "giving the correct answer in the last round", but also a multi-round refinement strategy of "progressively inferring complex types along the correct levels within a reasonable number of rounds, while avoiding ineffective stagnation, cross-level jumps, and premature termination as much as possible".

[0050] During warehouse-level scheduling, verification, and rollback, such as Figure 2 As shown, in actual deployment, not all slots can be completed independently. The prediction result of an element often affects its downstream callers, object state consumption methods, and the verification results of other slots to be completed. Therefore, this embodiment constructs a repository-level scheduling order for unresolved slots and maintains the status of each slot: in Indicates the slot status. This indicates the current prediction result. This indicates the total number of attempts. This indicates the number of consecutive global validations that passed. This indicates a summary of the feedback.

[0051] Preferably, a scheduling graph is first constructed based on the call dependencies, return value propagation dependencies, object state dependencies, and protocol dependencies between the elements to which the slot belongs; then, the strongly connected components in the graph are compressed to obtain a directed acyclic component graph that is more suitable for actual scheduling; and on this basis, the components, elements, and slots to be prioritized for processing are determined by comprehensively considering factors such as the number of unresolved dependencies, slot importance, and historical failure count.

[0052] After candidate types are generated, the prediction results of the currently visible slots are written back to the temporary type signature environment. A local static type check is performed within the module adjacent to the target element. If the newly introduced local diagnostic difference is empty, the corresponding slot is promoted to a local validation passed state; otherwise, the set of slots most likely to be affected is located based on the newly added local diagnostic, the relevant slots are rolled back, and the local error type, error location, and constraint conflict summary are written to the feedback field. After the round ends, a global static type check is performed across the entire repository; if the global diagnostic set is empty, the relevant slots are promoted to a global validation passed state, and slots with a stability count reaching the threshold are frozen; otherwise, a larger-scale rollback is performed based on the global diagnostic, and the next round of regeneration is driven by the feedback summary.

[0053] Through the aforementioned closed loop of "generation – local verification – global verification – rollback – feedback update", the inference stage can not only use the model's generation capabilities to propose candidate types, but also use a static type system to perform engineering-level screening of candidate results, thereby suppressing predictions that are locally correct but globally inconsistent.

[0054] In type-space self-consistent decoding, to reduce the impact of single-sample errors on the complex type inference process during the multi-round refinement inference stage, this embodiment employs type-space self-consistent decoding. For the first... In each round, the model samples in parallel within the same context. Each candidate output is assigned a prediction type. Truncate to the current depth to obtain the abstract type state. remember For the space of complete type expressions, It represents any type expression in the type expression space. Indicates will Cut off to depth The resulting type expression. In the abstract type space. Above, an empirical distribution is formed using multiple candidate samples. The mode of this empirical distribution is used as the stage decoding result. Obtain the stage decoding result Then, it can be written to the history of interaction state. This is used to constrain the generation of candidates in the next round, so that the model can continue to refine around the “currently relatively consistent hierarchical structure” instead of generating a complete complex type from scratch in each round.

[0055] When the proportion of termination markers in the candidate outputs reaches a preset threshold, or the decoding result of the current round no longer produces effective depth advancement compared to the previous round, or the current round reaches the maximum number of rounds, the multi-round refinement is terminated, and the process is completed in the full type space. An empirical distribution is formed using multiple candidate prediction types, and the mode is taken as the final prediction type. Through this process, the inference phase decodes the "most consistent abstract type state at the current level" in the intermediate rounds, and decodes the "most consistent final answer in the complete type space" in the final round, thereby reducing the impact of a single sampling failure on the final complex type inference result.

[0056] System Deployment: The method of this invention can be deployed in a local training and inference environment, or in a distributed environment that includes model services, static type checking services, repository parsing services, and task scheduling services. Preferably, for the system, the semantic graph construction module and the context construction module are used for offline preprocessing and cache the generated semantic graph index, target slot index, and context samples to persistent storage; the base training module, the verifiable reward training module, and the hierarchical refinement training module are used for offline model training and periodically output deployable policy model versions; the repository-level scheduling verification module and the type space self-consistency decoding module are used for online inference or offline batch completion and write back the verification results, diagnostic logs, and frozen slot status to the repository-level task status manager.

[0057] In a preferred deployment approach, the repository parsing service receives the target repository and generates a graph structure and a list of candidate slots; the model service receives the context and outputs candidate type results; the static type checking service performs local and global validation on intermediate results; and the task scheduling service determines the next batch of targets to be inferred based on the status of unresolved slots. Through the collaborative work of these modules, stable inference, continuous correction, and backtrackable deployment of complex types can be achieved in a real-world engineering repository.

[0058] End-to-end inference example for complex nested types: To further illustrate the working method of this invention, an end-to-end inference example for complex nested return value types is given below. Suppose that the target function in a certain repository lacks a return value type annotation. After traversing the input records, its function body aggregates multiple user objects by department identifier, converts each user object, and writes it into a list, ultimately returning a mapping structure of the user list indexed by string keys. Downstream calling code further performs operations such as key access, list iteration, and user field reading on the returned result.

[0059] In this invention, the return value slot of the function is first identified in the type usage semantic graph, and downstream consumption evidence is retrieved along the return value flow edge to obtain consumption-side constraints such as "the return result is accessed by string key", "the corresponding value is treated as a list iteration", and "list elements are read as fields of user objects". At the same time, dependency-side evidence is retrieved along the function body's internal call relationship and object state relationship to obtain implementation constraints such as "the user object is generated by a certain constructor or factory method" and "the aggregate container is initialized and elements are appended in the function body". Subsequently, this evidence is organized into a leading segment, a dependency segment, a target body segment, and a consumption segment and input into the model.

[0060] Take the complex nested type Dict[str, List[User]] as an example. In the supervised fine-tuning phase, the model first learns to output the structured complex type Dict[str, List[User]] in similar contexts. In the verifiable structured reward reinforcement learning phase, if the model outputs Dict[str, Any] or Dict[str, List[Any]], it can still obtain continuous rewards based on the maximum matching depth of the type tree, thereby encouraging the model to gradually infer deeper structures. In the hierarchical multi-round refinement reinforcement learning phase, the model can sequentially learn to infer stage-wise structures such as Dict, Dict[str, List], and Dict[str, List[User]].

[0061] During the inference phase, the warehouse-level scheduling module prioritizes the return value slot and writes its candidate predictions back to the temporary type signature environment for local and global verification. If a candidate result causes a new index or member access error at the downstream caller, the candidate is rolled back and a new feedback digest is generated; if the verification passes, a multi-round self-consistent decoding process begins. During decoding, the model generates multiple candidate types in parallel each round and performs aggregation on the current hierarchical structure in the abstract type space; when the phase result is stable and the termination condition is met, the mode of multiple candidate complex types is taken as the final result in the complete type space. Through the above end-to-end process, a complex return value type that is consistent with the internal construction logic of the function body and compatible with the downstream consumption method can be obtained.

[0062] Obviously, those skilled in the art should understand that the steps of the training and inference method for Python repository-level complex type inference or the modules of the training and inference system for Python repository-level complex type inference in the above embodiments of the present invention can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. Optionally, they can be implemented using computer-executable program code, thereby storing them in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the embodiments of the present invention are not limited to any particular hardware and software combination.

Claims

1. A training and inference method for complex type inference at the Python repository level, characterized in that, Includes the following steps: (1) Obtain the code repository to be processed, identify the parameter, return and variable type slots in it, and construct a type usage semantic graph; (2) Around the target type slot, retrieve the consumer-side semantic evidence and dependency-side semantic evidence of the target element from the semantic graph of the type to form the local semantic context of the target slot; (3) During the training phase, single-type inference training samples are constructed based on the local semantic context, and the large language model is supervised and fine-tuned to obtain the single-type inference base model; (4) Represent the predicted type and the reference type as a type tree, construct a verifiable structured reward that includes the correctness of the type structure and the correctness of the output format, and perform reinforcement learning training on the single type inference base model based on GRPO; (5) Model complex type inference as a multi-round refinement process according to the hierarchical depth of the reference type, construct phased target types, round-by-round refinement rewards, termination penalties and final result rewards, and conduct hierarchical reinforcement learning training based on GRPO; (6) During the inference phase, a warehouse-level scheduling order is constructed based on the dependencies between the elements to be inferred, and iterative scheduling, candidate generation, local verification, global verification, rollback and feedback update are performed on unresolved slots; (7) For each round of the multi-round refinement inference process, multiple candidate outputs are sampled in parallel from the current strategy, the candidate outputs are mapped to a depth-limited abstract type space, and the stage decoding result is obtained through type space self-consistency aggregation until the termination condition is met and the final predicted type is output.

2. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, The type uses nodes in the semantic graph that include at least function entities, variable entities, parameter slots, return value slots, class attribute slots, and object state entities; the edges include at least procedure call edges, parameter passing edges, return value flow edges, object state read / write edges, protocol call edges, and object shape dependency edges; and deterministic markers are recorded for nodes and edges respectively to distinguish between deterministic semantics obtained from explicit syntactic relations and indeterminate semantics obtained from backtracking matching.

3. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, The construction methods of the local semantic context include: For each slot to be inferred, consumer-side semantic evidence and dependency-side semantic evidence are retrieved from the semantic graph of the type, centered on the element to which the slot belongs. Consumer-side semantic evidence describes which downstream code will consume the predicted type of the slot, while dependency-side semantic evidence describes the interface, state, and auxiliary logic constraints that the element to which the slot belongs needs to satisfy during implementation. Subsequently, candidate evidence is filtered based on evidence certainty, graph distance to the target slot, whether it is located in the same module or the same call path, and input budget. The retained evidence is organized into a structured context in the order of lead segment, dependency segment, target body segment, and consumer segment, thereby prioritizing the retention of the repository-level semantic information most relevant to complex type inference within a limited input length.

4. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, In step (4), the reference type and the model prediction type are represented as type trees respectively. The maximum matching depth and the overall depth deviation of the two in the deep truncation representation are calculated. Based on this, a continuous type hierarchical matching score is constructed. Then, combined with the checks on the integrity of the structured output field, the parsability of the type string and the satisfaction of the format constraints, a verifiable structured reward for a single type inference is formed. During training, multiple candidate outputs are sampled in parallel for the same input. The relative advantage within the group is constructed based on the reward corresponding to each candidate output, and the model parameters are optimized and updated using a group relative strategy.

5. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, In step (5), for complex nested types, the inference process is broken down into multiple rounds of refinement tasks according to the reference type depth. In each round, a phased target type is constructed, and the structural depth of the confirmed inference is maintained. The refinement reward is calculated based on whether the phased target is achieved, whether effective hierarchical advancement is generated, and whether stagnation or cross-level jump occurs in the current round of prediction. Then, based on the difference between the actual number of termination rounds and the ideal number of termination rounds, a termination penalty is constructed, and combined with the structured reward of the final round, the total reward of the multi-round trajectory is obtained, which is used to optimize the model's ability to expand, correct and terminate reasonably for complex types layer by layer.

6. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, In step (6), for slots that are still unresolved in the inference phase, a repository-level scheduling order is constructed based on the call dependencies, object state dependencies, protocol dependencies, and return value propagation dependencies between the elements to which the unresolved slots belong. After compression of strongly connected components, the inference priorities at the component level, element level, and slot level are determined. After generating candidate types, the currently visible prediction results are written back to the temporary type signature environment. The static type checker is called to perform local verification within the vicinity of the target element and global verification within the entire repository. If a new diagnosis occurs, the affected slot set is located, the relevant slots are rolled back, and the diagnosis summary is written to the feedback field to drive the next round of regeneration. If the verification passes, the slot status is promoted and the stable slots are frozen.

7. The training and inference method for Python repository-level complex type inference according to claim 1, characterized in that, In step (7), for each round of multi-round refinement, multiple candidate outputs are sampled in parallel under the same context, the prediction type obtained by parsing the candidate output is mapped to the abstract type space with limited depth of the current round, an empirical distribution is formed on the abstract type space, and the mode of the empirical distribution is used as the decoding result of the current round stage. When the proportion of termination markers in the candidate outputs reaches a threshold, the decoding result of the current round no longer produces effective depth advancement relative to the previous round, or the current round reaches the maximum number of rounds, the multi-round refinement process is terminated, and self-consistent aggregation is performed on multiple candidate prediction types in the complete type space to output the final prediction type.

8. A training and inference system for complex type inference at the Python repository level, characterized in that, It includes a semantic graph construction module, a context construction module, a base training module, a verifiable reward training module, a hierarchical refinement training module, a warehouse-level scheduling verification module, and a type space self-consistency decoding module; The semantic graph construction module performs lexical, syntactic, and symbolic-level analysis on the code repository to be processed, identifies program entities, and registers the positions of missing type annotations as slots to be inferred. Based on cross-file call relationships, parameter passing relationships, return value usage relationships, object state propagation relationships between member functions, protocol method matching relationships, and object shape dependency relationships, it constructs a type usage semantic graph and records entity categories, modules, scopes, associated code positions, and deterministic markers on nodes and edges to form a repository-level semantic carrier that can be used for constraint propagation, evidence retrieval, and subsequent scheduling. The context building module retrieves consumer-side semantic evidence and dependency-side semantic evidence around the target type slot, and organizes them into a type semantic context consisting of a lead segment, a dependency segment, a target subject segment, and a consumer segment. The base training module organizes the local semantic context, target slot identifier and reference type answer into single-time type inference training samples, performs supervised fine-tuning on the large language model, and obtains a single-time type inference base model. The verifiable reward training module represents the predicted type and the reference type as a type tree, constructs a verifiable structured reward that includes the correctness of the type structure and the correctness of the output format, and performs reinforcement learning training based on group relative policy optimization. The hierarchical refinement training module constructs multiple rounds of refinement objectives according to the reference type depth, and aggregates the round-by-round refinement rewards, termination penalties and final result rewards to obtain a hierarchical reinforcement learning model. The warehouse-level scheduling verification module constructs a warehouse-level scheduling order based on the dependencies between the elements to be inferred, and performs iterative scheduling, candidate generation, local verification, global verification, rollback and feedback update for unresolved slots; The type space self-consistent decoding module, for the multi-round refinement process of complex types, maps multiple candidate outputs to a depth-limited abstract type space in each round, and obtains the stage decoding result through type space self-consistent aggregation, until the final predicted type is output after the termination condition is met.

9. A computer device, characterized in that: The computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the steps of the training and inference method for Python repository-level complex type inference as described in any one of claims 1-7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program that performs a training and inference method for Python repository-level complex type inference as described in any one of claims 1-7.