Skip to content

Commit 9b39a3d

Browse files
committed
Updates typing
1 parent cd63540 commit 9b39a3d

File tree

1 file changed

+96
-13
lines changed

1 file changed

+96
-13
lines changed

fortls/helper_functions.py

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@
2121
log = logging.getLogger(__name__)
2222

2323

24-
def expand_name(line, char_poss):
25-
"""Get full word containing given cursor position"""
24+
def expand_name(line: str, char_poss: int) -> str:
25+
"""Get full word containing given cursor position
26+
27+
Parameters
28+
----------
29+
line : str
30+
Text line
31+
char_poss : int
32+
Column position along the line
33+
34+
Returns
35+
-------
36+
str
37+
Word regex match for the input column
38+
"""
2639
# The order here is important.
2740
# WORD will capture substrings in logical and strings
2841
regexs = [LOGICAL_REGEX, SQ_STRING_REGEX, DQ_STRING_REGEX, WORD_REGEX, NUMBER_REGEX]
@@ -33,10 +46,21 @@ def expand_name(line, char_poss):
3346
return ""
3447

3548

36-
def detect_fixed_format(file_lines):
49+
def detect_fixed_format(file_lines: list[str]) -> bool:
3750
"""Detect fixed/free format by looking for characters in label columns
3851
and variable declarations before column 6. Treat intersection format
39-
files as free format."""
52+
files as free format.
53+
54+
Parameters
55+
----------
56+
file_lines : list[str]
57+
List of consecutive file lines
58+
59+
Returns
60+
-------
61+
bool
62+
True if file_lines are of Fixed Fortran style
63+
"""
4064
for line in file_lines:
4165
if FREE_FORMAT_TEST.match(line):
4266
return False
@@ -51,8 +75,20 @@ def detect_fixed_format(file_lines):
5175
return True
5276

5377

54-
def strip_line_label(line):
55-
"""Strip leading numeric line label"""
78+
def strip_line_label(line: str) -> tuple[str, str | None]:
79+
"""Strip leading numeric line label
80+
81+
Parameters
82+
----------
83+
line : str
84+
Text line
85+
86+
Returns
87+
-------
88+
tuple[str, str | None]
89+
Output string, Line label returns None if no line label present
90+
"""
91+
5692
match = LINE_LABEL_REGEX.match(line)
5793
if match is None:
5894
return line, None
@@ -62,8 +98,21 @@ def strip_line_label(line):
6298
return out_str, line_label
6399

64100

65-
def strip_strings(in_line, maintain_len=False):
66-
"""String string literals from code line"""
101+
def strip_strings(in_line: str, maintain_len: bool = False) -> str:
102+
"""Strips string literals from code line
103+
104+
Parameters
105+
----------
106+
in_line : str
107+
Text string
108+
maintain_len : bool, optional
109+
Maintain the len(in_line) in the output string, by default False
110+
111+
Returns
112+
-------
113+
str
114+
Stripped string
115+
"""
67116

68117
def repl_sq(m):
69118
return "'{0}'".format(" " * (len(m.group()) - 2))
@@ -80,12 +129,22 @@ def repl_dq(m):
80129
return out_line
81130

82131

83-
def separate_def_list(test_str):
132+
def separate_def_list(test_str: str) -> list[str] | None:
84133
"""Separate definition lists, skipping parenthesis and bracket groups
85134
86135
Examples:
87136
"var1, var2, var3" -> ["var1", "var2", "var3"]
88137
"var, init_var(3) = [1,2,3], array(3,3)" -> ["var", "init_var", "array"]
138+
139+
Parameters
140+
----------
141+
test_str : str
142+
Text string
143+
144+
Returns
145+
-------
146+
list[str] | None
147+
[description]
89148
"""
90149
stripped_str = strip_strings(test_str)
91150
paren_count = 0
@@ -111,8 +170,21 @@ def separate_def_list(test_str):
111170
return def_list
112171

113172

114-
def find_word_in_line(line, word):
115-
"""Find Fortran word in line"""
173+
def find_word_in_line(line: str, word: str) -> tuple[int, int]:
174+
"""Find Fortran word in line
175+
176+
Parameters
177+
----------
178+
line : str
179+
Text line
180+
word : str
181+
word to find in line
182+
183+
Returns
184+
-------
185+
tuple[int, int]
186+
start and end positions (indices) of the word if not found it returns
187+
-1, len(word) -1"""
116188
i0 = -1
117189
for poss_name in WORD_REGEX.finditer(line):
118190
if poss_name.group() == word:
@@ -121,9 +193,20 @@ def find_word_in_line(line, word):
121193
return i0, i0 + len(word)
122194

123195

124-
def find_paren_match(test_str):
196+
def find_paren_match(test_str: str) -> int:
125197
"""Find matching closing parenthesis by searching forward,
126-
returns -1 if no match is found"""
198+
returns -1 if no match is found
199+
200+
Parameters
201+
----------
202+
test_str : str
203+
Input string
204+
205+
Returns
206+
-------
207+
int
208+
The index of the matching `)` character in the string
209+
"""
127210
paren_count = 1
128211
ind = -1
129212
for (i, char) in enumerate(test_str):

0 commit comments

Comments
 (0)