Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / codegen / check / pin.py: 0%

28 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# Licensed under the Apache License, Version 2.0 

3# ============================================================================ 

4"""Transformers-pin verification for codegen artifacts. 

5 

6The generated modeling file embeds a modeling source written against some 

7transformers version. ``verify_transformers_pin`` asks whether the *installed* 

8transformers satisfies the pin the user expects (``==5.15.0``, ``>=5.14``, 

9``<6`` ...). The check returns **SKIP** rather than FAIL when the 

10installed version does NOT satisfy the pin: the artifact is not stale because 

11of an environment mismatch, it just cannot be validated by the pin — so the 

12caller exits 0 either way, and reserves exit 1 for "the pin itself cannot be 

13parsed" / "the installed version cannot be read". 

14""" 

15from __future__ import annotations 

16 

17import logging 

18from typing import Optional, Tuple 

19 

20logger = logging.getLogger(__name__) 

21 

22# The public status the CLI maps to an exit code. 

23OK = "OK" 

24SKIP = "SKIP" 

25 

26 

27def verify_transformers_pin(spec: str) -> Tuple[str, str, str]: 

28 """Compare the installed transformers to ``spec``. 

29 

30 Returns ``(status, installed, spec)`` where ``status`` is ``OK`` when the 

31 installed version satisfies ``spec`` and ``SKIP`` otherwise. Raises 

32 ``ValueError`` when ``spec`` cannot be parsed and ``ImportError`` when the 

33 installed transformers version cannot be read (both are exit-1 conditions 

34 for the CLI). 

35 """ 

36 installed = _installed_version() 

37 if installed is None: 

38 raise ImportError("codegen check: cannot read installed transformers version") 

39 

40 from packaging.specifiers import SpecifierSet 

41 

42 try: 

43 pin = SpecifierSet(spec) 

44 except Exception as exc: 

45 raise ValueError(f"codegen check: invalid transformers pin {spec!r}: {exc}") from exc 

46 

47 if installed in pin: 

48 return OK, installed, spec 

49 logger.warning( 

50 "codegen check: installed transformers %s does not satisfy pin %s — " 

51 "SKIP (artifact is not stale, pin is not satisfiable here)", 

52 installed, spec, 

53 ) 

54 return SKIP, installed, spec 

55 

56 

57def _installed_version() -> Optional[str]: 

58 """Installed transformers version, or ``None`` when not importable.""" 

59 try: 

60 return _version_from_importlib_metadata() 

61 except ImportError: 

62 return None 

63 

64 

65def _version_from_importlib_metadata() -> str: 

66 import importlib.metadata 

67 

68 return importlib.metadata.version("transformers") 

69 

70 

71__all__ = [ 

72 "OK", 

73 "SKIP", 

74 "verify_transformers_pin", 

75]