refactor(book-app): add type annotations and input validation#82
refactor(book-app): add type annotations and input validation#82
Conversation
- 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>
There was a problem hiding this comment.
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
-> Nonereturn annotations to CLI functions and alistparameter annotation toshow_books. - Added empty-string validation for
titleandauthorinhandle_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: |
There was a problem hiding this comment.
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:
Summary
Updates
samples/book-app-project/book_app.pywith:-> None) to all functions, andlistparameter type toshow_bookstitleandauthorinhandle_add()with user-friendly error messages