Skip to content

Commit f4562f9

Browse files
Dídac Colldidix21
authored andcommitted
GH-84: Add types to mdutils methods
1 parent 16bcd5a commit f4562f9

File tree

9 files changed

+68
-65
lines changed

9 files changed

+68
-65
lines changed

mdutils/fileutils/fileutils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# This file is part of mdutils. https://github.com/didix21/mdutils
66
#
77
# MIT License: (C) 2018 Dídac Coll
8-
8+
from typing import Optional
99

1010
class MarkDownFile(object):
1111
"""MarkDownFile class creates a new file of MarkDown extension.
@@ -15,7 +15,7 @@ class MarkDownFile(object):
1515
- Rewrite a file with new data.
1616
- Write at the end of the file."""
1717

18-
def __init__(self, name='', dirname: str = None):
18+
def __init__(self, name='', dirname: Optional[str] = None):
1919
"""Creates a markdown file, if name is not empty.
2020
:param str name: provide the file name or a path joinly with the file name. For example: `./my-path/my-file.md`.
2121
:param str dirname: use dirname if you want to provide the path separately from the name.
@@ -26,27 +26,27 @@ def __init__(self, name='', dirname: str = None):
2626
self.file = open(f'{self.file_name}', 'w+', encoding='UTF-8')
2727
self.file.close()
2828

29-
def _get_file_name(self, name: str, dirname: str = None ) -> str:
29+
def _get_file_name(self, name: str, dirname: Optional[str] = None ) -> str:
3030
if dirname:
3131
return f'{dirname}/{name}' if name.endswith('.md') else f'{dirname}/{name}.md'
3232

3333
return name if name.endswith('.md') else f'{name}.md'
3434

35-
def rewrite_all_file(self, data):
35+
def rewrite_all_file(self, data: str):
3636
"""Rewrite all the data of a Markdown file by ``data``.
3737
3838
:param str data: is a string containing all the data that is written in the markdown file."""
3939
with open(f'{self.file_name}', 'w', encoding='utf-8') as self.file:
4040
self.file.write(data)
4141

42-
def append_end(self, data):
42+
def append_end(self, data: str):
4343
"""Write at the last position of a Markdown file.
4444
4545
:param str data: is a string containing all the data that is written in the markdown file."""
4646
with open(f'{self.file_name}', 'a', encoding='utf-8') as self.file:
4747
self.file.write(data)
4848

49-
def append_after_second_line(self, data):
49+
def append_after_second_line(self, data: str):
5050
"""Write after the file's first line.
5151
5252
:param str data: is a string containing all the data that is written in the markdown file."""

mdutils/mdutils.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from mdutils.tools.TextUtils import TextUtils
2828
from mdutils.tools.MDList import MDList, MDCheckbox
2929
from textwrap import fill
30+
from typing import Union, Optional, List
3031

3132

3233
class MdUtils:
@@ -44,7 +45,7 @@ class MdUtils:
4445
- **file_data_text:** contains all the file data that will be written on the markdown file.
4546
"""
4647

47-
def __init__(self, file_name, title="", author=""):
48+
def __init__(self, file_name: str, title: str = '', author: str = ''):
4849
"""
4950
5051
:param file_name: it is the name of the Markdown file.
@@ -65,7 +66,7 @@ def __init__(self, file_name, title="", author=""):
6566
self.reference = Reference()
6667
self.image = Image(reference=self.reference)
6768

68-
def create_md_file(self):
69+
def create_md_file(self) -> MarkDownFile:
6970
"""It creates a new Markdown file.
7071
:return: return an instance of a MarkDownFile."""
7172
md_file = MarkDownFile(self.file_name)
@@ -80,7 +81,7 @@ def get_md_text(self) -> str:
8081
:return: return a string with the markdown text."""
8182
return self.title + self.table_of_contents + self.file_data_text + self.reference.get_references_as_markdown()
8283

