Skip to content

Commit 8101fa8

Browse files
authored
Merge pull request #595 from Alex-CodeLab/flake8_fix
fix most of #544 and some minor refactoring.
2 parents 3168b3a + 0c1365e commit 8101fa8

23 files changed

Lines changed: 99 additions & 94 deletions

‎rope/base/change.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self, description, timestamp=None):
6060
self.description = description
6161
self.time = timestamp
6262

63-
def do(self, job_set=taskhandle.NullJobSet()):
63+
def do(self, job_set=taskhandle.DEFAULT_JOB_SET):
6464
try:
6565
done = []
6666
for change in self.changes:
@@ -72,7 +72,7 @@ def do(self, job_set=taskhandle.NullJobSet()):
7272
change.undo()
7373
raise
7474

75-
def undo(self, job_set=taskhandle.NullJobSet()):
75+
def undo(self, job_set=taskhandle.DEFAULT_JOB_SET):
7676
try:
7777
done = []
7878
for change in reversed(self.changes):
@@ -123,7 +123,7 @@ def _handle_job_set(function):
123123
methods of `Change`.
124124
"""
125125

126-
def call(self, job_set=taskhandle.NullJobSet()):
126+
def call(self, job_set=taskhandle.DEFAULT_JOB_SET):
127127
job_set.started_job(str(self))
128128
function(self)
129129
job_set.finished_job()

‎rope/base/history.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _load_history(self):
2323
for data in result[1]:
2424
self._redo_list.append(to_change(data))
2525

26-
def do(self, changes, task_handle=taskhandle.NullTaskHandle()):
26+
def do(self, changes, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
2727
"""Perform the change and add it to the `self.undo_list`
2828
2929
Note that uninteresting changes (changes to ignored files)
@@ -50,7 +50,7 @@ def _is_change_interesting(self, changes):
5050
return True
5151
return False
5252

