Skip to content

Commit 72f9faf

Browse files
committed
fix more problems detected by pylint
1 parent b9de1b4 commit 72f9faf

18 files changed

+99
-98
lines changed

gdtoolkit/common/ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .utils import find_name_token_among_children
66

77

8+
# pylint: disable=too-few-public-methods
89
class Function:
910
"""Abstract representation of function"""
1011

@@ -14,6 +15,7 @@ def __init__(self, func_def: Tree):
1415
self.name = name_token.value
1516

1617

18+
# pylint: disable=too-few-public-methods
1719
class Class:
1820
"""Abstract representation of class.
1921
Since it contains sub-classes, it forms a tree"""
@@ -47,6 +49,7 @@ def _load_data_from_class_def(self, class_def: Tree) -> None:
4749
self._load_data_from_node_children(class_def)
4850

4951

52+
# pylint: disable=too-few-public-methods
5053
class AbstractSyntaxTree:
5154
"""Post-processed version of parse tree - more convenient representation
5255
for further processing"""

gdtoolkit/formatter/__main__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def _check_files_formatting(
8585
failed_files = set()
8686
for file_path in files:
8787
try:
88-
with open(file_path, "r") as fh:
89-
code = fh.read()
88+
with open(file_path, "r", encoding="utf-8") as handle:
89+
code = handle.read()
9090
success, actually_formatted, formatted_code = _format_code(
9191
code, line_length, file_path, safety_checks
9292
)
@@ -108,9 +108,9 @@ def _check_files_formatting(
108108
formattable_files.add(file_path)
109109
elif not success:
110110
failed_files.add(file_path)
111-
except OSError as e:
111+
except OSError as exceptions:
112112
print(
113-
"Cannot open file '{}': {}".format(file_path, e.strerror),
113+
"Cannot open file '{}': {}".format(file_path, exceptions.strerror),
114114
file=sys.stderr,
115115
)
116116
failed_files.add(file_path)
@@ -140,22 +140,22 @@ def _format_files(files: List[str], line_length: int, safety_checks: bool) -> No
140140
failed_files = set()
141141
for file_path in files:
142142
try:
143-
with open(file_path, "r+") as fh:
144-
code = fh.read()
143+
with open(file_path, "r+", encoding="utf-8") as handle:
144+
code = handle.read()
145145
success, actually_formatted, formatted_code = _format_code(
146146
code, line_length, file_path, safety_checks
147147
)
148148
if success and actually_formatted:
149149
print("reformatted {}".format(file_path))
150150
formatted_files.add(file_path)
151-
fh.seek(0)
152-
fh.truncate(0)
153-
fh.write(formatted_code)
151+
handle.seek(0)
152+
handle.truncate(0)
153+
handle.write(formatted_code)
154154
elif not success:
155155
failed_files.add(file_path)
156-
except OSError as e:
156+
except OSError as exceptions:
157157
print(
158-
"Cannot open file '{}': {}".format(file_path, e.strerror),
158+
"Cannot open file '{}': {}".format(file_path, exceptions.strerror),
159159
file=sys.stderr,
160160
)
161161
failed_files.add(file_path)
@@ -198,19 +198,19 @@ def _format_code(
198198
given_code_parse_tree=code_parse_tree,
199199
given_code_comment_parse_tree=comment_parse_tree,
200200
)
201-
except lark.exceptions.UnexpectedToken as e:
201+
except lark.exceptions.UnexpectedToken as exception:
202202
success = False
203203
print(
204204
f"{file_path}:\n",
205-
lark_unexpected_token_to_str(e, code),
205+
lark_unexpected_token_to_str(exception, code),
206206
sep="\n",
207207
file=sys.stderr,
208208
)
209-
except lark.exceptions.UnexpectedInput as e:
209+
except lark.exceptions.UnexpectedInput as exception:
210210
success = False
211211
print(
212212
f"{file_path}:\n",
213-
lark_unexpected_input_to_str(e),
213+
lark_unexpected_input_to_str(exception),
214214
sep="\n",
215215
file=sys.stderr,
216216
)

gdtoolkit/formatter/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# pylint: disable=too-many-arguments
88
# pylint: disable=too-many-instance-attributes
9+
# pylint: disable=too-few-public-methods
910
class Context:
1011
def __init__(
1112
self,

gdtoolkit/gd2py/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ def main():
3030
__doc__,
3131
version="gd2py {}".format(pkg_resources.get_distribution("gdtoolkit").version),
3232
)
33-
with open(arguments["<path>"], "r") as fh:
34-
print(convert_code(fh.read()))
33+
with open(arguments["<path>"], "r", encoding="utf-8") as handle:
34+
print(convert_code(handle.read()))

gdtoolkit/gdradon/__main__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def main():
4343

4444
def _cc(file_path: str) -> None:
4545
try:
46-
with open(file_path, "r") as fh:
47-
python_code = convert_code(fh.read())
46+
with open(file_path, "r", encoding="utf-8") as handle:
47+
python_code = convert_code(handle.read())
4848
results = cc_visit(python_code)
4949
if not results:
5050
return
@@ -67,13 +67,15 @@ def _cc(file_path: str) -> None:
6767
RESET,
6868
)
6969
)
70-
except OSError as e:
70+
except OSError as exception:
7171
print(
72-
"Cannot open file '{}': {}".format(file_path, e.strerror),
72+
"Cannot open file '{}': {}".format(file_path, exception.strerror),
7373
file=sys.stderr,
7474
)
75-
except Exception as e: # pylint: disable=broad-except
75+
except Exception as exception: # pylint: disable=broad-except
7676
print(
77-
"Cannot process file '{}' due to exception: {}".format(file_path, e),
77+
"Cannot process file '{}' due to exception: {}".format(
78+
file_path, exception
79+
),
7880
file=sys.stderr,
7981
)

