Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions samples/book-app-project/book_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
collection = BookCollection()


def show_books(books):
def show_books(books: list) -> None:
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show_books is annotated as books: list, which is effectively list[Any] and doesn’t convey the expected element type. Since this function assumes book has .read/.title/.author/.year, the annotation should be parameterized (e.g., list[Book] / List[Book]) or widened to a read-only abstraction like Sequence[Book] for better type safety and to match the project’s use of typed BookCollection return values.

See below for a potential fix:

from typing import Protocol, Sequence
from books import BookCollection


class SupportsShownBook(Protocol):
    read: bool
    title: str
    author: str
    year: int


# Global collection instance
collection = BookCollection()


def show_books(books: Sequence[SupportsShownBook]) -> None:

Copilot uses AI. Check for mistakes.
"""Display books in a user-friendly format."""
if not books:
print("No books found.")
Expand All @@ -21,16 +21,24 @@ def show_books(books):
print()


def handle_list():
def handle_list() -> None:
books = collection.list_books()
show_books(books)


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

title = input("Title: ").strip()
if not title:
print("\nError: Title cannot be empty.\n")
return

author = input("Author: ").strip()
if not author:
print("\nError: Author cannot be empty.\n")
return

year_str = input("Year: ").strip()

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


def handle_remove():
def handle_remove() -> None:
print("\nRemove a Book\n")

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


def handle_find():
def handle_find() -> None:
print("\nFind Books by Author\n")

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


def show_help():
def show_help() -> None:
print("""
Book Collection Helper

Expand All @@ -72,7 +80,7 @@ def show_help():
""")


def main():
def main() -> None:
if len(sys.argv) < 2:
show_help()
return
Expand Down
Loading