Skip to content

Commit

Permalink
style: Enable Ruff B (bugbear) rules (meltano#7445)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillDaSilva committed Mar 28, 2023
1 parent 4b772f7 commit 48e9911
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ pyflakes = [
[tool.flakeheaven.exceptions."src/meltano/core/project_init_service.py"]
"wemake-python-styleguide" = [
"-WPS226", # Found string constant over-use: blue > 3
"-WPS229", # Found too long ``try`` body length: 3 > 1
"-WPS213", # Found too many expressions: 27 > 9
]

Expand Down Expand Up @@ -566,6 +565,7 @@ ignore = [
"UP026", # Replace `mock` import with `unittest.mock` - remove once Python 3.7 support is dropped
]
select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"COM", # flake8-commas
"E", # pycodestyle (error)
Expand Down
14 changes: 11 additions & 3 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@ def build_webapp() -> None:
)
node_version_check.check_returncode()
except Exception:
warn(f"{warning_prefix} could not execute 'node --version'", Warning)
warn(
f"{warning_prefix} could not execute 'node --version'",
Warning,
stacklevel=2,
)
return

try:
subprocess.run(("yarn", "--version")).check_returncode()
except Exception:
warn(f"{warning_prefix} could not execute 'yarn --version'", Warning)
warn(
f"{warning_prefix} could not execute 'yarn --version'",
Warning,
stacklevel=2,
)
return

if not node_version_check.stdout.startswith("v16"):
warn(f"{warning_prefix} NodeJS v16 is required", Warning)
warn(f"{warning_prefix} NodeJS v16 is required", Warning, stacklevel=2)
return

# Build static web app
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def cli( # noqa: C901,WPS231
except OSError as ex:
raise Exception(f"Unable to run Meltano from {cwd!r}") from ex

try: # noqa: WPS229
try:
project = Project.find()
setup_logging(project)
if project.readonly:
Expand Down
8 changes: 4 additions & 4 deletions src/meltano/cli/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ def add(ctx, job_name: str, raw_tasks: str):
task_sets = tasks_from_yaml_str(job_name, raw_tasks)
except InvalidTasksError as yerr:
tracker.track_command_event(CliEvent.aborted)
raise CliError(yerr)
raise CliError(yerr) from yerr

try:
_validate_tasks(project, task_sets, ctx)
except InvalidTasksError as err:
tracker.track_command_event(CliEvent.aborted)
raise CliError(err)
raise CliError(err) from err

try:
task_sets_service.add(task_sets)
Expand Down Expand Up @@ -251,7 +251,7 @@ def set_cmd(ctx, job_name: str, raw_tasks: str):
_validate_tasks(project, task_sets, ctx)
except InvalidTasksError as err:
tracker.track_command_event(CliEvent.aborted)
raise CliError(err)
raise CliError(err) from err

try:
task_sets_service.update(task_sets)
Expand Down Expand Up @@ -312,7 +312,7 @@ def _validate_tasks(project: Project, task_set: TaskSets, ctx: click.Context) ->
tracker.add_contexts(PluginsTrackingContext.from_blocks(parsed_blocks))
except Exception as err:
tracker.track_command_event(CliEvent.aborted)
raise InvalidTasksError(task_set.name, err)
raise InvalidTasksError(task_set.name, err) from err
if not validate_block_sets(logger, parsed_blocks):
tracker.track_command_event(CliEvent.aborted)
raise InvalidTasksError(
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def decorate(*args, **kwargs):
migration_service.upgrade(silent=True)
migration_service.seed(project)
except MigrationError as err:
raise CliError(str(err))
raise CliError(str(err)) from err

func(project, *args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/block/extract_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ async def _start_blocks(self) -> t.AsyncIterator[None]:
Yields:
None
"""
try: # noqa: WPS229
try:
for block in self.blocks:
await block.pre(self.context)
await block.start()
Expand Down
12 changes: 6 additions & 6 deletions src/meltano/core/logging/job_logging_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def get_latest_log(self, state_id) -> str:
except StopIteration:
raise MissingJobLogException(
f"Could not find any log for job with ID '{state_id}'",
)
except FileNotFoundError:
) from None
except FileNotFoundError as ex:
raise MissingJobLogException(
f"Cannot log for job with ID '{state_id}': '{latest_log}' is missing.",
)
) from ex

def get_downloadable_log(self, state_id):
"""Get the `*.log` file of the most recent log for any ELT job that ran with the provided `state_id`.""" # noqa: E501, DAR101, DAR201, DAR401
Expand All @@ -94,11 +94,11 @@ def get_downloadable_log(self, state_id):
except StopIteration:
raise MissingJobLogException(
f"Could not find any log for job with ID '{state_id}'",
)
except FileNotFoundError:
) from None
except FileNotFoundError as ex:
raise MissingJobLogException(
f"Cannot log for job with ID '{state_id}': '{latest_log}' is missing.",
)
) from ex

def get_all_logs(self, state_id):
"""Get all the log files for any ELT job that ran with the provided `state_id`.
Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/logging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def writelines(self, lines: str):
async def _write_line_writer(writer, line):
# StreamWriters like a subprocess's stdin need special consideration
if isinstance(writer, asyncio.StreamWriter):
try: # noqa: WPS229
try:
writer.write(line)
await writer.drain()
except (BrokenPipeError, ConnectionResetError):
Expand Down
8 changes: 4 additions & 4 deletions src/meltano/core/manifest/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _get_active_manifest() -> Manifest:
try:
return _active_manifest[-1]
except IndexError:
raise Exception("No manifest has been activated")
raise Exception("No manifest has been activated") from None


@contextmanager
Expand Down Expand Up @@ -98,7 +98,7 @@ def plugin_context(plugin_name: str) -> t.Iterator[None]:
if x["name"] == plugin_name
)
except StopIteration:
raise ValueError(f"Plugin {plugin_name!r} not found in manifest")
raise ValueError(f"Plugin {plugin_name!r} not found in manifest") from None

with _env_context(plugin["env"]):
yield
Expand Down Expand Up @@ -126,7 +126,7 @@ def schedule_context(schedule_name: str) -> t.Iterator[None]:
try:
schedule = next(x for x in schedules if x["name"] == schedule_name)
except StopIteration:
raise ValueError(f"Schedule {schedule!r} not found in manifest")
raise ValueError(f"Schedule {schedule!r} not found in manifest") from None

with _env_context(schedule["env"]):
yield
Expand Down Expand Up @@ -154,7 +154,7 @@ def job_context(job_name: str) -> t.Iterator[None]:
try:
job = next(x for x in jobs if x["name"] == job_name)
except StopIteration:
raise ValueError(f"Job {job!r} not found in manifest")
raise ValueError(f"Job {job!r} not found in manifest") from None

with _env_context(job["env"]):
yield
Expand Down
12 changes: 6 additions & 6 deletions src/meltano/core/migration_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def upgrade( # noqa: WPS213, WPS231 too many expression and too complex

context = MigrationContext.configure(conn)

try: # noqa: WPS229
try:
# try to find the locked version
head = LOCK_PATH.open().read().strip()
self.ensure_migration_needed(script, context, head)
Expand All @@ -101,19 +101,19 @@ def upgrade( # noqa: WPS213, WPS231 too many expression and too complex

if silent:
migration_logger.setLevel(original_log_level)
except FileNotFoundError:
except FileNotFoundError as ex:
raise MigrationError(
"Cannot upgrade the system database, revision lock not found.",
)
) from ex
except MigrationUneededException:
if not silent:
click.secho("System database up-to-date.")
except Exception as err:
logging.exception(str(err))
except Exception as ex:
logging.exception(str(ex))
raise MigrationError(
"Cannot upgrade the system database. It might be corrupted or "
"was created before database migrations where introduced (v0.34.0)",
)
) from ex
finally:
conn.close()

Expand Down
2 changes: 1 addition & 1 deletion src/meltano/core/plugin_discovery_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def load_discovery(self, discovery_file, cache=False) -> DiscoveryFile:

return self._discovery
except (YAMLError, Exception) as err:
raise DiscoveryInvalidError(str(err))
raise DiscoveryInvalidError(str(err)) from err

def cache_discovery(self):
"""Cache the `discovery.yml` manifest."""
Expand Down
6 changes: 3 additions & 3 deletions src/meltano/core/plugin_invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async def prepared(self, session):
Yields:
Yields to the caller, then resetting the config.
"""
try: # noqa: WPS229. Allow try body of length > 1.
try:
await self.prepare(session)
yield
finally:
Expand Down Expand Up @@ -500,15 +500,15 @@ async def dump(self, file_id: str) -> str:
Raises:
__cause__: If file is not found.
"""
try: # noqa: WPS229. Allow try body of length > 1.
try:
if file_id != "config":
async with self._invoke():
return self.files[file_id].read_text()

return self.files[file_id].read_text()
except ExecutableNotFoundError as err: # noqa: WPS329. Allow "useless" except.
# Unwrap FileNotFoundError
raise err.__cause__ # noqa: WPS609. Allow accessing magic attribute.
raise err.__cause__ from None # noqa: WPS469, WPS609

def add_output_handler(self, src: str, handler: SubprocessOutputWriter):
"""Append an output handler for a given stdio stream.
Expand Down
9 changes: 5 additions & 4 deletions src/meltano/core/schedule_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ def remove_schedule(self, name: str) -> str:
try:
# guard if it doesn't exist
schedule = find_named(self.schedules(), name)
except NotFound:
raise ScheduleDoesNotExistError(name)
except NotFound as ex:
raise ScheduleDoesNotExistError(name) from ex

# find the schedules plugin config
meltano.schedules.remove(schedule)
Expand All @@ -260,9 +260,10 @@ def update_schedule(self, schedule: Schedule):
with self.project.meltano_update() as meltano:
try:
idx = meltano.schedules.index(schedule)
meltano.schedules[idx] = schedule
except ValueError:
raise ScheduleDoesNotExistError(schedule.name)
raise ScheduleDoesNotExistError(schedule.name) from None
else:
meltano.schedules[idx] = schedule

def find_namespace_schedule(self, namespace: str) -> Schedule:
"""Search for a Schedule that runs for a certain plugin namespace.
Expand Down
6 changes: 4 additions & 2 deletions src/meltano/core/settings_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ def db_namespace(self) -> str:
def setting_definitions(self) -> list[SettingDefinition]:
"""Return definitions of supported settings."""

@property # noqa: B027
@property
def inherited_settings_service(self):
"""Return settings service to inherit configuration from."""
return None # noqa: DAR201

@property
@abstractmethod
Expand Down Expand Up @@ -425,6 +426,7 @@ def get_with_metadata( # noqa: WPS210, WPS615
f"`{value!r}` will be used"
),
RuntimeWarning,
stacklevel=2,
)

return value, metadata
Expand Down Expand Up @@ -484,7 +486,7 @@ def set_with_metadata( # noqa: WPS615, WPS210
try:
setting_def = self.find_setting(name)
except SettingMissingError:
warnings.warn(f"Unknown setting {name!r}", RuntimeWarning)
warnings.warn(f"Unknown setting {name!r}", RuntimeWarning, stacklevel=2)
setting_def = None

metadata = {"name": name, "path": path, "store": store, "setting": setting_def}
Expand Down
10 changes: 8 additions & 2 deletions src/meltano/core/task_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,18 @@ def tasks_from_yaml_str(name: str, yaml_str: str) -> TaskSets:
try:
tasks = yaml.safe_load(yaml_str)
except yaml.parser.ParserError as yerr:
raise InvalidTasksError(name, f"Failed to parse yaml '{yaml_str}': {yerr}")
raise InvalidTasksError(
name,
f"Failed to parse yaml '{yaml_str}': {yerr}",
) from yerr

try:
validate(instance=tasks, schema=TASKS_JSON_SCHEMA)
except ValidationError as verr:
raise InvalidTasksError(name, f"Failed to validate task schema: {verr}")
raise InvalidTasksError(
name,
f"Failed to validate task schema: {verr}",
) from verr

# Handle the special case of a single task
if isinstance(tasks, str):
Expand Down
10 changes: 7 additions & 3 deletions src/meltano/core/tracking/contexts/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def _get_parent_context_uuid_str() -> str | None:
return str(uuid.UUID(uuid_str))
except ValueError:
warn(
f"Invalid telemetry parent environment context UUID {uuid_str!r} "
"from $MELTANO_PARENT_CONTEXT_UUID - Meltano will continue as if "
"$MELTANO_PARENT_CONTEXT_UUID had not been set",
(
f"Invalid telemetry parent environment context UUID "
f"{uuid_str!r} from $MELTANO_PARENT_CONTEXT_UUID - "
"Meltano will continue as if $MELTANO_PARENT_CONTEXT_UUID "
"had not been set"
),
stacklevel=2,
)
return None

Expand Down
1 change: 1 addition & 0 deletions src/meltano/core/tracking/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def get_client_id(self, stored_telemetry_settings: TelemetrySettings) -> uuid.UU
"$MELTANO_CLIENT_ID"
),
RuntimeWarning,
stacklevel=2,
)
if stored_telemetry_settings.client_id is not None:
return stored_telemetry_settings.client_id
Expand Down
4 changes: 2 additions & 2 deletions src/meltano/core/utils/pidfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def process(self) -> psutil.Process:

try:
return psutil.Process(self.pid)
except psutil.NoSuchProcess:
raise UnknownProcessError(self)
except psutil.NoSuchProcess as ex:
raise UnknownProcessError(self) from ex

def unlink(self):
return self.path.unlink()
Expand Down
2 changes: 1 addition & 1 deletion tests/meltano/core/tracking/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _function_to_deepen_traceback() -> None:
_function_to_deepen_traceback()
except Exception:
line_nums.append(1 + inspect.currentframe().f_lineno)
raise CustomException
raise CustomException from None
except Exception:
ctx = ExceptionContext()

Expand Down

0 comments on commit 48e9911

Please sign in to comment.