Skip to content

Commit adaf64e

Browse files
committed
Consolidate Protocols in typing module.
1 parent 3c6cf98 commit adaf64e

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

domdf_python_tools/stringlist.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
from typing import Any, Iterable, Iterator, List, Tuple, Union, cast, overload
2929

3030
# this package
31-
from domdf_python_tools.utils import String, convert_indents
31+
from domdf_python_tools.typing import String
32+
from domdf_python_tools.utils import convert_indents
3233

3334
# Will be removed in 1.0
3435
String = String

domdf_python_tools/typing.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
:data:`~.MethodWrapperType`, The type of *bound* methods of some built-in data types and base classes.
1616
:data:`~.MethodDescriptorType`, The type of methods of some built-in data types.
1717
: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``.
1820
1921
"""
2022
#
@@ -41,7 +43,7 @@
4143
import pathlib
4244
from decimal import Decimal
4345
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
4547

4648
# 3rd party
4749
from typing_extensions import Protocol
@@ -55,6 +57,8 @@
5557
"MethodWrapperType",
5658
"MethodDescriptorType",
5759
"ClassMethodDescriptorType",
60+
"HasHead",
61+
"String",
5862
]
5963

6064
#: Type hint for objects that represent filesystem paths.
@@ -172,3 +176,34 @@ def loads(
172176
MethodWrapperType = type(object().__str__)
173177
MethodDescriptorType = type(str.join)
174178
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

domdf_python_tools/utils.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
# this package
6363
import domdf_python_tools.words
6464
from domdf_python_tools import __version__
65+
from domdf_python_tools.typing import HasHead, String
6566

6667
if TYPE_CHECKING:
6768
# 3rd party
@@ -91,8 +92,6 @@
9192
"convert_indents",
9293
"etc",
9394
"head",
94-
"HasHead",
95-
"String",
9695
]
9796

9897
#: The current major python version.
@@ -102,16 +101,6 @@
102101
SPACE_PLACEHOLDER = '␣'
103102

104103

105-
@runtime_checkable
106-
class String(Protocol):
107-
"""
108-
Protocol for classes that implement ``__str__``.
109-
"""
110-
111-
def __str__(self) -> str:
112-
... # pragma: no cover
113-
114-
115104
def check_dependencies(dependencies: Iterable[str], prt: bool = True) -> List[str]:
116105
"""
117106
Check whether one or more dependencies are available to be imported.
@@ -438,21 +427,6 @@ def convert_indents(text: str, tab_width: int = 4, from_: str = "\t", to: str =
438427
)
439428

440429

441-
@runtime_checkable
442-
class HasHead(Protocol):
443-
"""
444-
:class:`typing.Protocol` for classes that have a ``head``.
445-
446-
This includes :class:`pandas.DataFrame` and :class:`pandas.Series`.
447-
"""
448-
449-
def head(self: "FrameOrSeries", n: int = 5) -> "FrameOrSeries":
450-
... # pragma: no cover
451-
452-
def to_string(self, *args, **kwargs) -> Optional[str]:
453-
... # pragma: no cover
454-
455-
456430
class _Etcetera(str):
457431

458432
def __new__(cls):

domdf_python_tools/words.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,6 @@ def get_random_word(min_length: int = 0, max_length: int = -1) -> str:
143143
return random.choice(words_list)
144144

145145

146-
# class SupportsLessThan(Protocol):
147-
#
148-
# def __lt__(self, other: Any) -> bool:
149-
# ... # pragma: no cover
150-
151146
# _default_unicode_sort_order: str = "".join(sorted(chr(i) for i in range(sys.maxunicode + 1)))
152147

153148

0 commit comments

Comments
 (0)