Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / emit / replacement.py: 89%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-12-05 05:12 +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"""Translate native replacement observations for source emission.""" 

16from __future__ import annotations 

17 

18from typing import Any 

19 

20 

21def replacement_records_from_snapshot(snapshot: Any) -> tuple[dict[str, Any], ...]: 

22 """Translate native replacement observations into emission records. 

23 

24 The conversion consumes immutable observation data and never recompiles 

25 rules or inspects a live model. 

26 

27 Args: 

28 snapshot: Resolved native build snapshot containing replacement observations. 

29 """ 

30 payload = snapshot._emission_view() if hasattr(snapshot, "_emission_view") else snapshot # pylint: disable=protected-access 

31 if not isinstance(payload, dict): 

32 raise TypeError("replacement snapshot must be a CodegenSnapshot or dict") 

33 replacements = payload.get("replacements", {}) 

34 declarations = replacements.get("declarations", ()) 

35 applications = replacements.get("applications", ()) 

36 declarations_by_id = {item["rule_id"]: item for item in declarations} 

37 if len(declarations_by_id) != len(declarations): 

38 raise ValueError("replacement snapshot contains duplicate rule_id values") 

39 records = [] 

40 for application in applications: 

41 rule_id = application.get("rule_id") 

42 declaration = declarations_by_id.get(rule_id) 

43 if declaration is None: 

44 raise ValueError( 

45 f"replacement application references unknown rule_id {rule_id!r}" 

46 ) 

47 source_identity = application.get("source_type") or declaration.get("source_type") 

48 if not isinstance(source_identity, str) or not source_identity: 

49 raise ValueError( 

50 f"replacement application {rule_id!r} has no resolved source_type" 

51 ) 

52 source_type = source_identity.replace(":", ".", 1) 

53 record = { 

54 "match": list(declaration["match"]), 

55 "fqn": application["primary_fqn"], 

56 "fqns": list(application["module_fqns"]), 

57 "module_type": source_type, 

58 "factory": application["replacement_type"].replace(":", ".", 1), 

59 "replacement_target": declaration["replacement_target"], 

60 "exact_type": bool(declaration["exact_type"]), 

61 } 

62 if declaration.get("static_arguments"): 

63 record["target_config"] = dict(declaration["static_arguments"]) 

64 family = application.get("family_request") 

65 if family is not None: 

66 record["factory"] = family["factory"].replace(":", ".", 1) 

67 record["replacement_target"] = record["factory"] 

68 record["target_config"] = {**family["kwargs"], "context": family["context"]} 

69 if application.get("construction"): 

70 record["construction"] = application["construction"] 

71 records.append(record) 

72 return tuple(records) 

73 

74 

75__all__ = ["replacement_records_from_snapshot"]