Skip to content

refactor(book-app): add type annotations and input validation#82

Closed
DanWahlin wants to merge 1 commit intomainfrom
add-type-annotations-and-input-validation
Closed

refactor(book-app): add type annotations and input validation#82
DanWahlin wants to merge 1 commit intomainfrom
add-type-annotations-and-input-validation

Conversation

@DanWahlin
Copy link
Copy Markdown
Collaborator

Summary

Updates samples/book-app-project/book_app.py with:

  • Type annotations: Added return type hints (-> None) to all functions, and list parameter type to show_books
  • Input validation: Empty string checks for title and author in handle_add() with user-friendly error messages

- 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>
Copilot AI review requested due to automatic review settings April 16, 2026 15:41
@DanWahlin DanWahlin closed this Apr 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the book_app.py CLI in samples/book-app-project by adding basic type annotations to functions and introducing simple input validation for adding books, improving clarity and user-facing robustness.

Changes:

  • Added -> None return annotations to CLI functions and a list parameter annotation to show_books.
  • Added empty-string validation for title and author in handle_add() with user-friendly error messages.
Show a summary per file
File Description
samples/book-app-project/book_app.py Adds function type annotations and validates title/author inputs when adding a book

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1



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.
@DanWahlin DanWahlin deleted the add-type-annotations-and-input-validation branch April 17, 2026 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants