Skip to content

Commit

Permalink
Merge pull request #595 from Alex-CodeLab/flake8_fix
Browse files Browse the repository at this point in the history
fix most of #544 and some minor refactoring.
  • Loading branch information
lieryan committed Dec 28, 2022
2 parents 3168b3a + 0c1365e commit 8101fa8
Show file tree
Hide file tree
Showing 23 changed files with 99 additions and 94 deletions.
6 changes: 3 additions & 3 deletions rope/base/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init__(self, description, timestamp=None):
self.description = description
self.time = timestamp

def do(self, job_set=taskhandle.NullJobSet()):
def do(self, job_set=taskhandle.DEFAULT_JOB_SET):
try:
done = []
for change in self.changes:
Expand All @@ -72,7 +72,7 @@ def do(self, job_set=taskhandle.NullJobSet()):
change.undo()
raise

def undo(self, job_set=taskhandle.NullJobSet()):
def undo(self, job_set=taskhandle.DEFAULT_JOB_SET):
try:
done = []
for change in reversed(self.changes):
Expand Down Expand Up @@ -123,7 +123,7 @@ def _handle_job_set(function):
methods of `Change`.
"""

def call(self, job_set=taskhandle.NullJobSet()):
def call(self, job_set=taskhandle.DEFAULT_JOB_SET):
job_set.started_job(str(self))
function(self)
job_set.finished_job()
Expand Down
6 changes: 3 additions & 3 deletions rope/base/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _load_history(self):
for data in result[1]:
self._redo_list.append(to_change(data))

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

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

def redo(self, change=None, task_handle=taskhandle.NullTaskHandle()):
def redo(self, change=None, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
"""Redo undone changes from the history
When `change` is `None`, the last undone change will be
Expand Down
2 changes: 1 addition & 1 deletion rope/base/libutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def analyze_module(project, resource):
project.pycore.analyze_module(resource)


