Skip to content

Commit 9498a82

Browse files
authored
Merge pull request #13767 from bluetech/compat-changes
compat: a few minor cleanups
2 parents 184f5f1 + 082a27e commit 9498a82

File tree

6 files changed

+18
-45
lines changed

6 files changed

+18
-45
lines changed

src/_pytest/assertion/truncate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import annotations
88

9-
from _pytest.assertion import util
9+
from _pytest.compat import running_on_ci
1010
from _pytest.config import Config
1111
from _pytest.nodes import Item
1212

@@ -43,7 +43,7 @@ def _get_truncation_parameters(item: Item) -> tuple[bool, int, int]:
4343

4444
verbose = item.config.get_verbosity(Config.VERBOSITY_ASSERTIONS)
4545

46-
should_truncate = verbose < 2 and not util.running_on_ci()
46+
should_truncate = verbose < 2 and not running_on_ci()
4747
should_truncate = should_truncate and (max_lines > 0 or max_chars > 0)
4848

4949
return should_truncate, max_lines, max_chars

src/_pytest/assertion/util.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from collections.abc import Mapping
1010
from collections.abc import Sequence
1111
from collections.abc import Set as AbstractSet
12-
import os
1312
import pprint
1413
from typing import Any
1514
from typing import Literal
@@ -21,6 +20,7 @@
2120
from _pytest._io.pprint import PrettyPrinter
2221
from _pytest._io.saferepr import saferepr
2322
from _pytest._io.saferepr import saferepr_unlimited
23+
from _pytest.compat import running_on_ci
2424
from _pytest.config import Config
2525

2626

@@ -613,9 +613,3 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> list[str]:
613613
else:
614614
newdiff.append(line)
615615
return newdiff
616-
617-
618-
def running_on_ci() -> bool:
619-
"""Check if we're currently running on a CI system."""
620-
env_vars = ["CI", "BUILD_NUMBER"]
621-
return any(var in os.environ for var in env_vars)

src/_pytest/cacheprovider.py

100755100644
File mode changed.

src/_pytest/compat.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# mypy: allow-untyped-defs
2-
"""Python version compatibility code."""
2+
"""Python version compatibility code and random general utilities."""
33

44
from __future__ import annotations
55

@@ -278,39 +278,12 @@ def get_user_id() -> int | None:
278278
return uid if uid != ERROR else None
279279

280280

281-
# Perform exhaustiveness checking.
282-
#
283-
# Consider this example:
284-
#
285-
# MyUnion = Union[int, str]
286-
#
287-
# def handle(x: MyUnion) -> int {
288-
# if isinstance(x, int):
289-
# return 1
290-
# elif isinstance(x, str):
291-
# return 2
292-
# else:
293-
# raise Exception('unreachable')
294-
#
295-
# Now suppose we add a new variant:
296-
#
297-
# MyUnion = Union[int, str, bytes]
298-
#
299-
# After doing this, we must remember ourselves to go and update the handle
300-
# function to handle the new variant.
301-
#
302-
# With `assert_never` we can do better:
303-
#
304-
# // raise Exception('unreachable')
305-
# return assert_never(x)
306-
#
307-
# Now, if we forget to handle the new variant, the type-checker will emit a
308-
# compile-time error, instead of the runtime error we would have gotten
309-
# previously.
310-
#
311-
# This also work for Enums (if you use `is` to compare) and Literals.
312-
def assert_never(value: NoReturn) -> NoReturn:
313-
assert False, f"Unhandled value: {value} ({type(value).__name__})"
281+
if sys.version_info >= (3, 11):
282+
from typing import assert_never
283+
else:
284+
285+
def assert_never(value: NoReturn) -> NoReturn:
286+
assert False, f"Unhandled value: {value} ({type(value).__name__})"
314287

315288

316289
class CallableBool:
@@ -331,3 +304,9 @@ def __bool__(self) -> bool:
331304

332305
def __call__(self) -> bool:
333306
return self._value
307+
308+
309+
def running_on_ci() -> bool:
310+
"""Check if we're currently running on a CI system."""
311+
env_vars = ["CI", "BUILD_NUMBER"]
312+
return any(var in os.environ for var in env_vars)

src/_pytest/terminal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from _pytest._io import TerminalWriter
4040
from _pytest._io.wcwidth import wcswidth
4141
import _pytest._version
42-
from _pytest.assertion.util import running_on_ci
42+
from _pytest.compat import running_on_ci
4343
from _pytest.config import _PluggyPlugin
4444
from _pytest.config import Config
4545
from _pytest.config import ExitCode

testing/test_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import tempfile
1111
import textwrap
1212

13-
from _pytest.assertion.util import running_on_ci
13+
from _pytest.compat import running_on_ci
1414
from _pytest.config import ExitCode
1515
from _pytest.fixtures import FixtureRequest
1616
from _pytest.main import _in_venv

0 commit comments

Comments
 (0)