83-
def read_md_file(self, file_name):
84+
def read_md_file(self, file_name: str) -> str:
8485
"""Reads a Markdown file and save it to global class `file_data_text`.
8586
8687
:param file_name: Markdown file's name that has to be read.
@@ -93,7 +94,7 @@ def read_md_file(self, file_name):
9394

9495
return file_data
9596

96-
def new_header(self, level, title, style='atx', add_table_of_contents='y', header_id=''):
97+
def new_header(self, level: int, title: str, style: str = 'atx', add_table_of_contents: str = 'y', header_id: str = '') -> str:
9798
"""Add a new header to the Markdown file.
9899
99100
:param level: Header level. *atx* style can take values 1 til 6 and *setext* style take values 1 and 2.
@@ -122,7 +123,7 @@ def new_header(self, level, title, style='atx', add_table_of_contents='y', heade
122123
self.___update_file_data(self.header.choose_header(level, title, style, header_id))
123124
return self.header.choose_header(level, title, style, header_id)
124125

125-
def __add_new_item_table_of_content(self, level, item):
126+
def __add_new_item_table_of_content(self, level: int, item: Union[List[str], str]):
126127
"""Automatically add new atx headers to the table of contents.
127128
128129
:param level: add levels up to 6.
@@ -143,7 +144,7 @@ def __add_new_item_table_of_content(self, level, item):
143144
curr.append([])
144145

145146

146-
def new_table_of_contents(self, table_title="Table of contents", depth=1, marker=''):
147+
def new_table_of_contents(self, table_title: str = "Table of contents", depth: int = 1, marker: str = '') -> str:
147148
"""Table of contents can be created if Headers of 'atx' style have been defined.
148149
149150
This method allows to create a table of contents and define a title for it. Moreover, `depth` allows user to
@@ -175,7 +176,7 @@ def new_table_of_contents(self, table_title="Table of contents", depth=1, marker
175176

176177
return self.table_of_contents + marker_table_of_contents
177178

178-
def new_table(self, columns, rows, text, text_align='center', marker=''):
179+
def new_table(self, columns: int, rows: int, text: List[str], text_align: str = 'center', marker: str = '') -> str:
179180
"""This method takes a list of strings and creates a table.
180181
181182
Using arguments ``columns`` and ``rows`` allows to create a table of *n* columns and *m* rows. The
@@ -222,7 +223,7 @@ def new_table(self, columns, rows, text, text_align='center', marker=''):
222223

223224
return text_table
224225

225-
def new_paragraph(self, text='', bold_italics_code='', color='black', align='', wrap_width=0):
226+
def new_paragraph(self, text: str = '', bold_italics_code: str = '', color: str = 'black', align: str = '', wrap_width:int = 0) -> str:
226227
"""Add a new paragraph to Markdown file. The text is saved to the global variable file_data_text.
227228
228229
:param text: is a string containing the paragraph text. Optionally, the paragraph text is returned.
@@ -252,7 +253,7 @@ def new_paragraph(self, text='', bold_italics_code='', color='black', align='',
252253

253254
return self.file_data_text
254255

255-
def new_line(self, text='', bold_italics_code='', color='black', align='', wrap_width=0):
256+
def new_line(self, text: str = '', bold_italics_code: str = '', color: str = 'black', align: str = '', wrap_width: int = 0) -> str:
256257
"""Add a new line to Markdown file. The text is saved to the global variable file_data_text.
257258
258259
:param text: is a string containing the paragraph text. Optionally, the paragraph text is returned.
@@ -281,7 +282,7 @@ def new_line(self, text='', bold_italics_code='', color='black', align='', wrap_
281282

282283
return self.file_data_text
283284

284-
def write(self, text='', bold_italics_code='', color='black', align='', marker='', wrap_width=0):
285+
def write(self, text: str = '', bold_italics_code: str = '', color: str = 'black', align: str = '', marker: str = '', wrap_width: int = 0) -> str:
285286
"""Write text in ``file_Data_text`` string.
286287
287288
:param text: a text a string.
@@ -314,7 +315,7 @@ def write(self, text='', bold_italics_code='', color='black', align='', marker='
314315

315316
return new_text
316317

317-
def insert_code(self, code, language=''):
318+
def insert_code(self, code: str, language: str = '') -> str:
318319
"""This method allows to insert a peace of code on a markdown file.
319320
320321
:param code: code string.
@@ -328,7 +329,7 @@ def insert_code(self, code, language=''):
328329
self.___update_file_data(md_code)
329330
return md_code
330331

331-
def create_marker(self, text_marker):
332+
def create_marker(self, text_marker: str) -> str:
332333
"""This will add a marker to ``file_data_text`` and returns the marker result in order to be used whenever
333334
you need.
334335
@@ -345,7 +346,7 @@ def create_marker(self, text_marker):
345346
self.___update_file_data(new_marker)
346347
return new_marker
347348

348-
def place_text_using_marker(self, text, marker):
349+
def place_text_using_marker(self, text: str, marker: str) -> str:
349350
"""It replace a previous marker created with ``create_marker`` with a text string.
350351
351352
This method is going to search for the ``marker`` argument, which has been created previously using
@@ -363,7 +364,7 @@ def place_text_using_marker(self, text, marker):
363364
def ___update_file_data(self, file_data):
364365
self.file_data_text += file_data
365366

366-
def new_inline_link(self, link, text=None, bold_italics_code='', align=''):
367+
def new_inline_link(self, link: str, text: Optional[str] = None, bold_italics_code: str = '', align: str = '') -> str:
367368
"""Creates a inline link in markdown format.
368369
369370
:param link:
@@ -392,7 +393,7 @@ def new_inline_link(self, link, text=None, bold_italics_code='', align=''):
392393

393394
return Inline.new_link(link=link, text=n_text)
394395

395-
def new_reference_link(self, link, text, reference_tag=None, bold_italics_code='', align=''):
396+
def new_reference_link(self, link: str, text: str, reference_tag: Optional[str] = None, bold_italics_code: str = '', align: str = '') -> str:
396397
"""Creates a reference link in markdown format. All references will be stored at the end of the markdown file.
397398
398399
@@ -444,7 +445,7 @@ def new_reference_link(self, link, text, reference_tag=None, bold_italics_code='
444445
return self.reference.new_link(link=link, text=n_text, reference_tag=reference_tag)
445446

446447
@staticmethod
447-
def new_inline_image(text, path):
448+
def new_inline_image(text: str, path: str) -> str:
448449
"""Add inline images in a markdown file. For example ``[MyImage](../MyImage.jpg)``.
449450
450451
:param text: Text that is going to be displayed in the markdown file as a iamge.
@@ -458,7 +459,7 @@ def new_inline_image(text, path):
458459

459460
return Image.new_inline_image(text=text, path=path)
460461

461-
def new_reference_image(self, text, path, reference_tag=None):
462+
def new_reference_image(self, text: str, path: str, reference_tag: Optional[str] = None) -> str:
462463
"""Add reference images in a markdown file. For example ``[MyImage][my_image]``. All references will be stored
463464
at the end of the markdown file.
464465
@@ -476,7 +477,7 @@ def new_reference_image(self, text, path, reference_tag=None):
476477
"""
477478
return self.image.new_reference_image(text=text, path=path, reference_tag=reference_tag)
478479

479-
def new_list(self, items: [str], marked_with: str = "-"):
480+
def new_list(self, items: List[str], marked_with: str = "-"):
480481
"""Add unordered or ordered list in MarkDown file.
481482
482483
:param items: Array of items for generating the list.
@@ -489,7 +490,7 @@ def new_list(self, items: [str], marked_with: str = "-"):
489490
mdlist = MDList(items, marked_with)
490491
self.___update_file_data('\n' + mdlist.get_md())
491492

492-
def new_checkbox_list(self, items: [str], checked: bool = False):
493+
def new_checkbox_list(self, items: List[str], checked: bool = False):
493494
"""Add checkbox list in MarkDown file.
494495
495496
:param items: Array of items for generating the checkbox list.

mdutils/tools/Header.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Header:
2525
# * Atx-Style *
2626
# ********************************************************************
2727
@staticmethod
28-
def atx_level_1(title, header_id=''):
28+
def atx_level_1(title: str, header_id: str = '') -> str:
2929
"""Return a atx level 1 header.
3030
3131
:param str title: text title.
@@ -39,7 +39,7 @@ def atx_level_1(title, header_id=''):
3939
return '\n# ' + title + header_id + '\n'
4040

4141
@staticmethod
42-
def atx_level_2(title, header_id=''):
42+
def atx_level_2(title: str, header_id: str = '') -> str:
4343
"""Return a atx level 2 header.
4444
4545
:param str title: text title.
@@ -53,7 +53,7 @@ def atx_level_2(title, header_id=''):
5353
return '\n## ' + title + header_id + '\n'
5454

5555
@staticmethod
56-
def atx_level_3(title, header_id=''):
56+
def atx_level_3(title: str, header_id: str = '') -> str:
5757
"""Return a atx level 3 header.
5858
5959
:param str title: text title.
@@ -67,7 +67,7 @@ def atx_level_3(title, header_id=''):
6767
return '\n### ' + title + header_id + '\n'
6868

6969
@staticmethod
70-
def atx_level_4(title, header_id=''):
70+
def atx_level_4(title: str, header_id: str = '') -> str:
7171
"""Return a atx level 4 header.
7272
7373
:param str title: text title.
@@ -81,7 +81,7 @@ def atx_level_4(title, header_id=''):
8181
return '\n#### ' + title + header_id + '\n'
8282

8383
@staticmethod
84-
def atx_level_5(title, header_id=''):
84+
def atx_level_5(title: str, header_id: str = '') -> str:
8585
"""Return a atx level 5 header.
8686
8787
:param str title: text title.
@@ -95,7 +95,7 @@ def atx_level_5(title, header_id=''):
9595
return '\n##### ' + title + header_id + '\n'
9696

9797
@staticmethod
98-
def atx_level_6(title, header_id=''):
98+
def atx_level_6(title: str, header_id: str = '') -> str:
9999
"""Return a atx level 6 header.
100100
101101
:param str title: text title.
@@ -112,7 +112,7 @@ def atx_level_6(title, header_id=''):
112112
# * Setext-Style *
113113
# ********************************************************************
114114
@staticmethod
115-
def setext_level_1(title):
115+
def setext_level_1(title: str) -> str:
116116
"""Return a setext level 1 header.
117117
118118
:param str title: text title.
@@ -123,7 +123,7 @@ def setext_level_1(title):
123123
return '\n' + title + '\n' + ''.join(['=' for _ in title]) + '\n'
124124

125125
@staticmethod
126-
def setext_level_2(title):
126+
def setext_level_2(title: str) -> str:
127127
"""Return a setext level 1 header.
128128
129129
:param str title: text title.
@@ -134,7 +134,7 @@ def setext_level_2(title):
134134
return '\n' + title + '\n' + ''.join(['-' for _ in title]) + '\n'
135135

136136
@staticmethod
137-
def header_anchor(text, link=""):
137+
def header_anchor(text: str, link: str = '') -> str:
138138
"""Creates an internal link of a defined Header level 1 or level 2 in the markdown file.
139139
140140
Giving a text string an text link you can create an internal link of already existing header. If the ``link``
@@ -160,7 +160,7 @@ def header_anchor(text, link=""):
160160
return '[' + text + '](' + link + ')'
161161

162162
@staticmethod
163-
def choose_header(level, title, style='atx', header_id=''):
163+
def choose_header(level: int, title: str, style: str = 'atx', header_id: str = '') -> str:
164164
# noinspection SpellCheckingInspection
165165
"""This method choose the style and the header level.
166166

mdutils/tools/Image.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
class Image:
1313

14-
def __init__(self, reference):
14+
def __init__(self, reference: Reference):
1515
"""
1616
:param reference:
1717
:type reference: Reference
1818
"""
1919
self.reference = reference
2020

2121
@staticmethod
22-
def new_inline_image(text, path, tooltip=None):
22+
def new_inline_image(text: str, path: str, tooltip: str = None) -> str:
2323
"""
2424
:param text: Text that is going to be displayed in the markdown file as a iamge.
2525
:type text: str
@@ -32,7 +32,7 @@ def new_inline_image(text, path, tooltip=None):
3232
"""
3333
return '!' + Inline.new_link(link=path, text=text, tooltip=tooltip)
3434

35-
def new_reference_image(self, text, path, reference_tag=None, tooltip=None):
35+
def new_reference_image(self, text: str, path: str, reference_tag: str = None, tooltip: str = None) -> str:
3636
"""
3737
:param text: Text that is going to be displayed in the markdown file as a image.
3838
:type text: str

0 commit comments

Comments
 (0)