Repository: pytest-dev/pytest
File: src/_pytest/stepwise.py (Python)

Code before:

    def update_date_to_now(self) -> None:
        self.last_cache_date_str = datetime.now().strftime(self._DATE_FORMAT)


class StepwisePlugin:
    def __init__(self, config: Config) -> None:
        self.config = config
        self.session: Session | None = None
        self.report_status: list[str] = []
        assert config.cache is not None
        self.cache: Cache = config.cache
        self.skip: bool = config.getoption("stepwise_skip")
        self.reset: bool = config.getoption("stepwise_reset")
        self.cached_info = self._load_cached_info()

    def _load_cached_info(self) -> StepwiseCacheInfo:
        cached_dict: dict[str, Any] | None = self.cache.get(STEPWISE_CACHE_DIR, None)
        if cached_dict:
            try:
                return StepwiseCacheInfo(
                    cached_dict["last_failed"],
                    cached_dict["last_test_count"],
                    cached_dict["last_cache_date_str"],
                )
            except Exception as e:
                error = f"{type(e).__name__}: {e}"
                self.report_status.append(f"error reading cache, discarding ({error})")

        # Cache not found or error during load, return a new cache.
        return StepwiseCacheInfo.empty()

    def pytest_sessionstart(self, session: Session) -> None:
        self.session = session

    def pytest_collection_modifyitems(
        self, config: Config, items: list[nodes.Item]
    ) -> None:
        last_test_count = self.cached_info.last_test_count
        self.cached_info.last_test_count = len(items)

        if self.reset:
            self.report_status.append("resetting state, not skipping.")
            self.cached_info.last_failed = None
            return

        if not self.cached_info.last_failed:
            self.report_status.append("no previously failed tests, not skipping.")
            return

        if last_test_count is not None and last_test_count != len(items):

Review comment (suggestion):
```suggestion
            except (KeyError, TypeError) as e:
```

maybe? I can't really see anything else that could happen here, unfortunately any other type issues would probably only surface sometime later when actually using the data.

Code after:
    def __init__(self, config: Config) -> None:
        self.config = config
        self.session: Session | None = None
        self.report_status: list[str] = []
        assert config.cache is not None
        self.cache: Cache = config.cache
        self.skip: bool = config.getoption("stepwise_skip")
        self.reset: bool = config.getoption("stepwise_reset")
        self.cached_info = self._load_cached_info()

    def _load_cached_info(self) -> StepwiseCacheInfo:
        cached_dict: dict[str, Any] | None = self.cache.get(STEPWISE_CACHE_DIR, None)
        if cached_dict:
            try:
                return StepwiseCacheInfo(
                    cached_dict["last_failed"],
                    cached_dict["last_test_count"],
                    cached_dict["last_cache_date_str"],
                )
            except (KeyError, TypeError) as e:
                error = f"{type(e).__name__}: {e}"
                self.report_status.append(f"error reading cache, discarding ({error})")

        # Cache not found or error during load, return a new cache.
        return StepwiseCacheInfo.empty()

    def pytest_sessionstart(self, session: Session) -> None:
        self.session = session

    def pytest_collection_modifyitems(
        self, config: Config, items: list[nodes.Item]
    ) -> None:
        last_test_count = self.cached_info.last_test_count
        self.cached_info.last_test_count = len(items)

        if self.reset:
            self.report_status.append("resetting state, not skipping.")
            self.cached_info.last_failed = None
            return

        if not self.cached_info.last_failed:
            self.report_status.append("no previously failed tests, not skipping.")
            return

        if last_test_count is not None and last_test_count != len(items):
            self.report_status.append(
                f"test count changed, not skipping (now {len(items)} tests, previously {last_test_count})."
            )
            self.cached_info.last_failed = None
            return