53-
def undo(self, change=None, drop=False, task_handle=taskhandle.NullTaskHandle()):
53+
def undo(self, change=None, drop=False, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
5454
"""Redo done changes from the history
5555
5656
When `change` is `None`, the last done change will be undone.
@@ -75,7 +75,7 @@ def undo(self, change=None, drop=False, task_handle=taskhandle.NullTaskHandle())
7575
del self.redo_list[-len(dependencies) :]
7676
return result
7777

78-
def redo(self, change=None, task_handle=taskhandle.NullTaskHandle()):
78+
def redo(self, change=None, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
7979
"""Redo undone changes from the history
8080
8181
When `change` is `None`, the last undone change will be

‎rope/base/libutils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def analyze_module(project, resource):
6767
project.pycore.analyze_module(resource)
6868

6969

70-
def analyze_modules(project, task_handle=taskhandle.NullTaskHandle()):
70+
def analyze_modules(project, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
7171
"""Perform static object analysis on all python files in the project
7272
7373
Note that this might be really time consuming.

‎rope/base/oi/doa.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import contextlib
23
import hashlib
34
import hmac
45

@@ -122,7 +123,7 @@ def kill_process(self):
122123
"""Stop the process"""
123124
if self.process.poll() is not None:
124125
return
125-
try:
126+
with contextlib.suppress(OSError):
126127
if hasattr(self.process, "terminate"):
127128
self.process.terminate()
128129
elif os.name != "nt":
@@ -132,8 +133,6 @@ def kill_process(self):
132133

133134
handle = int(self.process._handle)
134135
ctypes.windll.kernel32.TerminateProcess(handle, -1)
135-
except OSError:
136-
pass
137136

138137
def add_finishing_observer(self, observer):
139138
"""Notify this observer when execution finishes"""

‎rope/base/project.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import json
23
import os
34
import sys
@@ -65,11 +66,9 @@ def get_module(self, name, folder=None):
6566
def get_python_path_folders(self):
6667
result = []
6768
for src in self.prefs.get("python_path", []) + sys.path:
68-
try:
69+
with contextlib.suppress(exceptions.ResourceNotFoundError):
6970
src_folder = get_no_project().get_resource(src)
7071
result.append(src_folder)
71-
except exceptions.ResourceNotFoundError:
72-
pass
7372
return result
7473

7574
# INFO: It was decided not to cache source folders, since:
@@ -107,7 +106,7 @@ def remove_observer(self, observer):
107106
if observer in self.observers:
108107
self.observers.remove(observer)
109108

110-
def do(self, changes, task_handle=taskhandle.NullTaskHandle()):
109+
def do(self, changes, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
111110
"""Apply the changes in a `ChangeSet`
112111
113112
Most of the time you call this function for committing the
@@ -253,9 +252,8 @@ def _get_resource_path(self, name):
253252
return os.path.join(self._address, *name.split("/"))
254253

255254
def _init_ropefolder(self):
256-
if self.ropefolder is not None:
257-
if not self.ropefolder.exists():
258-
self._create_recursively(self.ropefolder)
255+
if self.ropefolder is not None and not self.ropefolder.exists():
256+
self._create_recursively(self.ropefolder)
259257

260258
def _create_recursively(self, folder):
261259
if folder.parent != self.root and not folder.parent.exists():

‎rope/base/pycore.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import bisect
2+
import contextlib
23
import difflib
34
import warnings
45

@@ -207,7 +208,8 @@ def analyze_module(
207208
self, pymodule, should_analyze, search_subscopes, followed_calls
208209
)
209210

210-
def get_classes(self, task_handle=taskhandle.NullTaskHandle()):
211+
def get_classes(self, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
212+
211213
warnings.warn(
212214
"`PyCore.get_classes()` is deprecated", DeprecationWarning, stacklevel=2
213215
)
@@ -288,7 +290,7 @@ def get_pymodule(self, name):
288290
def perform_soa_on_changed_scopes(project, resource, old_contents):
289291
pycore = project.pycore
290292
if resource.exists() and pycore.is_python_file(resource):
291-
try:
293+
with contextlib.suppress(exceptions.ModuleSyntaxError):
292294
new_contents = resource.read()
293295
# detecting changes in new_contents relative to old_contents
294296
detector = _TextChangeDetector(new_contents, old_contents)
@@ -304,8 +306,6 @@ def should_analyze(pydefined):
304306
return detector.consume_changes(start, end)
305307

306308
pycore.analyze_module(resource, should_analyze, search_subscopes)
307-
except exceptions.ModuleSyntaxError:
308-
pass
309309

310310

311311
class _TextChangeDetector:

‎rope/base/pynamesdef.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
import rope.base.oi.soi
24
import rope.base.pyobjects
35
from rope.base import pynames, utils
@@ -28,10 +30,8 @@ def get_object(self):
2830
def get_definition_location(self):
2931
"""Returns a (module, lineno) tuple"""
3032
if self.lineno is None and self.assignments:
31-
try:
33+
with contextlib.suppress(AttributeError):
3234
self.lineno = self.assignments[0].get_lineno()
33-
except AttributeError:
34-
pass
3535
return (self.module, self.lineno)
3636

3737
def invalidate(self):

‎rope/base/taskhandle.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ def get_name(self):
213213

214214
def increment(self):
215215
pass
216+
217+
218+
DEFAULT_TASK_HANDLE = NullTaskHandle()
219+
DEFAULT_JOB_SET = NullJobSet()

‎rope/contrib/autoimport/pickle.py‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
sqlite-based storage backend (rope.contrib.autoimport.sqlite.AutoImport).
1111
"""
1212

13+
14+
import contextlib
1315
import re
1416

1517
from rope.base import (
@@ -63,9 +65,12 @@ def import_assist(self, starting):
6365
# XXX: breaking if gave up! use generators
6466
result = []
6567
for module in self.names:
66-
for global_name in self.names[module]:
67-
if global_name.startswith(starting):
68-
result.append((global_name, module))
68+
result.extend(
69+
(global_name, module)
70+
for global_name in self.names[module]
71+
if global_name.startswith(starting)
72+
)
73+
6974
return result
7075

7176
def get_modules(self, name):
@@ -84,7 +89,7 @@ def get_name_locations(self, name):
8489
result = []
8590
for module in self.names:
8691
if name in self.names[module]:
87-
try:
92+
with contextlib.suppress(exceptions.ModuleNotFoundError):
8893
pymodule = self.project.get_module(module)
8994
if name in pymodule:
9095
pyname = pymodule[name]
@@ -93,12 +98,13 @@ def get_name_locations(self, name):
9398
resource = module.get_module().get_resource()
9499
if resource is not None and lineno is not None:
95100
result.append((resource, lineno))
96-
except exceptions.ModuleNotFoundError:
97-
pass
98101
return result
99102

100103
def generate_cache(
101-
self, resources=None, underlined=None, task_handle=taskhandle.NullTaskHandle()
104+
self,
105+
resources=None,
106+
underlined=None,
107+
task_handle=taskhandle.DEFAULT_TASK_HANDLE,
102108
):
103109
"""Generate global name cache for project files
104110
@@ -118,7 +124,7 @@ def generate_cache(
118124
job_set.finished_job()
119125

120126
def generate_modules_cache(
121-
self, modules, underlined=None, task_handle=taskhandle.NullTaskHandle()
127+
self, modules, underlined=None, task_handle=taskhandle.DEFAULT_TASK_HANDLE
122128
):
123129
"""Generate global name cache for modules listed in `modules`"""
124130
job_set = task_handle.create_jobset(
@@ -164,23 +170,19 @@ def find_insertion_line(self, code):
164170

165171
def update_resource(self, resource, underlined=None):
166172
"""Update the cache for global names in `resource`"""
167-
try:
173+
with contextlib.suppress(exceptions.ModuleSyntaxError):
168174
pymodule = self.project.get_pymodule(resource)
169175
modname = self._module_name(resource)
170176
self._add_names(pymodule, modname, underlined)
171-
except exceptions.ModuleSyntaxError:
172-
pass
173177

174178
def update_module(self, modname, underlined=None):
175179
"""Update the cache for global names in `modname` module
176180
177181
`modname` is the name of a module.
178182
"""
179-
try:
183+
with contextlib.suppress(exceptions.ModuleNotFoundError):
180184
pymodule = self.project.get_module(modname)
181185
self._add_names(pymodule, modname, underlined)
182-
except exceptions.ModuleNotFoundError:
183-
pass
184186

185187
def _module_name(self, resource):
186188
return libutils.modname(resource)

‎rope/contrib/autoimport/sqlite.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""AutoImport module for rope."""
2+
3+
import contextlib
24
import re
35
import sqlite3
46
import sys
@@ -153,7 +155,7 @@ def search_full(
153155
self,
154156
name: str,
155157
exact_match: bool = False,
156-
ignored_names: Set[str] = set(),
158+
ignored_names: Optional[Set[str]] = None,
157159
) -> Generator[SearchResult, None, None]:
158160
"""
159161
Search both modules and names for an import string.
@@ -172,6 +174,8 @@ def search_full(
172174
__________
173175
Unsorted Generator of SearchResults. Each is guaranteed to be unique.
174176
"""
177+
if ignored_names is None:
178+
ignored_names = set()
175179
results = set(self._search_name(name, exact_match))
176180
results = results.union(self._search_module(name, exact_match))
177181
for result in results:
@@ -256,9 +260,9 @@ def _dump_all(self) -> Tuple[List[Name], List[Package]]:
256260

257261
def generate_cache(
258262
self,
259-
resources: List[Resource] = None,
263+
resources: Optional[List[Resource]] = None,
260264
underlined: bool = False,
261-
task_handle: taskhandle.BaseTaskHandle = taskhandle.NullTaskHandle(),
265+
task_handle: taskhandle.BaseTaskHandle = taskhandle.DEFAULT_TASK_HANDLE,
262266
):
263267
"""Generate global name cache for project files.
264268
@@ -287,8 +291,8 @@ def generate_cache(
287291

288292
def generate_modules_cache(
289293
self,
290-
modules: List[str] = None,
291-
task_handle: taskhandle.BaseTaskHandle = taskhandle.NullTaskHandle(),
294+
modules: Optional[List[str]] = None,
295+
task_handle: taskhandle.BaseTaskHandle = taskhandle.DEFAULT_TASK_HANDLE,
292296
single_thread: bool = False,
293297
underlined: Optional[bool] = None,
294298
):
@@ -310,7 +314,7 @@ def generate_modules_cache(
310314

311315
existing = self._get_packages_from_cache()
312316
packages = list(filter_packages(packages, underlined, existing))
313-
if len(packages) == 0:
317+
if not packages:
314318
return
315319
self._add_packages(packages)
316320
job_set = task_handle.create_jobset("Generating autoimport cache", 0)
@@ -354,7 +358,7 @@ def get_name_locations(self, name):
354358
models.Name.search_by_name_like.select("module"), (name,)
355359
).fetchall()
356360
for module in modules:
357-
try:
361+
with contextlib.suppress(exceptions.ModuleNotFoundError):
358362
module_name = module[0]
359363
if module_name.startswith(f"{self.project_package.name}."):
360364
module_name = ".".join(module_name.split("."))
@@ -366,8 +370,6 @@ def get_name_locations(self, name):
366370
resource = module.get_module().get_resource()
367371
if resource is not None and lineno is not None:
368372
result.append((resource, lineno))
369-
except exceptions.ModuleNotFoundError:
370-
pass
371373
return result
372374

373375
def clear_cache(self):

0 commit comments

Comments
 (0)