| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import enum |
| 5 |
from functools import cached_property |
| 6 |
from functools import partial |
| 7 |
from functools import wraps |
| 8 |
from typing import Literal |
| 9 |
import warnings |
| 10 |
|
| 11 |
from _pytest.compat import assert_never |
| 12 |
from _pytest.compat import deprecated |
| 13 |
from _pytest.compat import get_real_func |
| 14 |
from _pytest.compat import safe_getattr |
| 15 |
from _pytest.compat import safe_isclass |
| 16 |
from _pytest.outcomes import OutcomeException |
| 17 |
import pytest |
| 18 |
|
| 19 |
|
| 20 |
def test_real_func_loop_limit() -> None: |
| 21 |
class Evil: |
| 22 |
def __init__(self): |
| 23 |
self.left = 1000 |
| 24 |
|
| 25 |
def __repr__(self): |
| 26 |
return f"<Evil left={self.left}>" |
| 27 |
|
| 28 |
def __getattr__(self, attr): |
| 29 |
if not self.left: |
| 30 |
raise RuntimeError("it's over") # pragma: no cover |
| 31 |
self.left -= 1 |
| 32 |
return self |
| 33 |
|
| 34 |
evil = Evil() |
| 35 |
|
| 36 |
with pytest.raises( |
| 37 |
ValueError, |
| 38 |
match=("wrapper loop when unwrapping <Evil left=998>"), |
| 39 |
): |
| 40 |
get_real_func(evil) |
| 41 |
|
| 42 |
|
| 43 |
def test_get_real_func() -> None: |
| 44 |
"""Check that get_real_func correctly unwraps decorators until reaching the real function""" |
| 45 |
|
| 46 |
def decorator(f): |
| 47 |
@wraps(f) |
| 48 |
def inner(): |
| 49 |
pass # pragma: no cover |
| 50 |
|
| 51 |
return inner |
| 52 |
|
| 53 |
def func(): |
| 54 |
pass # pragma: no cover |
| 55 |
|
| 56 |
wrapped_func = decorator(decorator(func)) |
| 57 |
assert get_real_func(wrapped_func) is func |
| 58 |
|
| 59 |
wrapped_func2 = decorator(decorator(wrapped_func)) |
| 60 |
assert get_real_func(wrapped_func2) is func |
| 61 |
|
| 62 |
# obtain the function up until the point a function was wrapped by pytest itself |
| 63 |
@pytest.fixture |
| 64 |
def wrapped_func3(): |
| 65 |
pass # pragma: no cover |
| 66 |
|
| 67 |
wrapped_func4 = decorator(wrapped_func3) |
| 68 |
assert get_real_func(wrapped_func4) is wrapped_func3._get_wrapped_function() |
| 69 |
|
| 70 |
|
| 71 |
def test_get_real_func_partial() -> None: |
| 72 |
"""Test get_real_func handles partial instances correctly""" |
| 73 |
|
| 74 |
def foo(x): |
| 75 |
return x |
| 76 |
|
| 77 |
assert get_real_func(foo) is foo |
| 78 |
assert get_real_func(partial(foo)) is foo |
| 79 |
|
| 80 |
|
| 81 |
class ErrorsHelper: |
| 82 |
@property |
| 83 |
def raise_baseexception(self): |
| 84 |
raise BaseException("base exception should be raised") |
| 85 |
|
| 86 |
@property |
| 87 |
def raise_exception(self): |
| 88 |
raise Exception("exception should be caught") |
| 89 |
|
| 90 |
@property |
| 91 |
def raise_fail_outcome(self): |
| 92 |
pytest.fail("fail should be caught") |
| 93 |
|
| 94 |
|
| 95 |
def test_helper_failures() -> None: |
| 96 |
helper = ErrorsHelper() |
| 97 |
with pytest.raises(Exception): # noqa: B017 |
| 98 |
_ = helper.raise_exception |
| 99 |
with pytest.raises(OutcomeException): |
| 100 |
_ = helper.raise_fail_outcome |
| 101 |
|
| 102 |
|
| 103 |
def test_safe_getattr() -> None: |
| 104 |
helper = ErrorsHelper() |
| 105 |
assert safe_getattr(helper, "raise_exception", "default") == "default" |
| 106 |
assert safe_getattr(helper, "raise_fail_outcome", "default") == "default" |
| 107 |
with pytest.raises(BaseException): # noqa: B017 |
| 108 |
assert safe_getattr(helper, "raise_baseexception", "default") |
| 109 |
|
| 110 |
|
| 111 |
def test_safe_isclass() -> None: |
| 112 |
assert safe_isclass(type) is True |
| 113 |
|
| 114 |
class CrappyClass(Exception): |
| 115 |
# Type ignored because it's bypassed intentionally. |
| 116 |
@property # type: ignore |
| 117 |
def __class__(self): |
| 118 |
assert False, "Should be ignored" |
| 119 |
|
| 120 |
assert safe_isclass(CrappyClass()) is False |
| 121 |
|
| 122 |
|
| 123 |
def test_cached_property() -> None: |
| 124 |
ncalls = 0 |
| 125 |
|
| 126 |
class Class: |
| 127 |
@cached_property |
| 128 |
def prop(self) -> int: |
| 129 |
nonlocal ncalls |
| 130 |
ncalls += 1 |
| 131 |
return ncalls |
| 132 |
|
| 133 |
c1 = Class() |
| 134 |
assert ncalls == 0 |
| 135 |
assert c1.prop == 1 |
| 136 |
assert c1.prop == 1 |
| 137 |
c2 = Class() |
| 138 |
assert ncalls == 1 |
| 139 |
assert c2.prop == 2 |
| 140 |
assert c1.prop == 1 |
| 141 |
|
| 142 |
|
| 143 |
def test_assert_never_union() -> None: |
| 144 |
x: int | str = 10 |
| 145 |
|
| 146 |
if isinstance(x, int): |
| 147 |
pass |
| 148 |
else: |
| 149 |
with pytest.raises(AssertionError): |
| 150 |
assert_never(x) # type: ignore[arg-type] |
| 151 |
|
| 152 |
if isinstance(x, int): |
| 153 |
pass |
| 154 |
elif isinstance(x, str): |
| 155 |
pass |
| 156 |
else: |
| 157 |
assert_never(x) |
| 158 |
|
| 159 |
|
| 160 |
def test_assert_never_enum() -> None: |
| 161 |
E = enum.Enum("E", "a b") |
| 162 |
x: E = E.a |
| 163 |
|
| 164 |
if x is E.a: |
| 165 |
pass |
| 166 |
else: |
| 167 |
with pytest.raises(AssertionError): |
| 168 |
assert_never(x) # type: ignore[arg-type] |
| 169 |
|
| 170 |
if x is E.a: |
| 171 |
pass |
| 172 |
elif x is E.b: |
| 173 |
pass |
| 174 |
else: |
| 175 |
assert_never(x) |
| 176 |
|
| 177 |
|
| 178 |
def test_assert_never_literal() -> None: |
| 179 |
x: Literal["a", "b"] = "a" |
| 180 |
|
| 181 |
if x == "a": |
| 182 |
pass |
| 183 |
else: |
| 184 |
with pytest.raises(AssertionError): |
| 185 |
assert_never(x) # type: ignore[arg-type] |
| 186 |
|
| 187 |
if x == "a": |
| 188 |
pass |
| 189 |
elif x == "b": |
| 190 |
pass |
| 191 |
else: |
| 192 |
assert_never(x) |
| 193 |
|
| 194 |
|
| 195 |
def test_deprecated() -> None: |
| 196 |
# This test is mostly for coverage. |
| 197 |
|
| 198 |
@deprecated("This is deprecated!") |
| 199 |
def old_way() -> str: |
| 200 |
return "human intelligence" |
| 201 |
|
| 202 |
with warnings.catch_warnings(): |
| 203 |
warnings.simplefilter("ignore", DeprecationWarning) |
| 204 |
assert old_way() == "human intelligence" # type: ignore[deprecated] |