diff --git a/stdlib/@tests/test_cases/check_logging.py b/stdlib/@tests/test_cases/check_logging.py index fe3d8eb16fd0..5e39b066fd68 100644 --- a/stdlib/@tests/test_cases/check_logging.py +++ b/stdlib/@tests/test_cases/check_logging.py @@ -1,10 +1,12 @@ from __future__ import annotations +import io import logging import logging.handlers import multiprocessing import queue -from typing import Any +from typing import Any, Union +from typing_extensions import assert_type # This pattern comes from the logging docs, and should therefore pass a type checker # See https://docs.python.org/3/library/logging.html#logrecord-objects @@ -28,3 +30,7 @@ def record_factory(*args: Any, **kwargs: Any) -> logging.LogRecord: logging.handlers.QueueListener(queue.Queue()) logging.handlers.QueueListener(queue.SimpleQueue()) logging.handlers.QueueListener(multiprocessing.Queue()) + +# FileHandler.stream can be None when delay=True or after close() +fh = logging.FileHandler("test.log", delay=True) +assert_type(fh.stream, Union[io.TextIOWrapper, None]) diff --git a/stdlib/logging/__init__.pyi b/stdlib/logging/__init__.pyi index 8248f82ea87a..3a79e3a65d19 100644 --- a/stdlib/logging/__init__.pyi +++ b/stdlib/logging/__init__.pyi @@ -616,6 +616,7 @@ class FileHandler(StreamHandler[TextIOWrapper]): encoding: str | None # undocumented delay: bool # undocumented errors: str | None # undocumented + stream: TextIOWrapper | None # type: ignore[assignment] # None when delay=True or after close() def __init__( self, filename: StrPath, mode: str = "a", encoding: str | None = None, delay: bool = False, errors: str | None = None ) -> None: ...