Skip to content

Commit dede4a2

Browse files
committed
chore: Fix more type issues
1 parent c3b9435 commit dede4a2

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

normfn

+26-26
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,17 @@ def main(argv, syserr_handler):
270270
logger.debug(f"Valid years are: {year_list}")
271271
YEAR = r"(" + "|".join(year_list) + r")"
272272

273+
filenames: list[str] = args.filenames # pyright: ignore[reportAny]
274+
273275
with suppress(QuitException):
274-
for filename in args.filenames:
276+
for filename in filenames:
275277
filename = os.path.abspath(filename)
276278
if not os.path.exists(filename):
277279
raise FatalException(
278280
filename + " specified on the command line does not exist."
279281
)
280282

281-
if os.path.isdir(filename) and args.recursive:
283+
if os.path.isdir(filename) and args.recursive: # pyright: ignore[reportAny]
282284
new_filename = process_filename(filename, args)
283285
assert new_filename is not None
284286
walk_tree(new_filename, args)
@@ -313,8 +315,8 @@ def walk_tree(dirname: str, args):
313315
def datetime_prefix(args, non_extension: str, filename: str):
314316
logger = logging.getLogger("normfn")
315317

316-
def first_not_none(list: list):
317-
for item in list:
318+
def first_not_none[T](lst: list[T]):
319+
for item in lst:
318320
if item is not None:
319321
return item
320322

@@ -477,7 +479,7 @@ def datetime_prefix(args, non_extension: str, filename: str):
477479
if number_of_subs == 0:
478480
logger.debug("Didn't find date or time")
479481

480-
timetouse = get_timetouse(args, filename)
482+
timetouse = get_timetouse(args.time_option, filename)
481483

482484
newname_with_dash_if_needed = (
483485
("-" + newname) if not args.discard_existing_name else ""
@@ -519,7 +521,7 @@ def process_filename(filename: str, args):
519521
logger.debug("New filename would be identical, skipping.")
520522
return original_filename
521523

522-
validate_move(args, original_filename, filename)
524+
validate_move(args.force, original_filename, filename)
523525

524526
move_it = True
525527

@@ -532,14 +534,14 @@ def process_filename(filename: str, args):
532534
new_filename = os.path.join(
533535
os.path.dirname(original_filename), new_filename
534536
)
535-
validate_move(args, original_filename, new_filename)
537+
validate_move(args.force, original_filename, new_filename)
536538
filename = new_filename
537539
else:
538540
move_it = True if move_it == b"y" else False
539541

540542
if move_it:
541543
if not args.dry_run:
542-
shiftfile(args, original_filename, filename)
544+
shiftfile(args.undo_log_file, original_filename, filename)
543545
return filename
544546
else:
545547
logger.info(
@@ -569,22 +571,22 @@ def should_exclude(filename: str, basename: str):
569571
return (match, exclude_pattern)
570572

571573

572-
def get_timetouse(args, filename: str):
574+
def get_timetouse(time_option: str, filename: str):
573575
ctime = datetime.fromtimestamp(os.path.getctime(filename))
574576
mtime = datetime.fromtimestamp(os.path.getmtime(filename))
575577

576-
if args.time_option == "now":
578+
if time_option == "now":
577579
timetouse = datetime.now()
578-
elif args.time_option == "earliest":
580+
elif time_option == "earliest":
579581
timetouse = min(ctime, mtime)
580582
else:
581583
timetouse = max(ctime, mtime)
582584

583585
return timetouse
584586

585587

586-
def validate_move(args, original_filename: str, filename: str) -> None:
587-
if os.path.exists(filename) and not args.force:
588+
def validate_move(force: bool, original_filename: str, filename: str) -> None:
589+
if os.path.exists(filename) and not force:
588590
raise FatalException(
589591
f"Want to move {original_filename.strip()} to "
590592
f"{filename}, but it already exists."
@@ -604,7 +606,7 @@ def rlinput(prompt: str, prefill: str = ""):
604606
readline.set_startup_hook()
605607

606608

607-
def shiftfile(args, source: str, target: str):
609+
def shiftfile(undo_log_file: str, source: str, target: str):
608610
logger = logging.getLogger("normfn")
609611

610612
source = os.path.abspath(source)
@@ -623,17 +625,17 @@ def shiftfile(args, source: str, target: str):
623625
else:
624626
raise
625627

626-
if args.undo_log_file:
627-
check_undo_log_file_header(args)
628-
with open(args.undo_log_file, "a", encoding="utf-8") as log_file:
628+
if undo_log_file:
629+
check_undo_log_file_header(undo_log_file)
630+
with open(undo_log_file, "a", encoding="utf-8") as log_file:
629631
_ = log_file.write(f"# {dt_now}: moving {source} to {target}\n")
630632
_ = log_file.write(f"mv {shlex.quote(target)} {shlex.quote(source)}\n")
631633
logger.info(source + " moved to " + target)
632634

633635

634-
def check_undo_log_file_header(args):
635-
if not os.path.exists(args.undo_log_file):
636-
with open(args.undo_log_file, "w") as log_file:
636+
def check_undo_log_file_header(undo_log_file: str):
637+
if not os.path.exists(undo_log_file):
638+
with open(undo_log_file, "w") as log_file:
637639
wrapper = textwrap.TextWrapper(initial_indent="# ", subsequent_indent="# ")
638640
_ = log_file.write("#!/bin/sh\n")
639641
_ = log_file.write(
@@ -649,9 +651,7 @@ def check_undo_log_file_header(args):
649651
wrapper.fill(
650652
"This file contains shell commands which can be run to invert (undo) the effects of "
651653
"running normfn. They must be run in *reverse order*. You can achieve "
652-
"this by running `tac "
653-
+ args.undo_log_file
654-
+ " | sh`. If you wish, you can edit "
654+
f"this by running `tac {undo_log_file} | sh`. If you wish, you can edit "
655655
"the file first to control which actions are undone."
656656
)
657657
+ "\n"
@@ -706,10 +706,10 @@ def insensitiveize(string: str) -> str:
706706
return "".join(map(lambda char: ("[" + char.lower() + char.upper() + "]"), string))
707707

708708

709-
class FatalException(Exception):
710-
def __init__(self, value):
709+
class FatalException[T](Exception):
710+
def __init__(self, value: T):
711711
Exception.__init__(self, value)
712-
self.value = value
712+
self.value: T = value
713713

714714
def __str__(self):
715715
return repr(self.value)

0 commit comments

Comments
 (0)