|
15 | 15 | :data:`~.MethodWrapperType`, The type of *bound* methods of some built-in data types and base classes.
|
16 | 16 | :data:`~.MethodDescriptorType`, The type of methods of some built-in data types.
|
17 | 17 | :data:`~.ClassMethodDescriptorType`, The type of *unbound* class methods of some built-in data types.
|
| 18 | + :data:`~.String`, :class:`~typing.Protocol` for classes that implement ``__str__``. |
| 19 | + :data:`~.HasHead`, :class:`typing.Protocol` for classes that have a ``head``. |
18 | 20 |
|
19 | 21 | """
|
20 | 22 | #
|
|
41 | 43 | import pathlib
|
42 | 44 | from decimal import Decimal
|
43 | 45 | from json import JSONDecoder, JSONEncoder
|
44 |
| -from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union |
| 46 | +from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union, runtime_checkable |
45 | 47 |
|
46 | 48 | # 3rd party
|
47 | 49 | from typing_extensions import Protocol
|
|
55 | 57 | "MethodWrapperType",
|
56 | 58 | "MethodDescriptorType",
|
57 | 59 | "ClassMethodDescriptorType",
|
| 60 | + "HasHead", |
| 61 | + "String", |
58 | 62 | ]
|
59 | 63 |
|
60 | 64 | #: Type hint for objects that represent filesystem paths.
|
@@ -172,3 +176,34 @@ def loads(
|
172 | 176 | MethodWrapperType = type(object().__str__)
|
173 | 177 | MethodDescriptorType = type(str.join)
|
174 | 178 | ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
|
| 179 | + |
| 180 | + |
| 181 | +@runtime_checkable |
| 182 | +class String(Protocol): |
| 183 | + """ |
| 184 | + :class:`~typing.Protocol` for classes that implement ``__str__``. |
| 185 | + """ |
| 186 | + |
| 187 | + def __str__(self) -> str: |
| 188 | + ... # pragma: no cover |
| 189 | + |
| 190 | + |
| 191 | +@runtime_checkable |
| 192 | +class HasHead(Protocol): |
| 193 | + """ |
| 194 | + :class:`typing.Protocol` for classes that have a ``head``. |
| 195 | +
|
| 196 | + This includes :class:`pandas.DataFrame` and :class:`pandas.Series`. |
| 197 | + """ |
| 198 | + |
| 199 | + def head(self: "FrameOrSeries", n: int = 5) -> "FrameOrSeries": |
| 200 | + ... # pragma: no cover |
| 201 | + |
| 202 | + def to_string(self, *args, **kwargs) -> Optional[str]: |
| 203 | + ... # pragma: no cover |
| 204 | + |
| 205 | + |
| 206 | +# class SupportsLessThan(Protocol): |
| 207 | +# |
| 208 | +# def __lt__(self, other: Any) -> bool: |
| 209 | +# ... # pragma: no cover |
0 commit comments