Skip to content

Commit 4def731

Browse files
committed
Addresses mutable default argument warnings
1 parent 313b566 commit 4def731

File tree

4 files changed

+72
-25
lines changed

4 files changed

+72
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELONG
22

3+
## 2.0.1
4+
5+
### Fixed
6+
7+
- Fixed some mutable default argument warnings in methods and classes
8+
39
## 2.0.0
410

511
### Added

fortls/langserver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ def get_candidates(
382382
):
383383
#
384384
def child_candidates(
385-
scope, only_list=[], filter_public=True, req_abstract=False
385+
scope, only_list: list = None, filter_public=True, req_abstract=False
386386
):
387+
if only_list is None:
388+
only_list = []
387389
tmp_list = []
388390
# Filter children
389391
nonly = len(only_list)

fortls/objects.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def get_use_tree(
7979
scope: fortran_scope,
8080
use_dict: dict,
8181
obj_tree: dict,
82-
only_list: list = [],
83-
rename_map: dict = {},
84-
curr_path: list = [],
82+
only_list: list = None,
83+
rename_map: dict = None,
84+
curr_path: list = None,
8585
):
8686
def intersect_only(use_stmnt):
8787
tmp_list = []
@@ -97,6 +97,13 @@ def intersect_only(use_stmnt):
9797
tmp_map.pop(val1, None)
9898
return tmp_list, tmp_map
9999

100+
if only_list is None:
101+
only_list = []
102+
if rename_map is None:
103+
rename_map = {}
104+
if curr_path is None:
105+
curr_path = []
106+
100107
# Detect and break circular references
101108
if scope.FQSN in curr_path:
102109
return use_dict
@@ -297,15 +304,17 @@ def __init__(
297304
self,
298305
mod_name: str,
299306
line_number: int,
300-
only_list: list = [],
301-
rename_map: dict = {},
307+
only_list: list = None,
308+
rename_map: dict = None,
302309
):
303310
self.mod_name: str = mod_name.lower()
304311
self.line_number: int = line_number
305-
self.only_list: list = [only.lower() for only in only_list]
306-
self.rename_map: dict = {
307-
key.lower(): value.lower() for key, value in rename_map.items()
308-
}
312+
if only_list is not None:
313+
self.only_list: list = [only.lower() for only in only_list]
314+
if rename_map is not None:
315+
self.rename_map: dict = {
316+
key.lower(): value.lower() for key, value in rename_map.items()
317+
}
309318

310319

311320
class fortran_diagnostic:
@@ -466,15 +475,19 @@ def req_named_end(self):
466475
def check_valid_parent(self):
467476
return True
468477

469-
def check_definition(self, obj_tree, known_types={}, interface=False):
478+
def check_definition(self, obj_tree, known_types: dict = None, interface=False):
479+
if known_types is None:
480+
known_types = {}
470481
return None, known_types
471482

472483

473484
class fortran_scope(fortran_obj):
474485
def __init__(self, file_ast, line_number: int, name: str):
475486
self.base_setup(file_ast, line_number, name)
476487

477-
def base_setup(self, file_ast, sline: int, name: str, keywords: list = []):
488+
def base_setup(self, file_ast, sline: int, name: str, keywords: list = None):
489+
if keywords is None:
490+
keywords = []
478491
self.file_ast: fortran_ast = file_ast
479492
self.sline: int = sline
480493
self.eline: int = sline
@@ -514,7 +527,13 @@ def copy_from(self, copy_source):
514527
self.implicit_vars = copy_source.implicit_vars
515528
self.implicit_line = copy_source.implicit_line
516529

517-
def add_use(self, use_mod, line_number, only_list=[], rename_map={}):
530+
def add_use(
531+
self, use_mod, line_number, only_list: list = None, rename_map: dict = None
532+
):
533+
if only_list is None:
534+
only_list = []
535+
if rename_map is None:
536+
rename_map = {}
518537
self.use.append(USE_line(use_mod, line_number, only_list, rename_map))
519538

