Skip to content

Commit

Permalink
Update and apply ruff refactorings
Browse files Browse the repository at this point in the history
ruff 0.280.0 has a bug that would turn the imports guarded by `if
TYPE_CHECKING` into their non-string variant, which would break the
runtime. So we can't merge 23.1 into dev without this.
  • Loading branch information
mvdbeek committed Aug 9, 2023
1 parent c62da4a commit 130d2b0
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/galaxy/datatypes/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def do_slow_split(cls, input_datasets, subdir_generator_function, split_params):
def do_fast_split(cls, input_datasets, toc_file_datasets, subdir_generator_function, split_params):
data = json.load(open(toc_file_datasets[0].file_name))
sections = data["sections"]
total_sequences = int(0)
total_sequences = 0
for section in sections:
total_sequences += int(section["sequences"])
sequences_per_file = cls.get_sequences_per_file(total_sequences, split_params)
Expand Down Expand Up @@ -247,7 +247,7 @@ def get_split_commands_with_toc(
sections = toc_file["sections"]
result = []

current_sequence = int(0)
current_sequence = 0
i = 0
# skip to the section that contains my starting sequence
while i < len(sections) and start_sequence >= current_sequence + int(sections[i]["sequences"]):
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/dependencies/pinned-lint-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ importlib-metadata==4.2.0
mccabe==0.7.0
pycodestyle==2.9.1
pyflakes==2.5.0
ruff==0.0.280
ruff==0.0.283
typing_extensions==4.7.1
zipp==3.15.0
8 changes: 3 additions & 5 deletions lib/galaxy/managers/citations.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,10 @@ def to_bibtex(self):
log.exception("Failed to fetch bibtex for DOI %s", self.__doi)

if self.raw_bibtex is DoiCitation.BIBTEX_UNSET:
return """@MISC{{{doi},
DOI = {{{doi}}},
return f"""@MISC{{{self.__doi},
DOI = {{{self.__doi}}},
note = {{Failed to fetch BibTeX for DOI.}}
}}""".format(
doi=self.__doi
)
}}"""
else:
return self.raw_bibtex

Expand Down
10 changes: 5 additions & 5 deletions lib/galaxy/managers/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def configure_provider(provider, credentials):
if len(missing_credentials) > 0:
raise RequestParameterMissingException(
"The following required key(s) are missing from the provided "
"credentials object: {}".format(missing_credentials)
f"credentials object: {missing_credentials}"
)
session_token = credentials.get("SessionToken")
config = {"aws_access_key": access, "aws_secret_key": secret, "aws_session_token": session_token}
Expand All @@ -107,7 +107,7 @@ def configure_provider(provider, credentials):
if len(missing_credentials) > 0:
raise RequestParameterMissingException(
"The following required key(s) are missing from the provided "
"credentials object: {}".format(missing_credentials)
f"credentials object: {missing_credentials}"
)

config = {
Expand Down Expand Up @@ -145,7 +145,7 @@ def configure_provider(provider, credentials):
if len(missing_credentials) > 0:
raise RequestParameterMissingException(
"The following required key(s) are missing from the provided "
"credentials object: {}".format(missing_credentials)
f"credentials object: {missing_credentials}"
)
config = {
"os_username": username,
Expand All @@ -161,8 +161,8 @@ def configure_provider(provider, credentials):
connection = CloudProviderFactory().create_provider(ProviderList.GCP, config)
else:
raise RequestParameterInvalidException(
"Unrecognized provider '{}'; the following are the supported "
"providers: {}.".format(provider, SUPPORTED_PROVIDERS.keys())
f"Unrecognized provider '{provider}'; the following are the supported "
f"providers: {SUPPORTED_PROVIDERS.keys()}."
)

# The authorization-assertion mechanism of Cloudbridge assumes a user has an elevated privileges,
Expand Down
10 changes: 2 additions & 8 deletions lib/galaxy/objectstore/pithos.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ def _size(self, obj, **kwargs) -> int:
try:
return os.path.getsize(self._get_cache_path(path))
except OSError as ex:
log.warning(
"Could not get size of file {path} in local cache,"
"will try Pithos. Error: {err}".format(path=path, err=ex)
)
log.warning(f"Could not get size of file {path} in local cache," f"will try Pithos. Error: {ex}")
try:
file = self.pithos.get_object_info(path)
except ClientError as ce:
Expand Down Expand Up @@ -406,10 +403,7 @@ def _update_from_file(self, obj, **kwargs):
if kwargs.get("create"):
self._create(obj, **kwargs)
if not self._exists(obj, **kwargs):
raise ObjectNotFound(
"objectstore.update_from_file, object does not exist: {obj}, "
"kwargs: {kwargs}".format(obj=obj, kwargs=kwargs)
)
raise ObjectNotFound(f"objectstore.update_from_file, object does not exist: {obj}, " f"kwargs: {kwargs}")

path = self._construct_path(obj, **kwargs)
cache_path = self._get_cache_path(path)
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def all_control_queues_for_declare(application_stack):
"""
# Get all active processes and construct queues for each process
process_names = (
"{p.server_name}@{p.hostname}".format(p=p)
for p in application_stack.app.database_heartbeat.get_active_processes()
f"{p.server_name}@{p.hostname}" for p in application_stack.app.database_heartbeat.get_active_processes()
)
return [Queue(f"control.{server_name}", galaxy_exchange, routing_key="control.*") for server_name in process_names]

Expand Down
8 changes: 4 additions & 4 deletions lib/galaxy/tool_util/deps/brew_exts.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ def __init__(self, command, stdout, stderr):
self.stdout = stdout
self.stderr = stderr
self.message = (
"Failed to execute command-line {}, stderr was:\n"
f"Failed to execute command-line {command}, stderr was:\n"
"-------->>begin stderr<<--------\n"
"{}\n"
f"{stderr}\n"
"-------->>end stderr<<--------\n"
"-------->>begin stdout<<--------\n"
"{}\n"
f"{stdout}\n"
"-------->>end stdout<<--------\n"
).format(command, stderr, stdout)
)

def __str__(self):
return self.message
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def pretty_print_time_interval(time=False, precise=False, utc=False):
now = datetime.utcnow()
else:
now = datetime.now()
if type(time) is int:
if isinstance(time, (int, float)):
diff = now - datetime.fromtimestamp(time)
elif isinstance(time, datetime):
diff = now - time
Expand Down
8 changes: 4 additions & 4 deletions lib/galaxy/util/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ def __init__(self, command, stdout, stderr, returncode):
self.stderr = stderr
self.returncode = returncode
self.message = (
"Failed to execute command-line {}, stderr was:\n"
f"Failed to execute command-line {command}, stderr was:\n"
"-------->>begin stderr<<--------\n"
"{}\n"
f"{stderr}\n"
"-------->>end stderr<<--------\n"
"-------->>begin stdout<<--------\n"
"{}\n"
f"{stdout}\n"
"-------->>end stdout<<--------\n"
).format(command, stderr, stdout)
)

def __str__(self):
"""Return a verbose error message indicating the command problem."""
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/base/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def build_apispec(self):
methods = []
if rule.conditions:
m = rule.conditions.get("method", [])
methods = type(m) is str and [m] or m
methods = [m] if isinstance(m, str) else m
# Find the controller class
if controller not in self.api_controllers:
# Only happens when removing a controller after porting to FastAPI.
Expand Down
3 changes: 1 addition & 2 deletions lib/galaxy/webapps/galaxy/api/cloudauthz.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ def create(self, trans, payload, **kwargs):
if not isinstance(config, dict):
log.debug(msg_template.format(f"invalid config type `{type(config)}`, expect `dict`"))
raise RequestParameterInvalidException(
"Invalid type for the required `config` variable; expect `dict` "
"but received `{}`.".format(type(config))
"Invalid type for the required `config` variable; expect `dict` " f"but received `{type(config)}`."
)
if authn_id:
try:
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/webapps/galaxy/api/library_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ def _upload_library_dataset(self, trans, folder_id: int, **kwd):
trans, folder_id=folder.id, replace_dataset=replace_dataset, **kwd
)
if created_outputs_dict:
if type(created_outputs_dict) == str:
if isinstance(created_outputs_dict, str):
return 400, created_outputs_dict
elif type(created_outputs_dict) == tuple:
elif isinstance(created_outputs_dict, tuple):
return created_outputs_dict[0], created_outputs_dict[1]
return 200, created_outputs_dict
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/webapps/galaxy/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ def set_toolbox_filters(self, trans, id, payload=None, **kwd):
for prefixed_name in payload:
if prefixed_name.startswith(filter_type):
filter_selection = payload.get(prefixed_name)
if type(filter_selection) != bool:
if not isinstance(filter_selection, bool):
raise exceptions.RequestParameterInvalidException(
"Please specify the filter selection as boolean value."
)
Expand Down
4 changes: 2 additions & 2 deletions lib/galaxy/webapps/galaxy/controllers/authnz.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def callback(self, trans, provider, idphint=None, **kwargs):
" Error message: {}".format(provider, user, kwargs.get("error", "None"))
)
return trans.show_error_message(
"Failed to handle authentication callback from {}. "
f"Failed to handle authentication callback from {provider}. "
"Please try again, and if the problem persists, contact "
"the Galaxy instance admin".format(provider)
"the Galaxy instance admin"
)
try:
success, message, (redirect_url, user) = trans.app.authnz_manager.callback(
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy_test/api/test_drs.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_exception_handling_schema(self):
for method in HTTP_METHODS:
api_url = self._url_join(f"ga4gh/drs/v1/objects/{drs_id}/access/fakeid")
error_response = method(api_url)
assert type(error_response.status_code) == int
assert isinstance(error_response.status_code, int)
assert error_response.status_code == 404
error_as_dict = error_response.json()
assert "status_code" in error_as_dict
Expand Down
2 changes: 1 addition & 1 deletion scripts/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def display(api_key, url, return_formatted=True):
print("------------------")
for k, v in r.items():
print(f"{k}: {v}")
elif type(r) == str:
elif isinstance(r, str):
print(r)
else:
print("response is unknown type: %s" % type(r))
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_cli_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def start_ssh_docker(container_name, jobs_directory, port=10022, image="agaveapi
"--rm",
"--privileged", # for torque
"-v",
"{jobs_directory}:{jobs_directory}".format(jobs_directory=jobs_directory),
f"{jobs_directory}:{jobs_directory}",
"-v",
f"{ssh_keys.public_key_file}:/home/testuser/.ssh/authorized_keys",
"--ulimit",
Expand Down
26 changes: 13 additions & 13 deletions test/unit/config/test_load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def test_load_config_from_schema(mock_init):
assert config.property5 is None
assert config.property6 is None

assert type(config.property1) is str
assert type(config.property2) is int
assert type(config.property3) is float
assert type(config.property4) is bool
assert isinstance(config.property1, str)
assert isinstance(config.property2, int)
assert isinstance(config.property3, float)
assert isinstance(config.property4, bool)


def test_update_raw_config_from_kwargs(mock_init):
Expand All @@ -64,10 +64,10 @@ def test_update_raw_config_from_kwargs(mock_init):
assert config._raw_config["property5"] is None # no change
assert config._raw_config["property6"] is None # no change

assert type(config._raw_config["property1"]) is str
assert type(config._raw_config["property2"]) is int
assert type(config._raw_config["property3"]) is float
assert type(config._raw_config["property4"]) is bool
assert isinstance(config._raw_config["property1"], str)
assert isinstance(config._raw_config["property2"], int)
assert isinstance(config._raw_config["property3"], float)
assert isinstance(config._raw_config["property4"], bool)


def test_update_raw_config_from_string_kwargs(mock_init):
Expand All @@ -80,10 +80,10 @@ def test_update_raw_config_from_string_kwargs(mock_init):
assert config._raw_config["property3"] == 2.0 # updated
assert config._raw_config["property4"] is False # updated

assert type(config._raw_config["property1"]) is str
assert type(config._raw_config["property2"]) is int
assert type(config._raw_config["property3"]) is float
assert type(config._raw_config["property4"]) is bool
assert isinstance(config._raw_config["property1"], str)
assert isinstance(config._raw_config["property2"], int)
assert isinstance(config._raw_config["property3"], float)
assert isinstance(config._raw_config["property4"], bool)


def test_update_raw_config_from_kwargs_with_none(mock_init):
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_update_raw_config_from_kwargs_falsy_not_none(mock_init):
config = BaseAppConfiguration(property1=0)

assert config._raw_config["property1"] == "0" # updated
assert type(config._raw_config["property1"]) is str # and converted to str
assert isinstance(config._raw_config["property1"], str) # and converted to str


def test_unset_renamed_option_set_by_old_option(mock_init, monkeypatch):
Expand Down
8 changes: 4 additions & 4 deletions test/unit/config/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def mock_init(self, stream):
assert schema.defaults["property5"] is None
assert schema.defaults["property6"] is None

assert type(schema.defaults["property1"]) is str
assert type(schema.defaults["property2"]) is int
assert type(schema.defaults["property3"]) is float
assert type(schema.defaults["property4"]) is bool
assert isinstance(schema.defaults["property1"], str)
assert isinstance(schema.defaults["property2"], int)
assert isinstance(schema.defaults["property3"], float)
assert isinstance(schema.defaults["property4"], bool)

assert len(schema.reloadable_options) == 2
assert "property2" in schema.reloadable_options
Expand Down

0 comments on commit 130d2b0

Please sign in to comment.