Skip to content

Commit d1a73a2

Browse files
committed
Rewriting of type hintings.
1 parent 3e6574c commit d1a73a2

File tree

5 files changed

+70
-83
lines changed

5 files changed

+70
-83
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.vscode
22
.idea
3+
4+
# Sphinx documentation
5+
/doc/build/

visualdialog/box.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import curses.textpad
2626
from typing import List, Tuple, Union
2727

28-
from .utils import (CursesKeyConstants,
28+
from .utils import (CursesKeyConstant,
29+
CursesKeyConstants,
30+
CursesTextAttributesConstant,
2931
CursesTextAttributesConstants,
3032
TextAttributes)
3133

@@ -38,7 +40,7 @@ class PanicError(Exception):
3840
:type key: CursesKeyConstants
3941
"""
4042
def __init__(self,
41-
key: CursesKeyConstants):
43+
key: CursesKeyConstant):
4244
self.key = key
4345

4446
def __str__(self):
@@ -49,8 +51,8 @@ class TextBox:
4951
"""This class provides attributs and methods to manage a text box.
5052
5153
.. NOTE::
52-
This class provides a general API for text boxes, it does not need
53-
to be instantiated.
54+
This class provides a general API for text boxes, it does not
55+
need to be instantiated.
5456
5557
:param pos_x: x position of the dialog box in ``curses`` window
5658
object on which methods will have effects.
@@ -72,31 +74,31 @@ class TextBox:
7274
of dialog box.
7375
If title is an empty string, the title will not be displayed.
7476
This defaults an empty string.
75-
:type title: Optional[str]
77+
:type title: str
7678
7779
:param title_colors_pair_nb:
7880
Number of the curses color pair that will be used to color the
7981
title. Zero corresponding to the pair of white color on black
8082
background initialized by ``curses``). This defaults to ``0``.
81-
:type title_colors_pair_nb: Optional[int]
83+
:type title_colors_pair_nb: int
8284
8385
:param title_text_attr:
8486
Dialog box title text attributes. It should be a single curses
8587
text attribute or a tuple of curses text attribute. This
8688
defaults to ``curses.A_BOLD``.
87-
:type title_text_attr: Optional[Union[CursesTextAttributesConstants,tuple[CursesTextAttributesConstants],list[CursesTextAttributesConstants]]]
89+
:type title_text_attr: Union[CursesTextAttributesConstant,CursesTextAttributesConstants]
8890
8991
:param downtime_chars:
9092
List of characters that will trigger a ``downtime_chars_delay``
9193
time second between the writing of each character.
9294
This defaults to ``(",", ".", ":", ";", "!", "?")``.
93-
:type downtime_chars: Optional[Union[tuple[str],list[str]]]
95+
:type downtime_chars: Union[tuple[str],list[str]]
9496
9597
:param downtime_chars_delay:
9698
Waiting time in seconds after writing a character contained in
9799
``downtime_chars``.
98100
This defaults to ``0.6``.
99-
:type downtime_chars_delay: Optional[Union[int,float]]
101+
:type downtime_chars_delay: Union[int,float]
100102
"""
101103

102104
def __init__(
@@ -106,10 +108,9 @@ def __init__(
106108
length: int,
107109
width: int,
108110
title: str = "",
109-
title_colors_pair_nb: CursesTextAttributesConstants = 0,
110-
title_text_attr: Union[CursesTextAttributesConstants,
111-
Tuple[CursesTextAttributesConstants],
112-
List[CursesTextAttributesConstants]] = curses.A_BOLD,
111+
title_colors_pair_nb: int = 0,
112+
title_text_attr: Union[CursesTextAttributesConstant,
113+
CursesTextAttributesConstants] = curses.A_BOLD,
113114
downtime_chars: Union[Tuple[str],
114115
List[str]] = (",", ".", ":", ";", "!", "?"),
115116
downtime_chars_delay: Union[int, float] = .6):
@@ -140,27 +141,25 @@ def __init__(
140141
self.downtime_chars_delay = downtime_chars_delay
141142

142143
#: List of accepted key codes to skip dialog. ``curses`` constants are supported. This defaults to an empty tuple.
143-
self.confirm_dialog_key: Union[Tuple[CursesKeyConstants],
144-
List[CursesKeyConstants]] = ()
144+
self.confirm_dialog_key: List[CursesKeyConstants] = []
145145
#: List of accepted key codes to raise PanicError. ``curses`` constants are supported. This defaults to an empty tuple.
146-
self.panic_key: Union[Tuple[CursesKeyConstants],
147-
List[CursesKeyConstants]] = ()
146+
self.panic_key: List[CursesKeyConstants] = []
148147

149148
@property
150-
def position(self) -> Tuple[int, int]:
149+
def position(self) -> Tuple[int]:
151150
"""Returns a tuple contains x;y position of ``TextBox``.
152151
153152
:returns: x;y position of ``TextBox``.
154-
:rtype: tuple[int, int]
153+
:rtype: tuple[int]
155154
"""
156155
return self.text_pos_x - 2, self.text_pos_y - 3
157156

158157
@property
159-
def dimensions(self) -> Tuple[int, int]:
158+
def dimensions(self) -> Tuple[int]:
160159
"""Returns a tuple contains dimensions of ``TextBox``.
161160
162161
:returns: Length and width of ``TextBox``.
163-
:rtype: tuple[int, int]
162+
:rtype: tuple[int]
164163
"""
165164
return self.length, self.width
166165

visualdialog/choices.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,9 @@
3030

3131
class ChoiceBox(DialogBox):
3232

33-
def __init__(
34-
self,
35-
pos_x: int,
36-
pos_y: int,
37-
box_length: int,
38-
box_width: int,
39-
title: str = "",
40-
title_colors_pair_nb: CursesTextAttributesConstants = 0,
41-
title_text_attributes: Tuple[CursesTextAttributesConstants] = (
42-
curses.A_BOLD, ),
43-
downtime_chars: Tuple[str] = (",", ".", ":", ";", "!", "?"),
44-
downtime_chars_delay: Union[int, float] = 0.6):
45-
super().__init__(pos_x,
46-
pos_y,
47-
box_length,
48-
box_width,
49-
title,
50-
title_colors_pair_nb,
51-
title_text_attributes,
52-
downtime_chars,
53-
downtime_chars_delay)
33+
def __init__(self,
34+
**kwargs):
35+
super().__init__(**kwargs)
5436

5537
def chain(
5638
self,

visualdialog/dialog.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
from typing import Callable, Dict, List, Optional, Tuple, Union
2929

3030
from .box import TextBox
31-
from .utils import (CursesTextAttributesConstants,
31+
from .utils import (CursesTextAttributesConstant,
32+
CursesTextAttributesConstants,
3233
TextAttributes,
3334
_make_chunk)
3435

@@ -43,11 +44,12 @@ class DialogBox(TextBox):
4344
to ``"►"``.
4445
:type end_dialog_indicator: str
4546
46-
:key kwargs: Keyword arguments correspond to the instance attributes of
47-
``TextBox``.
47+
:key kwargs: Keyword arguments correspond to the instance attributes
48+
of ``TextBox``.
4849
4950
.. NOTE::
50-
This class inherits all the methods and attributes of ``TextBox``.
51+
This class inherits all the methods and attributes of
52+
``TextBox``.
5153
5254
.. WARNING::
5355
Parameters ``downtime_chars`` and ``downtime_chars_delay`` do
@@ -78,8 +80,8 @@ def __exit__(self, type, value, traceback):
7880
def _display_end_dialog_indicator(
7981
self,
8082
stdscr,
81-
text_attr: Optional[Union[Tuple[CursesTextAttributesConstants], List[CursesTextAttributesConstants]]] = (
82-
curses.A_BOLD, curses.A_BLINK)):
83+
text_attr: CursesTextAttributesConstants = (curses.A_BOLD,
84+
curses.A_BLINK)):
8385
"""Displays an end of dialog indicator in the lower right corner
8486
of textbox.
8587
@@ -89,7 +91,7 @@ def _display_end_dialog_indicator(
8991
:param text_attr: Text attributes of
9092
``end_dialog_indicator`` method. This defaults to
9193
``(curses.A_BOLD, curses.A_BLINK)``.
92-
:type text_attr: Optional[Union[tuple[CursesTextAttributesConstants],list[CursesTextAttributesConstants]]]
94+
:type text_attr: CursesTextAttributesConstants
9395
"""
9496
if self.end_dialog_indicator_char:
9597
with TextAttributes(stdscr, *text_attr):
@@ -102,15 +104,13 @@ def char_by_char(
102104
stdscr,
103105
text: str,
104106
colors_pair_nb: int = 0,
105-
text_attr: Union[CursesTextAttributesConstants,
106-
Tuple[CursesTextAttributesConstants],
107-
List[CursesTextAttributesConstants]] = (),
108-
words_attr: Union[Dict[Tuple[str], CursesTextAttributesConstants],
109-
Dict[Tuple[str],
110-
Tuple[CursesTextAttributesConstants]]] = {},
107+
text_attr: Union[CursesTextAttributesConstant,
108+
CursesTextAttributesConstants] = (),
109+
words_attr: Union[Dict[Tuple[str], CursesTextAttributesConstant],
110+
Dict[Tuple[str], CursesTextAttributesConstants]] = {},
111111
flash_screen: bool = False,
112112
delay: Union[int, float] = .04,
113-
random_delay: Tuple[float, float] = (0, 0),
113+
random_delay: Union[Tuple[int, float], List[int, float]] = (0, 0),
114114
callback: Callable = lambda: None,
115115
cargs: Union[Tuple, List] = ()):
116116
"""Writes the given text character by character in the current
@@ -129,40 +129,40 @@ def char_by_char(
129129
corresponding to the pair of white color on black
130130
background initialized by ``curses``). This defaults to
131131
``0``.
132-
:type colors_pair_nb: Optional[int]
132+
:type colors_pair_nb: int
133133
134134
:param text_attr: Dialog box curses text attributes. It should
135135
be a single curses text attribute or a tuple of curses text
136136
attribute. This defaults an empty tuple.
137-
:type text_attr: Optional[Union[CursesTextAttributesConstants,tuple[CursesTextAttributesConstants],list[CursesTextAttributesConstants]]]
137+
:type text_attr: Union[CursesTextAttributesConstant,CursesTextAttributesConstants]
138138
139139
:param words_attr: This defaults to an empty dictionary.
140-
:type words_atttr: Union[Dict[tuple[str],CursesTextAttributesConstants],Dict[tuple[str],tuple[CursesTextAttributesConstants]]]
140+
:type words_atttr: Union[dict[Tuple[str], CursesTextAttributesConstant],dict[Tuple[str], CursesTextAttributesConstants]]
141141
142142
:param flash_screen: Allows or not to flash screen with a short
143143
light effect done before writing the first character by
144144
``flash`` function from ``curses`` module. This defaults to
145145
``False``.
146-
:type flash_screen: Optional[bool]
146+
:type flash_screen: bool
147147
148148
:param delay: Waiting time between the writing of each character
149149
of text in second. This defaults to ``0.04``.
150-
:type delay: Optional[Union[int, float]]
150+
:type delay: Union[int, float]
151151
152152
:param random_delay: Waiting time between the writing of each
153153
character in seconds where time waited is a random number
154154
generated in ``random_delay`` interval. This defaults to
155155
``(0, 0)``.
156-
:type random_delay: Optional[tuple[float, flot],list[float, float]]
156+
:type random_delay: Union[tuple[int, float],list[int, float]]
157157
158158
:param callback: Callable called after writing a character and
159159
the delay time has elapsed. This defaults to a lambda which
160160
do nothing.
161-
:type callback: Optional[Callable]
161+
:type callback: Callable
162162
163163
:param cargs: All the arguments that will be passed to callback.
164164
This defaults to an empty tuple.
165-
:type cargs: Optional[Union[tuple[Any],list[Any]]]
165+
:type cargs: Union[tuple,list]
166166
167167
.. NOTE::
168168
Method flow:
@@ -182,7 +182,8 @@ def char_by_char(
182182
If the volume of text displayed is too large to be contained
183183
in a dialog box, text will be automatically cut into
184184
paragraphs using ``textwrap.wrap`` function. See
185-
`textwrap module documentation <https://docs.python.org/fr/3.8/library/textwrap.html#textwrap.wrap>`_.
185+
`textwrap module documentation
186+
<https://docs.python.org/fr/3.8/library/textwrap.html#textwrap.wrap>`_.
186187
for more information of the behavior of text wrap.
187188
188189
.. WARNING::
@@ -248,15 +249,13 @@ def word_by_word(
248249
text: str,
249250
colors_pair_nb: int = 0,
250251
cut_char: str = " ",
251-
text_attr: Union[CursesTextAttributesConstants,
252-
Tuple[CursesTextAttributesConstants],
253-
List[CursesTextAttributesConstants]] = (),
254-
words_attr: Union[Dict[Tuple[str], CursesTextAttributesConstants],
255-
Dict[Tuple[str],
256-
Tuple[CursesTextAttributesConstants]]] = {},
252+
text_attr: Union[CursesTextAttributesConstant,
253+
CursesTextAttributesConstants] = (),
254+
words_attr: Union[Dict[Tuple[str], CursesTextAttributesConstant],
255+
Dict[Tuple[str], CursesTextAttributesConstants]] = {},
257256
flash_screen: bool = False,
258257
delay: Union[int, float] = .15,
259-
random_delay: Tuple[float, float] = (0, 0),
258+
random_delay: Union[Tuple[int, float], List[int, float]] = (0, 0),
260259
callback: Callable = lambda: None,
261260
cargs: Union[Tuple, List] = ()):
262261
"""Writes the given text word by word at position in the current
@@ -275,15 +274,15 @@ def word_by_word(
275274
the text. The number zero corresponding to the pair of
276275
white color on black background initialized by ``curses``).
277276
This defaults to ``0``.
278-
:type colors_pair_nb: Optional[int]
277+
:type colors_pair_nb: int
279278
280279
:param text_attr: Dialog box curses text attributes. It should
281280
be a single curses text attribute or a tuple of curses text
282281
attribute. This defaults an empty tuple.
283-
:type text_attr: Optional[Union[CursesTextAttributesConstants,tuple[CursesTextAttributesConstants],list[CursesTextAttributesConstants]]]
282+
:type text_attr: Union[CursesTextAttributesConstant,CursesTextAttributesConstants]
284283
285284
:param words_attr: This defaults to an empty dictionary.
286-
:type words_atttr: Union[Dict[tuple[str],CursesTextAttributesConstants],Dict[tuple[str],tuple[CursesTextAttributesConstants]]]
285+
:type words_atttr: Union[dict[Tuple[str], CursesTextAttributesConstant],dict[Tuple[str], CursesTextAttributesConstants]]
287286
288287
:param cut_char: The delimiter according which to split the text
289288
in word. This defaults to ``" "``.
@@ -293,26 +292,26 @@ def word_by_word(
293292
light effect done before writing the first character by
294293
``flash`` function from ``curses`` module. This defaults to
295294
``False``.
296-
:type flash_screen: Optional[bool]
295+
:type flash_screen: bool
297296
298297
:param delay: Waiting time between the writing of each word of
299298
``text`` in second. This defaults to ``0.15``.
300-
:type delay: Optional[Union[int, float]]
299+
:type delay: Union[int, float]
301300
302301
:param random_delay: Waiting time between the writing of each
303302
word in seconds where time waited is a random number
304303
generated in ``random_delay`` interval. This defaults to
305304
``(0, 0)``.
306-
:type random_delay: Optional[tuple[float, float],list[float, float]]
305+
:type random_delay: Union[tuple[int, float],list[int, float]]
307306
308307
:param callback: Callable called after writing a word and the
309308
delay time has elapsed. This defaults to a lambda which do
310309
nothing.
311-
:type callback: Optional[Callable]
310+
:type callback: Callable
312311
313312
:param cargs: All the arguments that will be passed to callback.
314313
This defaults to an empty tuple.
315-
:type cargs: Optional[Union[tuple[Any],list[Any]]]
314+
:type cargs: Union[tuple,list]
316315
317316
.. NOTE::
318317
Method flow:
@@ -333,7 +332,8 @@ def word_by_word(
333332
If the volume of text displayed is too large to be contained
334333
in a dialog box, text will be automatically cut into
335334
paragraphs using ``textwrap.wrap`` function. See
336-
`textwrap module documentation <https://docs.python.org/fr/3.8/library/textwrap.html#textwrap.wrap>`_
335+
`textwrap module documentation
336+
<https://docs.python.org/fr/3.8/library/textwrap.html#textwrap.wrap>`_
337337
for more information of the behavior of text wrap.
338338
339339
.. WARNING::

visualdialog/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424

2525
# curses text attribute constants are integers.
2626
# See https://docs.python.org/3/library/curses.html?#constants
27-
CursesTextAttributesConstants = int
27+
CursesTextAttributesConstant = int
28+
CursesTextAttributesConstants = Union[Tuple[int], List[int]]
2829

2930
# curses key constants are integers.
3031
# See https://docs.python.org/3/library/curses.html?#constants
31-
CursesKeyConstants = int
32+
CursesKeyConstant = int
33+
CursesKeyConstants = Union[Tuple[int], List[int]]
34+
3235

3336
def _make_chunk(iterable: Union[Tuple, List],
3437
chunk_length: int) -> Generator:

0 commit comments

Comments
 (0)