aliases: add warnAlias

This commit is contained in:
Emily 2025-10-27 19:05:09 +00:00
parent 97eba2f29f
commit c5b82e49b3
2 changed files with 29 additions and 4 deletions

View file

@ -98,8 +98,8 @@ def get_date_lists(
date_too_complex_list.append(f"{lineno:>5} {line}")
elif "= with " in line:
date_too_complex_list.append(f"{lineno:>5} {line}")
elif "lib.warnOnInstantiate" in line or "warning" in line:
if 'lib.warnOnInstantiate "' in line:
elif "warnAlias" in line or "warning" in line:
if 'warnAlias "' in line:
date_older_warning_list.append(line)
else:
date_too_complex_list.append(f"{lineno:>5} {line}")
@ -140,7 +140,7 @@ def convert(lines: list[str], convert_to: str) -> list[tuple[str, str]]:
before_equal, after_equal = (
x.strip() for x in line.split("=", maxsplit=2)
)
if after_equal.startswith("lib.warnOnInstantiate"):
if after_equal.startswith("warnAlias"):
after_equal = after_equal.split("\"", maxsplit=3)[2].strip()
except ValueError as err:
print(err, line, "\n")
@ -162,7 +162,7 @@ def convert(lines: list[str], convert_to: str) -> list[tuple[str, str]]:
converted_lines[line] = converted
elif convert_to == "warnings":
converted = (
f"{indent}{alias} = lib.warnOnInstantiate \"'{alias_unquoted}' has been"
f"{indent}{alias} = warnAlias \"'{alias_unquoted}' has been"
f" renamed to/replaced by '{replacement}'\" {replacement};"
f" # Converted to warning {datetime.today().strftime('%Y-%m-%d')}"
)

View file

@ -231,6 +231,31 @@ let
aliases: lib.mapAttrs (n: alias: removeRecurseForDerivations (checkInPkgs n alias)) aliases;
plasma5Throws = mapAliases (lib.mapAttrs (k: _: makePlasma5Throw k) deprecatedPlasma5Packages);
warnAlias =
msg: v:
if lib.isDerivation v then
lib.warnOnInstantiate msg v
else if lib.isAttrs v then
lib.mapAttrs (lib.warn msg) v
else if lib.isFunction v then
arg: lib.warn msg (v arg)
else if lib.isList v then
map (lib.warn msg) v
else if lib.isString v then
# Unlike the other cases, this changes the type of the value and
# is therefore a breaking change for some code, but its the best
# we can do.
{ __toString = lib.warn msg (lib.const v); }
else
# Cant do better than this, and a `throw` would be more
# disruptive for users…
#
# `nix search` flags up warnings already, so hopefully this wont
# make things much worse until we have proper CI for aliases,
# especially since aliases of paths and numbers are presumably
# not common.
lib.warn msg v;
in
mapAliases {