MCPcopy Create free account
hub / github.com/psf/black / parse_req_python_specifier

Function parse_req_python_specifier

src/black/files.py:171–185  ·  view source on GitHub ↗

Parse a specifier string (i.e. ``">=3.7,<3.10"``) to a list of TargetVersion. If parsing fails, will raise a packaging.specifiers.InvalidSpecifier error. If the parsed specifier cannot be mapped to a valid TargetVersion, returns None.

(requires_python: str)

Source from the content-addressed store, hash-verified

169
170
171def parse_req_python_specifier(requires_python: str) -> Optional[List[TargetVersion]]:
172 """Parse a specifier string (i.e. ``">=3.7,<3.10"``) to a list of TargetVersion.
173
174 If parsing fails, will raise a packaging.specifiers.InvalidSpecifier error.
175 If the parsed specifier cannot be mapped to a valid TargetVersion, returns None.
176 """
177 specifier_set = strip_specifier_set(SpecifierSet(requires_python))
178 if not specifier_set:
179 return None
180
181 target_version_map = {f"3.{v.value}": v for v in TargetVersion}
182 compatible_versions: List[str] = list(specifier_set.filter(target_version_map))
183 if compatible_versions:
184 return [target_version_map[v] for v in compatible_versions]
185 return None
186
187
188def strip_specifier_set(specifier_set: SpecifierSet) -> SpecifierSet:

Callers 1

infer_target_versionFunction · 0.85

Calls 1

strip_specifier_setFunction · 0.85

Tested by

no test coverage detected