520539
def set_inherit(self, inherit_type):
@@ -841,8 +860,10 @@ def __init__(
841860
name: str,
842861
args: str = "",
843862
mod_flag: bool = False,
844-
keywords: list = [],
863+
keywords: list = None,
845864
):
865+
if keywords is None:
866+
keywords = []
846867
self.base_setup(file_ast, line_number, name, keywords=keywords)
847868
self.args: str = args.replace(" ", "")
848869
self.args_snip: str = self.args
@@ -1055,10 +1076,12 @@ def __init__(
10551076
name: str,
10561077
args: str = "",
10571078
mod_flag: bool = False,
1058-
keywords: list = [],
1079+
keywords: list = None,
10591080
return_type=None,
10601081
result_var=None,
10611082
):
1083+
if keywords is None:
1084+
keywords = []
10621085
self.base_setup(file_ast, line_number, name, keywords=keywords)
10631086
self.args: str = args.replace(" ", "").lower()
10641087
self.args_snip: str = self.args
@@ -1495,7 +1518,7 @@ class fortran_int(fortran_scope):
14951518
def __init__(
14961519
self,
14971520
file_ast: fortran_ast,
1498-
line_number: list,
1521+
line_number: int,
14991522
name: str,
15001523
abstract: bool = False,
15011524
):
@@ -1540,9 +1563,11 @@ def __init__(
15401563
name: str,
15411564
var_desc: str,
15421565
keywords: list,
1543-
keyword_info: dict = {},
1566+
keyword_info: dict = None,
15441567
link_obj=None,
15451568
):
1569+
if keyword_info is None:
1570+
keyword_info = {}
15461571
self.base_setup(
15471572
file_ast, line_number, name, var_desc, keywords, keyword_info, link_obj
15481573
)
@@ -1920,7 +1945,7 @@ def get_enc_scope_name(self):
19201945
def add_scope(
19211946
self,
19221947
new_scope: fortran_scope,
1923-
END_SCOPE_REGEX,
1948+
END_SCOPE_REGEX: Pattern[str],
19241949
exportable: bool = True,
19251950
req_container: bool = False,
19261951
):

fortls/parse_fortran.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,8 +1101,13 @@ def find_word_in_code_line(
11011101
return line_number, i0, i1
11021102

11031103
def preprocess(
1104-
self, pp_defs: dict = {}, include_dirs: list = [], debug: bool = False
1104+
self, pp_defs: dict = None, include_dirs: list = None, debug: bool = False
11051105
) -> tuple[list, list]:
1106+
if pp_defs is None:
1107+
pp_defs = {}
1108+
if include_dirs is None:
1109+
include_dirs = []
1110+
11061111
self.contents_pp, pp_skips, pp_defines, self.pp_defs = preprocess_file(
11071112
self.contents_split,
11081113
self.path,
@@ -1167,15 +1172,15 @@ def check_file(self, obj_tree, max_line_length=-1, max_comment_line_length=-1):
11671172
def preprocess_file(
11681173
contents_split: list,
11691174
file_path: str = None,
1170-
pp_defs: dict = {},
1171-
include_dirs: list = [],
1175+
pp_defs: dict = None,
1176+
include_dirs: list = None,
11721177
debug: bool = False,
11731178
):
11741179
# Look for and mark excluded preprocessor paths in file
11751180
# Initial implementation only looks for "if" and "ifndef" statements.
11761181
# For "if" statements all blocks are excluded except the "else" block if present
11771182
# For "ifndef" statements all blocks excluding the first block are excluded
1178-
def eval_pp_if(text, defs: dict = {}):
1183+
def eval_pp_if(text, defs: dict = None):
11791184
def replace_ops(expr: str):
11801185
expr = expr.replace("&&", " and ")
11811186
expr = expr.replace("||", " or ")
@@ -1212,6 +1217,8 @@ def replace_vars(line: str):
12121217
out_line = out_line.replace("$%", "False")
12131218
return out_line
12141219

1220+
if defs is None:
1221+
defs = {}
12151222
out_line = replace_defined(text)
12161223
out_line = replace_vars(out_line)
12171224
try:
@@ -1221,7 +1228,10 @@ def replace_vars(line: str):
12211228
else:
12221229
return line_res
12231230

1224-
#
1231+
if pp_defs is None:
1232+
pp_defs = {}
1233+
if include_dirs is None:
1234+
include_dirs = []
12251235
if file_path is not None:
12261236
include_dirs = [os.path.dirname(file_path)] + include_dirs
12271237
pp_skips = []
@@ -1386,14 +1396,18 @@ def replace_vars(line: str):
13861396
def process_file(
13871397
file_obj: fortran_file,
13881398
debug: bool = False,
1389-
pp_defs: dict = {},
1390-
include_dirs: list = [],
1399+
pp_defs: dict = None,
1400+
include_dirs: list = None,
13911401
):
13921402
"""Build file AST by parsing file"""
13931403

13941404
def parser_debug_msg(msg: str, line: str, ln: int):
13951405
log.debug(f"{line.strip()} !!! {msg} statement({ln})")
13961406

1407+
if pp_defs is None:
1408+
pp_defs = {}
1409+
if include_dirs is None:
1410+
include_dirs = []
13971411
# Configure the parser logger
13981412
if debug:
13991413
logging.basicConfig(

0 commit comments

Comments
 (0)