Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / inline / spec_bundle.py: 80%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-10-25 05:45 +0800

1# Copyright 2026 Huawei Technologies Co., Ltd 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================ 

15"""Data contract for the inline pipeline's resolved source-level specs.""" 

16 

17from __future__ import annotations 

18 

19from dataclasses import dataclass 

20 

21from hyper_parallel.codegen.inline.ir import ImportPatch, ModuleSnippetPatch 

22 

23 

24@dataclass(frozen=True) 

25class ReplacementSpec: 

26 """Source-level form of one ``replace_module`` target.""" 

27 

28 old_ctor: str 

29 new_ctor: str 

30 imports: tuple[ImportPatch, ...] 

31 mode: str = "name" 

32 keyword_args: tuple[str, ...] = () 

33 snippets: tuple[ModuleSnippetPatch, ...] = () 

34 remove_class: bool = True 

35 replacement_note: str | None = None 

36 

37 

38@dataclass(frozen=True) 

39class StrategySpec: 

40 """Source-level form of one parallel strategy target.""" 

41 

42 kind: str 

43 imports: tuple[ImportPatch, ...] 

44 target_class: str | None = None 

45 method_name: str = "forward" 

46 body_template: str | None = None 

47 snippets: tuple[ModuleSnippetPatch, ...] = () 

48 strip_boundary_subpatterns: tuple[str, ...] = () 

49 

50 def __post_init__(self) -> None: 

51 """Require a complete method patch or an imports-only declaration.""" 

52 if (self.target_class is None) != (self.body_template is None): 

53 raise ValueError("Inline strategy target_class and body_template must be provided together") 

54 if self.target_class is not None and (not self.target_class or not self.body_template.strip()): 

55 raise ValueError("Inline strategy class and body must be nonempty") 

56 if not self.method_name.isidentifier(): 

57 raise ValueError("Inline strategy method_name must be a Python identifier") 

58 

59 

60__all__ = [ 

61 "ReplacementSpec", 

62 "StrategySpec", 

63]