Python Testing with pytest, Second Edition: Typing mistake (page 13)

In the Card class definition, the type hints for the summary, owner, and id attributes are currently specified as str, str, and int respectively, with default values of None.

@dataclass
class Card:
    """
    Represents a card with a name, description, and an optional image URL.
    """

    owner: str = None
    summary: str = None
    state: str = "todo"
    id: int = field(default=None, compare=False)

This is a minor mistake. To accurately reflect that these fields are optional and can be None, they should be defined using Optional from the typing module. The corrected type hints are:

  • owner should be Optional[str]
  • summary should be Optional[str]
  • id should be Optional[int]

The corrected class definition would appear as follows (assuming from typing import Optional is present):

from typing import Optional

@dataclass
class Card:
    """
    Represents a card with a name, description, and an optional image URL.
    """

    owner: Optional[str] = None
    summary: Optional[str] = None
    state: str = "todo"
    id: Optional[int] = field(default=None, compare=False)