Skip to content

Commit d885de3

Browse files
DanWahlinCopilot
andcommitted
refactor(book-app): add type annotations and input validation
- Add return type annotations to all functions - Add empty string validation for title and author in handle_add() Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ffc668a commit d885de3

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

samples/book-app-project/book_app.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
collection = BookCollection()
77

88

9-
def show_books(books):
9+
def show_books(books: list) -> None:
1010
"""Display books in a user-friendly format."""
1111
if not books:
1212
print("No books found.")
@@ -21,16 +21,24 @@ def show_books(books):
2121
print()
2222

2323

24-
def handle_list():
24+
def handle_list() -> None:
2525
books = collection.list_books()
2626
show_books(books)
2727

2828

29-
def handle_add():
29+
def handle_add() -> None:
3030
print("\nAdd a New Book\n")
3131

3232
title = input("Title: ").strip()
33+
if not title:
34+
print("\nError: Title cannot be empty.\n")
35+
return
36+
3337
author = input("Author: ").strip()
38+
if not author:
39+
print("\nError: Author cannot be empty.\n")
40+
return
41+
3442
year_str = input("Year: ").strip()
3543

3644
try:
@@ -41,7 +49,7 @@ def handle_add():
4149
print(f"\nError: {e}\n")
4250

4351

44-
def handle_remove():
52+
def handle_remove() -> None:
4553
print("\nRemove a Book\n")
4654

4755
title = input("Enter the title of the book to remove: ").strip()
@@ -50,7 +58,7 @@ def handle_remove():
5058
print("\nBook removed if it existed.\n")
5159

5260

53-
def handle_find():
61+
def handle_find() -> None:
5462
print("\nFind Books by Author\n")
5563

5664
author = input("Author name: ").strip()
@@ -59,7 +67,7 @@ def handle_find():
5967
show_books(books)
6068

6169

62-
def show_help():
70+
def show_help() -> None:
6371
print("""
6472
Book Collection Helper
6573
@@ -72,7 +80,7 @@ def show_help():
7280
""")
7381

7482

75-
def main():
83+
def main() -> None:
7684
if len(sys.argv) < 2:
7785
show_help()
7886
return

0 commit comments

Comments
 (0)