Skip to content

Commit 3acda93

Browse files
committed
we don't need to import entire pytest
1 parent 4bb7a7b commit 3acda93

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import ast
5+
import enum
56
import hashlib
67
import os
78
import pickle
@@ -11,12 +12,11 @@
1112
import unittest
1213
from collections import defaultdict
1314
from pathlib import Path
14-
from typing import TYPE_CHECKING, Callable, Optional
15+
from typing import TYPE_CHECKING, Callable, Optional, final
1516

1617
if TYPE_CHECKING:
1718
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
1819

19-
import pytest
2020
from pydantic.dataclasses import dataclass
2121
from rich.panel import Panel
2222
from rich.text import Text
@@ -35,6 +35,22 @@
3535
from codeflash.verification.verification_utils import TestConfig
3636

3737

38+
@final
39+
class PytestExitCode(enum.IntEnum): # don't need to import entire pytest just for this
40+
#: Tests passed.
41+
OK = 0
42+
#: Tests failed.
43+
TESTS_FAILED = 1
44+
#: pytest was interrupted.
45+
INTERRUPTED = 2
46+
#: An internal error got in the way.
47+
INTERNAL_ERROR = 3
48+
#: pytest was misused.
49+
USAGE_ERROR = 4
50+
#: pytest couldn't find tests.
51+
NO_TESTS_COLLECTED = 5
52+
53+
3854
@dataclass(frozen=True)
3955
class TestFunction:
4056
function_name: str
@@ -412,15 +428,15 @@ def discover_tests_pytest(
412428
error_section = match.group(1) if match else result.stdout
413429

414430
logger.warning(
415-
f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}\n {error_section}"
431+
f"Failed to collect tests. Pytest Exit code: {exitcode}={PytestExitCode(exitcode).name}\n {error_section}"
416432
)
417433
if "ModuleNotFoundError" in result.stdout:
418434
match = ImportErrorPattern.search(result.stdout).group()
419435
panel = Panel(Text.from_markup(f"⚠️ {match} ", style="bold red"), expand=False)
420436
console.print(panel)
421437

422438
elif 0 <= exitcode <= 5:
423-
logger.warning(f"Failed to collect tests. Pytest Exit code: {exitcode}={pytest.ExitCode(exitcode).name}")
439+
logger.warning(f"Failed to collect tests. Pytest Exit code: {exitcode}={PytestExitCode(exitcode).name}")
424440
else:
425441
logger.warning(f"Failed to collect tests. Pytest Exit code: {exitcode}")
426442
console.rule()

0 commit comments

Comments
 (0)