gdtoolkit/linter/__main__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def main():
8080
def _dump_default_config() -> None:
8181
# TODO: error handling
8282
assert not os.path.isfile(CONFIG_FILE_NAME)
83-
with open(CONFIG_FILE_NAME, "w") as fh:
84-
fh.write(yaml.dump(DEFAULT_CONFIG.copy()))
83+
with open(CONFIG_FILE_NAME, "w", encoding="utf-8") as handle:
84+
handle.write(yaml.dump(DEFAULT_CONFIG.copy()))
8585
sys.exit(0)
8686

8787

@@ -105,8 +105,8 @@ def _load_config_file_or_default(config_file_path: Optional[str]) -> MappingProx
105105
# TODO: error handling
106106
if config_file_path is not None:
107107
logging.info("Config file found: '%s'", config_file_path)
108-
with open(config_file_path, "r") as fh:
109-
return yaml.load(fh.read(), Loader=yaml.Loader)
108+
with open(config_file_path, "r", encoding="utf-8") as handle:
109+
return yaml.load(handle.read(), Loader=yaml.Loader)
110110

111111
logging.info("""No 'gdlintrc' nor '.gdlintrc' found. Using default config...""")
112112
return DEFAULT_CONFIG
@@ -129,31 +129,31 @@ def _update_config_with_missing_entries_inplace(config: dict) -> None:
129129

130130
def _lint_file(file_path: str, config: MappingProxyType) -> int:
131131
try:
132-
with open(file_path, "r") as fh:
133-
content = fh.read()
132+
with open(file_path, "r", encoding="utf-8") as handle:
133+
content = handle.read()
134134
problems = lint_code(content, config)
135135
if len(problems) > 0: # TODO: friendly frontend like in halint
136136
for problem in problems:
137137
print_problem(problem, file_path)
138138
return len(problems)
139-
except OSError as e:
139+
except OSError as exception:
140140
print(
141-
"Cannot open file '{}': {}".format(file_path, e.strerror),
141+
"Cannot open file '{}': {}".format(file_path, exception.strerror),
142142
file=sys.stderr,
143143
)
144144
return 1
145-
except lark.exceptions.UnexpectedToken as e:
145+
except lark.exceptions.UnexpectedToken as exception:
146146
print(
147147
f"{file_path}:\n",
148-
lark_unexpected_token_to_str(e, content),
148+
lark_unexpected_token_to_str(exception, content),
149149
sep="\n",
150150
file=sys.stderr,
151151
)
152152
return 1
153-
except lark.exceptions.UnexpectedInput as e:
153+
except lark.exceptions.UnexpectedInput as exception:
154154
print(
155155
f"{file_path}:\n",
156-
lark_unexpected_input_to_str(e),
156+
lark_unexpected_input_to_str(exception),
157157
sep="\n",
158158
file=sys.stderr,
159159
)

