MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / require_version

Function require_version

diffusers/src/diffusers/utils/versions.py:49–111  ·  view source on GitHub ↗

Perform a runtime check of the dependency versions, using the exact same syntax used by pip. The installed module version comes from the *site-packages* dir via *importlib.metadata*. Args: requirement (`str`): pip style definition, e.g., "tokenizers==0.9.4", "tqdm>=4.27", "nu

(requirement: str, hint: Optional[str] = None)

Source from the content-addressed store, hash-verified

47
48
49def require_version(requirement: str, hint: Optional[str] = None) -> None:
50 """
51 Perform a runtime check of the dependency versions, using the exact same syntax used by pip.
52
53 The installed module version comes from the *site-packages* dir via *importlib.metadata*.
54
55 Args:
56 requirement (`str`): pip style definition, e.g., "tokenizers==0.9.4", "tqdm>=4.27", "numpy"
57 hint (`str`, *optional*): what suggestion to print in case of requirements not being met
58
59 Example:
60
61 ```python
62 require_version("pandas>1.1.2")
63 require_version("numpy>1.18.5", "this is important to have for whatever reason")
64 ```"""
65
66 hint = f"\n{hint}" if hint is not None else ""
67
68 # non-versioned check
69 if re.match(r"^[\w_\-\d]+$", requirement):
70 pkg, op, want_ver = requirement, None, None
71 else:
72 match = re.findall(r"^([^!=<>\s]+)([\s!=<>]{1,2}.+)", requirement)
73 if not match:
74 raise ValueError(
75 "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23, but"
76 f" got {requirement}"
77 )
78 pkg, want_full = match[0]
79 want_range = want_full.split(",") # there could be multiple requirements
80 wanted = {}
81 for w in want_range:
82 match = re.findall(r"^([\s!=<>]{1,2})(.+)", w)
83 if not match:
84 raise ValueError(
85 "requirement needs to be in the pip package format, .e.g., package_a==1.23, or package_b>=1.23,"
86 f" but got {requirement}"
87 )
88 op, want_ver = match[0]
89 wanted[op] = want_ver
90 if op not in ops:
91 raise ValueError(f"{requirement}: need one of {list(ops.keys())}, but got {op}")
92
93 # special case
94 if pkg == "python":
95 got_ver = ".".join([str(x) for x in sys.version_info[:3]])
96 for op, want_ver in wanted.items():
97 _compare_versions(op, got_ver, want_ver, requirement, pkg, hint)
98 return
99
100 # check if any version is installed
101 try:
102 got_ver = importlib.metadata.version(pkg)
103 except importlib.metadata.PackageNotFoundError:
104 raise importlib.metadata.PackageNotFoundError(
105 f"The '{requirement}' distribution was not found and is required by this application. {hint}"
106 )

Callers 2

dep_version_checkFunction · 0.85
require_version_coreFunction · 0.85

Calls 1

_compare_versionsFunction · 0.85

Tested by

no test coverage detected