From 1682ee75e369a6d241088cca6748ae24dba72d3b Mon Sep 17 00:00:00 2001 From: Andy Freeland Date: Thu, 3 Apr 2025 11:23:10 -0700 Subject: [PATCH 1/2] Clean up type hints from #13345 and add a test --- changelog/13348.misc.rst | 1 + src/_pytest/reports.py | 4 +--- testing/typing_checks.py | 8 ++++++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 changelog/13348.misc.rst diff --git a/changelog/13348.misc.rst b/changelog/13348.misc.rst new file mode 100644 index 00000000000..df87c6e338c --- /dev/null +++ b/changelog/13348.misc.rst @@ -0,0 +1 @@ +Clean up type hints on :attr:`pytest.TestReport.when` and add a test. diff --git a/src/_pytest/reports.py b/src/_pytest/reports.py index c85bf78594f..480ffae1f9c 100644 --- a/src/_pytest/reports.py +++ b/src/_pytest/reports.py @@ -261,8 +261,6 @@ class TestReport(BaseReport): __test__ = False - when: Literal["setup", "call", "teardown"] - location: tuple[str, int | None, str] # Defined by skipping plugin. # xfail reason if xfailed, otherwise not defined. Use hasattr to distinguish. wasxfail: str @@ -307,7 +305,7 @@ def __init__( self.longrepr = longrepr #: One of 'setup', 'call', 'teardown' to indicate runtest phase. - self.when = when + self.when: Literal["setup", "call", "teardown"] = when #: User properties is a list of tuples (name, value) that holds user #: defined properties of the test. diff --git a/testing/typing_checks.py b/testing/typing_checks.py index d4d6a97aea6..8a316580a25 100644 --- a/testing/typing_checks.py +++ b/testing/typing_checks.py @@ -8,12 +8,14 @@ from __future__ import annotations import contextlib +from typing import Literal from typing import Optional from typing_extensions import assert_type import pytest from pytest import MonkeyPatch +from pytest import TestReport # Issue #7488. @@ -51,3 +53,9 @@ def check_raises_is_a_context_manager(val: bool) -> None: with pytest.raises(RuntimeError) if val else contextlib.nullcontext() as excinfo: pass assert_type(excinfo, Optional[pytest.ExceptionInfo[RuntimeError]]) + + +# Issue #12941. +def check_testreport_attributes(report: TestReport) -> None: + assert_type(report.when, Literal["setup", "call", "teardown"]) + assert_type(report.location, tuple[str, Optional[int], str]) From 4ab733a6f763faef4ef707879cd4c8dd7058d10e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 3 Apr 2025 16:44:25 -0300 Subject: [PATCH 2/2] Delete changelog/13348.misc.rst --- changelog/13348.misc.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 changelog/13348.misc.rst diff --git a/changelog/13348.misc.rst b/changelog/13348.misc.rst deleted file mode 100644 index df87c6e338c..00000000000 --- a/changelog/13348.misc.rst +++ /dev/null @@ -1 +0,0 @@ -Clean up type hints on :attr:`pytest.TestReport.when` and add a test.