-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix type hints for TestReport.when
and TestReport.location
#13345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
These are set in `TestReport.__init__` but weren't being picked up by type checkers because of the annotations on `BaseReport`. Fixes pytest-dev#12941.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @rouge8!
I don't think this actually works. I added a test: diff --git i/testing/typing_checks.py w/testing/typing_checks.py
index d4d6a97ae..33180048e 100644
--- i/testing/typing_checks.py
+++ w/testing/typing_checks.py
@@ -8,12 +8,12 @@ none of the code triggers any mypy errors.
from __future__ import annotations
import contextlib
-from typing import Optional
+from typing import Optional, Literal
from typing_extensions import assert_type
import pytest
-from pytest import MonkeyPatch
+from pytest import MonkeyPatch, TestReport
# Issue #7488.
@@ -51,3 +51,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, int | None, str]) and mypy says:
which means the IMHO this should be reverted and the issue reopened. |
Interesting, I didn't test with from typing import assert_never
import pytest
def match_it(report: pytest.TestReport) -> None:
match report.when:
case "setup":
pass
case "call":
pass
case "teardown":
pass
case _:
assert_never(report.when) On main (without this merged), mypy errors with:
On my branch, it doesn't have any errors. |
The |
Hmm thanks for checking @The-Compiler, I was away from the computer and it did seem right to me. Just so we are all on the same page, which mypy version everyone is using? |
Oh, my bad... I was accidentally running mypy against my system-wide pytest instead of the sources in the repo 😅 What about the |
Good catch! I opened #13348 with that change and the test you suggested. |
Based on discussion in #13345.
These are set in
TestReport.__init__
but weren't being picked up by type checkers because of the annotations onBaseReport
.Fixes #12941.