Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/numpy/_typing/_nested_sequence.py: 67%

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-09 06:12 +0000

1"""A module containing the `_NestedSequence` protocol.""" 

2 

3from __future__ import annotations 

4 

5from collections.abc import Iterator 

6from typing import ( 

7 Any, 

8 TypeVar, 

9 Protocol, 

10 runtime_checkable, 

11) 

12 

13__all__ = ["_NestedSequence"] 

14 

15_T_co = TypeVar("_T_co", covariant=True) 

16 

17 

18@runtime_checkable 

19class _NestedSequence(Protocol[_T_co]): 

20 """A protocol for representing nested sequences. 

21 

22 Warning 

23 ------- 

24 `_NestedSequence` currently does not work in combination with typevars, 

25 *e.g.* ``def func(a: _NestedSequnce[T]) -> T: ...``. 

26 

27 See Also 

28 -------- 

29 collections.abc.Sequence 

30 ABCs for read-only and mutable :term:`sequences`. 

31 

32 Examples 

33 -------- 

34 .. code-block:: python 

35 

36 >>> from __future__ import annotations 

37 

38 >>> from typing import TYPE_CHECKING 

39 >>> import numpy as np 

40 >>> from numpy._typing import _NestedSequence 

41 

42 >>> def get_dtype(seq: _NestedSequence[float]) -> np.dtype[np.float64]: 

43 ... return np.asarray(seq).dtype 

44 

45 >>> a = get_dtype([1.0]) 

46 >>> b = get_dtype([[1.0]]) 

47 >>> c = get_dtype([[[1.0]]]) 

48 >>> d = get_dtype([[[[1.0]]]]) 

49 

50 >>> if TYPE_CHECKING: 

51 ... reveal_locals() 

52 ... # note: Revealed local types are: 

53 ... # note: a: numpy.dtype[numpy.floating[numpy._typing._64Bit]] 

54 ... # note: b: numpy.dtype[numpy.floating[numpy._typing._64Bit]] 

55 ... # note: c: numpy.dtype[numpy.floating[numpy._typing._64Bit]] 

56 ... # note: d: numpy.dtype[numpy.floating[numpy._typing._64Bit]] 

57 

58 """ 

59 

60 def __len__(self, /) -> int: 

61 """Implement ``len(self)``.""" 

62 raise NotImplementedError 

63 

64 def __getitem__(self, index: int, /) -> _T_co | _NestedSequence[_T_co]: 

65 """Implement ``self[x]``.""" 

66 raise NotImplementedError 

67 

68 def __contains__(self, x: object, /) -> bool: 

69 """Implement ``x in self``.""" 

70 raise NotImplementedError 

71 

72 def __iter__(self, /) -> Iterator[_T_co | _NestedSequence[_T_co]]: 

73 """Implement ``iter(self)``.""" 

74 raise NotImplementedError 

75 

76 def __reversed__(self, /) -> Iterator[_T_co | _NestedSequence[_T_co]]: 

77 """Implement ``reversed(self)``.""" 

78 raise NotImplementedError 

79 

80 def count(self, value: Any, /) -> int: 

81 """Return the number of occurrences of `value`.""" 

82 raise NotImplementedError 

83 

84 def index(self, value: Any, /) -> int: 

85 """Return the first index of `value`.""" 

86 raise NotImplementedError