Skip to content

Commit

Permalink
style: Enable Ruff SIM (simplify) rules (meltano#7447)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillDaSilva committed Mar 28, 2023
1 parent cfa7304 commit 800ca9c
Show file tree
Hide file tree
Showing 23 changed files with 157 additions and 178 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pyflakes = [
"-WPS300", # Allow local folder imports (import .module)
"-WPS305", # Allow f-strings
"-WPS306", # Ignore missing base class, required by pyupgrade: https://github.com/asottile/pyupgrade#rewrites-class-declaration
"-WPS309", # Allow reversed compare order
"-WPS316",
"-WPS317", # Allow "incorrect multi-line parameters", since Black forces them to be "incorrect"
"-WPS324", # Allow inconsistent returns - permitted because of too many false positives
Expand Down Expand Up @@ -573,6 +574,7 @@ select = [
"I", # isort
"ISC", # flake8-implicit-str-concat
"RSE", # flake8-raise
"SIM", # flake8-simplify
"UP", # pyupgrade
"W", # pycodestyle (warning)
]
Expand Down
13 changes: 10 additions & 3 deletions src/meltano/cli/elt.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
@click.pass_context
@pass_project(migrate=True)
@click_run_async
async def elt(
async def elt( # WPS408
project: Project,
ctx: click.Context,
extractor: str,
Expand Down Expand Up @@ -303,7 +303,12 @@ async def _run_elt(
) from err


async def _run_extract_load(log, elt_context, output_logger, **kwargs): # noqa: WPS231
async def _run_extract_load( # noqa: WPS210, WPS231
log,
elt_context,
output_logger,
**kwargs,
):
extractor = elt_context.extractor.name
loader = elt_context.loader.name

Expand Down Expand Up @@ -344,7 +349,9 @@ async def _run_extract_load(log, elt_context, output_logger, **kwargs): # noqa:

singer_runner = SingerRunner(elt_context)
try:
with extractor_log.line_writer() as extractor_log_writer:
# Once Python 3.9 support has been dropped, consolidate these
# with-statements by using parentheses.
with extractor_log.line_writer() as extractor_log_writer: # noqa: SIM117
with loader_log.line_writer() as loader_log_writer:
with extractor_out_writer_ctxmgr() as extractor_out_writer:
with loader_out_writer_ctxmgr() as loader_out_writer:
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/cli/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def do_list_commands(plugin):
f"{plugin.name}:{cmd}": props.description
for cmd, props in plugin.all_commands.items()
}
column_len = max(len(name) for name in descriptions.keys()) + 2
column_len = max(len(name) for name in descriptions) + 2
for name, desc in descriptions.items():
click.secho(name.ljust(column_len, " "), fg="blue", nl=False)
click.echo(desc)
Expand Down
5 changes: 1 addition & 4 deletions src/meltano/core/block/future_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@

def all_done(tasks: list[Task], done: set[Task]) -> bool:
"""Iterate through a task list checking if ALL tasks are in the done set."""
for idx in tasks:
if idx not in done:
return False
return True
return all(idx in done for idx in tasks)


def first_failed_future(exception_future: Task, done: set[Task]) -> Task | None:
Expand Down
16 changes: 6 additions & 10 deletions src/meltano/core/block/singer.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async def close_stdin(self) -> None:
"""Close the underlying process stdin if the block is a consumer."""
if self.consumer:
self.process_handle.stdin.close()
with suppress(AttributeError): # `wait_closed` is Python 3.8+ (see #3347
with suppress(AttributeError): # `wait_closed` is Python 3.8+ (see #3347)
await self.process_handle.stdin.wait_closed()

def stdout_link(self, dst: SubprocessOutputWriter) -> None:
Expand Down Expand Up @@ -248,12 +248,10 @@ async def pre(self, context) -> None:

async def post(self) -> None:
"""Post triggers resetting the underlying plugin config."""
try:
with suppress(FileNotFoundError):
# TODO: should we preserve these on a failure?
# The invoker prepared context manager was able to clean up the configs
await self.invoker.cleanup()
except FileNotFoundError:
# TODO: should we preserve these on a failure ?
# the invoker prepared context manager was able to clean up the configs
pass

def _merge_outputs(self, source: str, outputs: list) -> list:
if not self.invoker.output_handlers:
Expand Down Expand Up @@ -367,8 +365,6 @@ async def stop(self, kill: bool = True):
self._stdout_future.cancel()
if self._stderr_future is not None:
self._stderr_future.cancel()
try:
await self.invoker.cleanup()
except FileNotFoundError:
with suppress(FileNotFoundError):
# the invoker prepared context manager was able to clean up the configs
pass
await self.invoker.cleanup()
14 changes: 8 additions & 6 deletions src/meltano/core/plugin_discovery_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ def load_local_discovery(self) -> DiscoveryFile | None:
Returns:
The discovery file.
"""
with suppress(FileNotFoundError):
with self.project.root_dir("discovery.yml").open() as local_discovery:
return self.load_discovery(local_discovery)
with suppress(FileNotFoundError), self.project.root_dir(
"discovery.yml",
).open() as local_discovery:
return self.load_discovery(local_discovery)

def load_remote_discovery(self) -> DiscoveryFile | None:
"""Load the remote `discovery.yml` manifest.
Expand Down Expand Up @@ -285,9 +286,10 @@ def load_cached_discovery(self) -> DiscoveryFile | None:
Returns:
The discovery file.
"""
with suppress(FileNotFoundError):
with self.cached_discovery_file.open() as cached_discovery:
return self.load_discovery(cached_discovery)
with suppress(
FileNotFoundError,
), self.cached_discovery_file.open() as cached_discovery:
return self.load_discovery(cached_discovery)

def load_bundled_discovery(self):
"""Load the bundled `discovery.yml` manifest.
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/project_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def _restore_file_key_order(self, file_dicts: dict[str, CommentedMap]):

# Restore sorting in project files
sorted_file_dicts[file] = CommentedMap()
for key in contents.keys():
for key in contents:
if key in file_dicts[file]:
sorted_file_dicts[file][key] = file_dicts[file][key]

Expand Down
7 changes: 2 additions & 5 deletions src/meltano/core/state_store/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,8 @@ def get_state_ids(self, pattern: str | None = None): # noqa: WPS210
name_starts_with=self.prefix.lstrip("/"),
):
(state_id, filename) = blob.name.split("/")[-2:]
if filename == "state.json":
if not pattern:
state_ids.add(state_id)
elif pattern_re.match(state_id):
state_ids.add(state_id)
if filename == "state.json" and (not pattern) or pattern_re.match(state_id):
state_ids.add(state_id)
return list(state_ids)

def delete(self, file_path: str):
Expand Down
4 changes: 1 addition & 3 deletions src/meltano/core/state_store/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,6 @@ def get_state_ids(self, pattern: str | None = None):
state_id = b64decode(
os.path.basename(os.path.dirname(state_file)).encode(),
).decode()
if not pattern:
state_ids.add(state_id)
elif pattern_re.match(state_id):
if (not pattern) or pattern_re.match(state_id):
state_ids.add(state_id)
return state_ids
7 changes: 2 additions & 5 deletions src/meltano/core/state_store/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ def get_state_ids(self, pattern: str | None = None): # noqa: WPS210
state_ids = set()
for blob in self.client.list_blobs(bucket_or_name=self.bucket):
(state_id, filename) = blob.name.split("/")[-2:]
if filename == "state.json":
if not pattern:
state_ids.add(state_id)
elif pattern_re.match(state_id):
state_ids.add(state_id)
if filename == "state.json" and (not pattern) or pattern_re.match(state_id):
state_ids.add(state_id)
return list(state_ids)

def delete(self, file_path: str):
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/validation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def select_test(self, name: str) -> None:

def select_all(self) -> None:
"""Mark all plugin validators as selected."""
for name in self.tests_selection.keys():
for name in self.tests_selection:
self.tests_selection[name] = True

async def run_all(self, session: sessionmaker) -> dict[str, int]:
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,16 @@ def project_directory(project_init_service):

@pytest.fixture(scope="class")
def project(project_init_service, tmp_path_factory: pytest.TempPathFactory):
with cd(tmp_path_factory.mktemp("meltano-project-dir")):
with project_directory(project_init_service) as project:
yield project
with cd(tmp_path_factory.mktemp("meltano-project-dir")), project_directory(
project_init_service,
) as project:
yield project


@pytest.fixture(scope="function")
def project_function(project_init_service, tmp_path: Path):
with cd(tmp_path):
with project_directory(project_init_service) as project:
yield project
with cd(tmp_path), project_directory(project_init_service) as project:
yield project


@pytest.fixture(scope="class")
Expand Down
11 changes: 5 additions & 6 deletions tests/meltano/api/controllers/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ def test_assign_role(self, user, status_code, api, app, impersonate):
],
)
def test_delete_admin_role(self, user, status_code, api, app, impersonate):
with app.test_request_context():
with impersonate(users.get_user(user)):
res = api.delete(
url_for("settings.roles"),
json={"role": {"name": "admin"}},
)
with app.test_request_context(), impersonate(users.get_user(user)):
res = api.delete(
url_for("settings.roles"),
json={"role": {"name": "admin"}},
)

assert res.status_code == status_code, res.data

Expand Down

0 comments on commit 800ca9c

Please sign in to comment.