gdtoolkit/linter/format_checks.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def lint(gdscript_code: str, config: MappingProxyType) -> List[Problem]:
4141
def _max_line_length_check(threshold, tab_characters, code: str) -> List[Problem]:
4242
problems = []
4343
lines = code.splitlines()
44-
for line_number in range(len(lines)):
44+
for line_number, line in enumerate(lines):
4545
assert tab_characters is not None
46-
line = lines[line_number].replace("\t", " " * tab_characters)
46+
line = line.replace("\t", " " * tab_characters)
4747
if len(line) > threshold:
4848
problems.append(
4949
Problem(
@@ -78,8 +78,7 @@ def _max_file_lines_check(threshold, code: str) -> List[Problem]:
7878
def _trailing_ws_check(code: str) -> List[Problem]:
7979
problems = []
8080
lines = code.splitlines()
81-
for line_number in range(len(lines)):
82-
line = lines[line_number]
81+
for line_number, line in enumerate(lines):
8382
if re.search(r"\s$", line) is not None:
8483
problems.append(
8584
Problem(
@@ -95,8 +94,7 @@ def _trailing_ws_check(code: str) -> List[Problem]:
9594
def _mixed_tabs_and_spaces_check(code: str) -> List[Problem]:
9695
problems = []
9796
lines = code.splitlines()
98-
for line_number in range(len(lines)):
99-
line = lines[line_number]
97+
for line_number, line in enumerate(lines):
10098
if re.search("^(\t+ +| +\t+)", line) is not None:
10199
problems.append(
102100
Problem(

gdtoolkit/parser/__main__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def main():
5050

5151
def _parse_file(file_path: str, arguments: Dict) -> bool:
5252
try:
53-
with open(file_path, "r") as fh:
54-
file_content = fh.read()
53+
with open(file_path, "r", encoding="utf-8") as handle:
54+
file_content = handle.read()
5555
return _parse_file_content(file_content, arguments, file_path)
56-
except OSError as e:
56+
except OSError as exception:
5757
print(
58-
"Cannot open file '{}': {}".format(file_path, e.strerror),
58+
"Cannot open file '{}': {}".format(file_path, exception.strerror),
5959
file=sys.stderr,
6060
)
6161
return False
@@ -65,18 +65,18 @@ def _parse_file_content(content: str, arguments: Dict, file_path: str = None) ->
6565
actual_file_path = "STDIN" if file_path is None else file_path
6666
try:
6767
tree = parser.parse(content) # TODO: handle exceptions
68-
except lark.exceptions.UnexpectedToken as e:
68+
except lark.exceptions.UnexpectedToken as exception:
6969
print(
7070
f"{actual_file_path}:\n",
71-
lark_unexpected_token_to_str(e, content),
71+
lark_unexpected_token_to_str(exception, content),
7272
sep="\n",
7373
file=sys.stderr,
7474
)
7575
return False
76-
except lark.exceptions.UnexpectedInput as e:
76+
except lark.exceptions.UnexpectedInput as exception:
7777
print(
7878
f"{actual_file_path}:\n",
79-
lark_unexpected_input_to_str(e),
79+
lark_unexpected_input_to_str(exception),
8080
sep="\n",
8181
file=sys.stderr,
8282
)

gdtoolkit/parser/parser.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Indenter(indenter.Indenter):
2121

2222

2323
# TODO: when upgrading to Python 3.8, replace with functools.cached_property
24-
class cached_property:
24+
# pylint: disable=too-few-public-methods
25+
class CachedProperty:
2526
"""A property that is only computed once per instance and then replaces
2627
itself with an ordinary attribute. Deleting the attribute resets the
2728
property.
@@ -53,6 +54,7 @@ def parse(self, code: str, gather_metadata: bool = False) -> Tree:
5354
If gather_metadata is True, parsing is slower but the returned Tree comes with
5455
line and column numbers for statements and rules.
5556
"""
57+
# pylint: disable=no-member
5658
return (
5759
self._parser_with_metadata.parse(code)
5860
if gather_metadata
@@ -61,6 +63,7 @@ def parse(self, code: str, gather_metadata: bool = False) -> Tree:
6163

6264
def parse_comments(self, code: str) -> Tree:
6365
"""Parses GDScript code and returns comments - both standalone, and inline."""
66+
# pylint: disable=no-member
6467
return self._comment_parser.parse(code)
6568

6669
def disable_grammar_caching(self) -> None:
@@ -94,15 +97,15 @@ def _get_parser(
9497

9598
return a_parser
9699

97-
@cached_property
100+
@CachedProperty
98101
def _parser(self) -> Lark:
99102
return self._get_parser("parser")
100103

101-
@cached_property
104+
@CachedProperty
102105
def _parser_with_metadata(self) -> Lark:
103106
return self._get_parser("parser_with_metadata", add_metadata=True)
104107

105-
@cached_property
108+
@CachedProperty
106109
def _comment_parser(self) -> Lark:
107110
return self._get_parser(
108111
"parser_comments", add_metadata=True, grammar_filename="comments.lark"

pylintrc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
[MESSAGES CONTROL]
22
disable =
3-
fixme, # TODO: check
4-
global-statement, # TODO: check
5-
too-few-public-methods, # TODO: check
3+
fixme,
64
missing-function-docstring, # too pedantic
7-
unspecified-encoding, # TODO: check
8-
consider-using-with, # TODO: check
95
missing-module-docstring, # too pedantic
106
missing-class-docstring, # too pedantic
11-
consider-using-f-string, # TODO: check
12-
invalid-name, # TODO: check
13-
consider-using-enumerate, # TODO: check
14-
duplicate-code # TODO: check
7+
consider-using-f-string, # TODO: fix only cases that make sense
8+
duplicate-code # TODO: check (fails on CI)

0 commit comments

Comments
 (0)