Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/setuptools/_reqs.py: 83%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

18 statements  

1from __future__ import annotations 

2 

3from functools import lru_cache 

4from typing import TYPE_CHECKING, Callable, Iterable, Iterator, TypeVar, Union, overload 

5 

6import jaraco.text as text 

7from packaging.requirements import Requirement 

8 

9if TYPE_CHECKING: 

10 from typing_extensions import TypeAlias 

11 

12_T = TypeVar("_T") 

13_StrOrIter: TypeAlias = Union[str, Iterable[str]] 

14 

15 

16parse_req: Callable[[str], Requirement] = lru_cache()(Requirement) 

17# Setuptools parses the same requirement many times 

18# (e.g. first for validation than for normalisation), 

19# so it might be worth to cache. 

20 

21 

22def parse_strings(strs: _StrOrIter) -> Iterator[str]: 

23 """ 

24 Yield requirement strings for each specification in `strs`. 

25 

26 `strs` must be a string, or a (possibly-nested) iterable thereof. 

27 """ 

28 return text.join_continuation(map(text.drop_comment, text.yield_lines(strs))) 

29 

30 

31# These overloads are only needed because of a mypy false-positive, pyright gets it right 

32# https://github.com/python/mypy/issues/3737 

33@overload 

34def parse(strs: _StrOrIter) -> Iterator[Requirement]: ... 

35@overload 

36def parse(strs: _StrOrIter, parser: Callable[[str], _T]) -> Iterator[_T]: ... 

37def parse(strs: _StrOrIter, parser: Callable[[str], _T] = parse_req) -> Iterator[_T]: # type: ignore[assignment] 

38 """ 

39 Replacement for ``pkg_resources.parse_requirements`` that uses ``packaging``. 

40 """ 

41 return map(parser, parse_strings(strs))