def analyze_modules(project, task_handle=taskhandle.NullTaskHandle()):
def analyze_modules(project, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
"""Perform static object analysis on all python files in the project
Note that this might be really time consuming.
Expand Down
5 changes: 2 additions & 3 deletions rope/base/oi/doa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import contextlib
import hashlib
import hmac

Expand Down Expand Up @@ -122,7 +123,7 @@ def kill_process(self):
"""Stop the process"""
if self.process.poll() is not None:
return
try:
with contextlib.suppress(OSError):
if hasattr(self.process, "terminate"):
self.process.terminate()
elif os.name != "nt":
Expand All @@ -132,8 +133,6 @@ def kill_process(self):

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

def add_finishing_observer(self, observer):
"""Notify this observer when execution finishes"""
Expand Down
12 changes: 5 additions & 7 deletions rope/base/project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import json
import os
import sys
Expand Down Expand Up @@ -65,11 +66,9 @@ def get_module(self, name, folder=None):
def get_python_path_folders(self):
result = []
for src in self.prefs.get("python_path", []) + sys.path:
try:
with contextlib.suppress(exceptions.ResourceNotFoundError):
src_folder = get_no_project().get_resource(src)
result.append(src_folder)
except exceptions.ResourceNotFoundError:
pass
return result

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

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

def _init_ropefolder(self):
if self.ropefolder is not None:
if not self.ropefolder.exists():
self._create_recursively(self.ropefolder)
if self.ropefolder is not None and not self.ropefolder.exists():
self._create_recursively(self.ropefolder)

def _create_recursively(self, folder):
if folder.parent != self.root and not folder.parent.exists():
Expand Down
8 changes: 4 additions & 4 deletions rope/base/pycore.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bisect
import contextlib
import difflib
import warnings

Expand Down Expand Up @@ -207,7 +208,8 @@ def analyze_module(
self, pymodule, should_analyze, search_subscopes, followed_calls
)

def get_classes(self, task_handle=taskhandle.NullTaskHandle()):
def get_classes(self, task_handle=taskhandle.DEFAULT_TASK_HANDLE):

warnings.warn(
"`PyCore.get_classes()` is deprecated", DeprecationWarning, stacklevel=2
)
Expand Down Expand Up @@ -288,7 +290,7 @@ def get_pymodule(self, name):
def perform_soa_on_changed_scopes(project, resource, old_contents):
pycore = project.pycore
if resource.exists() and pycore.is_python_file(resource):
try:
with contextlib.suppress(exceptions.ModuleSyntaxError):
new_contents = resource.read()
# detecting changes in new_contents relative to old_contents
detector = _TextChangeDetector(new_contents, old_contents)
Expand All @@ -304,8 +306,6 @@ def should_analyze(pydefined):
return detector.consume_changes(start, end)

pycore.analyze_module(resource, should_analyze, search_subscopes)
except exceptions.ModuleSyntaxError:
pass


class _TextChangeDetector:
Expand Down
6 changes: 3 additions & 3 deletions rope/base/pynamesdef.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import contextlib

import rope.base.oi.soi
import rope.base.pyobjects
from rope.base import pynames, utils
Expand Down Expand Up @@ -28,10 +30,8 @@ def get_object(self):
def get_definition_location(self):
"""Returns a (module, lineno) tuple"""
if self.lineno is None and self.assignments:
try:
with contextlib.suppress(AttributeError):
self.lineno = self.assignments[0].get_lineno()
except AttributeError:
pass
return (self.module, self.lineno)

def invalidate(self):
Expand Down
4 changes: 4 additions & 0 deletions rope/base/taskhandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@ def get_name(self):

def increment(self):
pass


DEFAULT_TASK_HANDLE = NullTaskHandle()
DEFAULT_JOB_SET = NullJobSet()
30 changes: 16 additions & 14 deletions rope/contrib/autoimport/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
sqlite-based storage backend (rope.contrib.autoimport.sqlite.AutoImport).
"""


import contextlib
import re

from rope.base import (
Expand Down Expand Up @@ -63,9 +65,12 @@ def import_assist(self, starting):
# XXX: breaking if gave up! use generators
result = []
for module in self.names:
for global_name in self.names[module]:
if global_name.startswith(starting):
result.append((global_name, module))
result.extend(
(global_name, module)
for global_name in self.names[module]
if global_name.startswith(starting)
)

return result

def get_modules(self, name):
Expand All @@ -84,7 +89,7 @@ def get_name_locations(self, name):
result = []
for module in self.names:
if name in self.names[module]:
try:
with contextlib.suppress(exceptions.ModuleNotFoundError):
pymodule = self.project.get_module(module)
if name in pymodule:
pyname = pymodule[name]
Expand All @@ -93,12 +98,13 @@ def get_name_locations(self, name):
resource = module.get_module().get_resource()
if resource is not None and lineno is not None:
result.append((resource, lineno))
except exceptions.ModuleNotFoundError:
pass
return result

def generate_cache(
self, resources=None, underlined=None, task_handle=taskhandle.NullTaskHandle()
self,
resources=None,
underlined=None,
task_handle=taskhandle.DEFAULT_TASK_HANDLE,
):
"""Generate global name cache for project files
Expand All @@ -118,7 +124,7 @@ def generate_cache(
job_set.finished_job()

def generate_modules_cache(
self, modules, underlined=None, task_handle=taskhandle.NullTaskHandle()
self, modules, underlined=None, task_handle=taskhandle.DEFAULT_TASK_HANDLE
):
"""Generate global name cache for modules listed in `modules`"""
job_set = task_handle.create_jobset(
Expand Down Expand Up @@ -164,23 +170,19 @@ def find_insertion_line(self, code):

def update_resource(self, resource, underlined=None):
"""Update the cache for global names in `resource`"""
try:
with contextlib.suppress(exceptions.ModuleSyntaxError):
pymodule = self.project.get_pymodule(resource)
modname = self._module_name(resource)
self._add_names(pymodule, modname, underlined)
except exceptions.ModuleSyntaxError:
pass

def update_module(self, modname, underlined=None):
"""Update the cache for global names in `modname` module
`modname` is the name of a module.
"""
try:
with contextlib.suppress(exceptions.ModuleNotFoundError):
pymodule = self.project.get_module(modname)
self._add_names(pymodule, modname, underlined)
except exceptions.ModuleNotFoundError:
pass

def _module_name(self, resource):
return libutils.modname(resource)
Expand Down
20 changes: 11 additions & 9 deletions rope/contrib/autoimport/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""AutoImport module for rope."""

import contextlib
import re
import sqlite3
import sys
Expand Down Expand Up @@ -153,7 +155,7 @@ def search_full(
self,
name: str,
exact_match: bool = False,
ignored_names: Set[str] = set(),
ignored_names: Optional[Set[str]] = None,
) -> Generator[SearchResult, None, None]:
"""
Search both modules and names for an import string.
Expand All @@ -172,6 +174,8 @@ def search_full(
__________
Unsorted Generator of SearchResults. Each is guaranteed to be unique.
"""
if ignored_names is None:
ignored_names = set()
results = set(self._search_name(name, exact_match))
results = results.union(self._search_module(name, exact_match))
for result in results:
Expand Down Expand Up @@ -256,9 +260,9 @@ def _dump_all(self) -> Tuple[List[Name], List[Package]]:

def generate_cache(
self,
resources: List[Resource] = None,
resources: Optional[List[Resource]] = None,
underlined: bool = False,
task_handle: taskhandle.BaseTaskHandle = taskhandle.NullTaskHandle(),
task_handle: taskhandle.BaseTaskHandle = taskhandle.DEFAULT_TASK_HANDLE,
):
"""Generate global name cache for project files.
Expand Down Expand Up @@ -287,8 +291,8 @@ def generate_cache(

def generate_modules_cache(
self,
modules: List[str] = None,
task_handle: taskhandle.BaseTaskHandle = taskhandle.NullTaskHandle(),
modules: Optional[List[str]] = None,
task_handle: taskhandle.BaseTaskHandle = taskhandle.DEFAULT_TASK_HANDLE,
single_thread: bool = False,
underlined: Optional[bool] = None,
):
Expand All @@ -310,7 +314,7 @@ def generate_modules_cache(

existing = self._get_packages_from_cache()
packages = list(filter_packages(packages, underlined, existing))
if len(packages) == 0:
if not packages:
return
self._add_packages(packages)
job_set = task_handle.create_jobset("Generating autoimport cache", 0)
Expand Down Expand Up @@ -354,7 +358,7 @@ def get_name_locations(self, name):
models.Name.search_by_name_like.select("module"), (name,)
).fetchall()
for module in modules:
try:
with contextlib.suppress(exceptions.ModuleNotFoundError):
module_name = module[0]
if module_name.startswith(f"{self.project_package.name}."):
module_name = ".".join(module_name.split("."))
Expand All @@ -366,8 +370,6 @@ def get_name_locations(self, name):
resource = module.get_module().get_resource()
if resource is not None and lineno is not None:
result.append((resource, lineno))
except exceptions.ModuleNotFoundError:
pass
return result

def clear_cache(self):
Expand Down
5 changes: 1 addition & 4 deletions rope/contrib/autoimport/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ def sort_and_deduplicate_tuple(
def should_parse(path: pathlib.Path, underlined: bool) -> bool:
if underlined:
return True
for part in path.parts:
if part.startswith("_"):
return False
return True
return all(not part.startswith("_") for part in path.parts)


def get_files(
Expand Down
11 changes: 7 additions & 4 deletions rope/contrib/findit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def find_occurrences(
unsure=False,
resources=None,
in_hierarchy=False,
task_handle=taskhandle.NullTaskHandle(),
task_handle=taskhandle.DEFAULT_TASK_HANDLE,
):
"""Return a list of `Location`
Expand Down Expand Up @@ -43,7 +43,11 @@ def is_match(occurrence):


def find_implementations(
project, resource, offset, resources=None, task_handle=taskhandle.NullTaskHandle()
project,
resource,
offset,
resources=None,
task_handle=taskhandle.DEFAULT_TASK_HANDLE,
):
"""Find the places a given method is overridden.
Expand Down Expand Up @@ -127,7 +131,6 @@ def _find_locations(finder, resources, job_set):
result = []
for resource in resources:
job_set.started_job(resource.path)
for occurrence in finder.find_occurrences(resource):
result.append(Location(occurrence))
result.extend(map(Location, finder.find_occurrences(resource)))
job_set.finished_job()
return result
2 changes: 1 addition & 1 deletion rope/contrib/fixmodnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FixModuleNames:
def __init__(self, project):
self.project = project

def get_changes(self, fixer=str.lower, task_handle=taskhandle.NullTaskHandle()):
def get_changes(self, fixer=str.lower, task_handle=taskhandle.DEFAULT_TASK_HANDLE):
"""Fix module names
`fixer` is a function that takes and returns a `str`. Given
Expand Down

0 comments on commit 8101fa8

Please sign in to comment.