Skip to content

Commit 0258311

Browse files
committed
Unsafe fixes
1 parent 77c3703 commit 0258311

289 files changed

Lines changed: 2027 additions & 1631 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/gensidebar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ def _write(project, desc, link, mapping=conf["intersphinx_mapping"]):
4242
if project != conf_api:
4343
if do_gen:
4444
args = desc, mapping[project][0], link
45-
lines.append(" %s <%s%s.html>" % args)
45+
lines.append(" {} <{}{}.html>".format(*args))
4646
else:
4747
args = desc, link
48-
lines.append(" %s <%s>" % args)
48+
lines.append(" {} <{}>".format(*args))
4949

5050
def _header(project, text):
5151
if project == conf_api or do_gen:
5252
lines.extend([".. toctree::", " :maxdepth: 2"])
53-
lines.extend([" :caption: %s" % text, ""])
53+
lines.extend([f" :caption: {text}", ""])
5454

5555
#
5656
# Specify the sidebar contents here

esmvalcore/_citation.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def _json_to_bibtex(data):
193193
doi = data["identifier"].get("id", "doi not found")
194194
url = f"https://doi.org/{doi}"
195195

196-
bibtex_entry = textwrap.dedent(f"""
196+
return textwrap.dedent(f"""
197197
@misc{{{url},
198198
\turl = {{{url}}},
199199
\ttitle = {{{title}}},
@@ -203,7 +203,6 @@ def _json_to_bibtex(data):
203203
\tdoi = {{{doi}}},
204204
}}
205205
""").lstrip()
206-
return bibtex_entry
207206

208207

209208
@lru_cache(maxsize=1024)
@@ -226,11 +225,7 @@ def _collect_bibtex_citation(tag):
226225
def _collect_cmip_citation(json_url):
227226
"""Collect information from CMIP6 Data Citation Service."""
228227
json_data = _get_response(json_url)
229-
if json_data:
230-
bibtex_entry = _json_to_bibtex(json_data)
231-
else:
232-
bibtex_entry = ""
233-
return bibtex_entry
228+
return _json_to_bibtex(json_data) if json_data else ""
234229

235230

236231
def _make_url_prefix(attribute):
@@ -246,17 +241,14 @@ def _make_url_prefix(attribute):
246241
for key, value in attribute:
247242
if key.localpart in localpart:
248243
localpart[key.localpart] = value
249-
url_prefix = ".".join(localpart.values())
250-
return url_prefix
244+
return ".".join(localpart.values())
251245

252246

253247
def _make_json_url(url_prefix):
254248
"""Make json url based on CMIP6 Data Citation Service."""
255-
json_url = f"{CMIP6_URL_STEM}/cerarest/exportcmip6?input={url_prefix}"
256-
return json_url
249+
return f"{CMIP6_URL_STEM}/cerarest/exportcmip6?input={url_prefix}"
257250

258251

259252
def _make_info_url(url_prefix):
260253
"""Make info url based on CMIP6 Data Citation Service."""
261-
info_url = f"{CMIP6_URL_STEM}/cmip6?input={url_prefix}"
262-
return info_url
254+
return f"{CMIP6_URL_STEM}/cmip6?input={url_prefix}"

esmvalcore/_main.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ def parse_resume(resume, recipe):
6565
for resume_dir in resume:
6666
resume_recipe = resume_dir / "run" / recipe.name
6767
if current_recipe != resume_recipe.read_text(encoding="utf-8"):
68-
raise ValueError(
68+
msg = (
6969
f"Only identical recipes can be resumed, but "
70-
f"{resume_recipe} is different from {recipe}",
70+
f"{resume_recipe} is different from {recipe}"
71+
)
72+
raise ValueError(
73+
msg,
7174
)
7275
return resume
7376

@@ -294,9 +297,12 @@ def get(recipe):
294297
configure_logging(console_log_level="info")
295298
installed_recipe = DIAGNOSTICS.recipes / recipe
296299
if not installed_recipe.exists():
297-
raise RecipeError(
300+
msg = (
298301
f"Recipe {recipe} not found. To list all available recipes, "
299-
'execute "esmvaltool list"',
302+
'execute "esmvaltool list"'
303+
)
304+
raise RecipeError(
305+
msg,
300306
)
301307
logger.info("Copying installed recipe to the current folder...")
302308
shutil.copy(installed_recipe, Path(recipe).name)
@@ -320,9 +326,12 @@ def show(recipe):
320326
configure_logging(console_log_level="info")
321327
installed_recipe = DIAGNOSTICS.recipes / recipe
322328
if not installed_recipe.exists():
323-
raise RecipeError(
329+
msg = (
324330
f"Recipe {recipe} not found. To list all available recipes, "
325-
'execute "esmvaltool list"',
331+
'execute "esmvaltool list"'
332+
)
333+
raise RecipeError(
334+
msg,
326335
)
327336
msg = f"Recipe {recipe}"
328337
logger.info(msg)
@@ -390,9 +399,12 @@ def run(self, recipe, **kwargs):
390399
if cli_config_dir is not None:
391400
cli_config_dir = Path(cli_config_dir).expanduser().absolute()
392401
if not cli_config_dir.is_dir():
393-
raise NotADirectoryError(
402+
msg = (
394403
f"Invalid --config_dir given: {cli_config_dir} is not an "
395-
f"existing directory",
404+
f"existing directory"
405+
)
406+
raise NotADirectoryError(
407+
msg,
396408
)
397409

398410
# TODO: remove in v2.14.0
@@ -416,10 +428,13 @@ def run(self, recipe, **kwargs):
416428
# validated) when importing the module with `from .config import
417429
# CFG`
418430
except InvalidConfigParameter as exc:
419-
raise InvalidConfigParameter(
431+
msg = (
420432
f"Failed to parse configuration directory "
421433
f"{cli_config_dir} (command line argument): "
422-
f"{exc!s}",
434+
f"{exc!s}"
435+
)
436+
raise InvalidConfigParameter(
437+
msg,
423438
) from exc
424439

425440
recipe = self._get_recipe(recipe)
@@ -456,9 +471,12 @@ def _create_session_dir(session):
456471
session.session_name = session_dir.name
457472
return
458473

459-
raise RecipeError(
474+
msg = (
460475
f"Output directory '{session.session_dir}' already exists and"
461-
" unable to find alternative, aborting to prevent data loss.",
476+
" unable to find alternative, aborting to prevent data loss."
477+
)
478+
raise RecipeError(
479+
msg,
462480
)
463481

464482
def _run(
@@ -534,8 +552,7 @@ def _get_recipe(recipe) -> Path:
534552
installed_recipe = DIAGNOSTICS.recipes / recipe
535553
if os.path.isfile(installed_recipe):
536554
recipe = installed_recipe
537-
recipe = Path(os.path.expandvars(recipe)).expanduser().absolute()
538-
return recipe
555+
return Path(os.path.expandvars(recipe)).expanduser().absolute()
539556

540557
@staticmethod
541558
def _get_config_info(cli_config_dir):

esmvalcore/_provenance.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ def get_esmvaltool_provenance():
2929
namespace = "software"
3030
create_namespace(provenance, namespace)
3131
attributes = {} # TODO: add dependencies with versions here
32-
activity = provenance.activity(
32+
return provenance.activity(
3333
namespace + ":esmvaltool==" + __version__,
3434
other_attributes=attributes,
3535
)
3636

37-
return activity
38-
3937

4038
ESMVALTOOL_PROVENANCE = get_esmvaltool_provenance()
4139

@@ -167,7 +165,8 @@ def __hash__(self):
167165
def copy_provenance(self):
168166
"""Create a copy with identical provenance information."""
169167
if self.provenance is None:
170-
raise ValueError(f"Provenance of {self} not initialized")
168+
msg = f"Provenance of {self} not initialized"
169+
raise ValueError(msg)
171170
new = TrackedFile(self.filename, self.attributes)
172171
new.provenance = copy.deepcopy(self.provenance)
173172
new.entity = new.provenance.get_record(self.entity.identifier)[0]
@@ -192,8 +191,9 @@ def initialize_provenance(self, activity):
192191
propagate into the provenance of this file.
193192
"""
194193
if self.provenance is not None:
194+
msg = f"Provenance of {self} already initialized"
195195
raise ValueError(
196-
f"Provenance of {self} already initialized",
196+
msg,
197197
)
198198
self.provenance = ProvDocument()
199199
self._initialize_namespaces()
@@ -252,7 +252,8 @@ def wasderivedfrom(self, other):
252252
other_entity = other
253253
self.provenance.update(other_entity.bundle)
254254
if not self.activity:
255-
raise ValueError("Activity not initialized.")
255+
msg = "Activity not initialized."
256+
raise ValueError(msg)
256257
self.entity.wasDerivedFrom(other_entity, self.activity)
257258

258259
def _select_for_include(self):

esmvalcore/_recipe/_io.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,4 @@ def _load_recipe(recipe: Path | str | dict[str, Any] | None) -> dict[str, Any]:
3939
if isinstance(recipe, str):
4040
recipe = yaml.safe_load(recipe)
4141

42-
recipe = _copy(recipe)
43-
44-
return recipe # type: ignore
42+
return _copy(recipe)

0 commit comments

Comments
 (0)