9
9
from typing import final
10
10
from typing import Literal
11
11
from typing import TextIO
12
- from typing import TYPE_CHECKING
12
+
13
+ import pygments
14
+ from pygments .formatters .terminal import TerminalFormatter
15
+ from pygments .lexer import Lexer
16
+ from pygments .lexers .diff import DiffLexer
17
+ from pygments .lexers .python import PythonLexer
13
18
14
19
from ..compat import assert_never
15
20
from .wcwidth import wcswidth
16
21
17
22
18
- if TYPE_CHECKING :
19
- from pygments .formatter import Formatter
20
- from pygments .lexer import Lexer
21
-
22
-
23
23
# This code was initially copied from py 1.8.1, file _io/terminalwriter.py.
24
24
25
25
@@ -201,37 +201,22 @@ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> No
201
201
for indent , new_line in zip (indents , new_lines ):
202
202
self .line (indent + new_line )
203
203
204
- def _get_pygments_lexer (self , lexer : Literal ["python" , "diff" ]) -> Lexer | None :
205
- try :
206
- if lexer == "python" :
207
- from pygments .lexers .python import PythonLexer
208
-
209
- return PythonLexer ()
210
- elif lexer == "diff" :
211
- from pygments .lexers .diff import DiffLexer
212
-
213
- return DiffLexer ()
214
- else :
215
- assert_never (lexer )
216
- except ModuleNotFoundError :
217
- return None
218
-
219
- def _get_pygments_formatter (self ) -> Formatter | None :
220
- try :
221
- import pygments .util
222
- except ModuleNotFoundError :
223
- return None
204
+ def _get_pygments_lexer (self , lexer : Literal ["python" , "diff" ]) -> Lexer :
205
+ if lexer == "python" :
206
+ return PythonLexer ()
207
+ elif lexer == "diff" :
208
+ return DiffLexer ()
209
+ else :
210
+ assert_never (lexer )
224
211
212
+ def _get_pygments_formatter (self ) -> TerminalFormatter :
225
213
from _pytest .config .exceptions import UsageError
226
214
227
215
theme = os .getenv ("PYTEST_THEME" )
228
216
theme_mode = os .getenv ("PYTEST_THEME_MODE" , "dark" )
229
217
230
218
try :
231
- from pygments .formatters .terminal import TerminalFormatter
232
-
233
219
return TerminalFormatter (bg = theme_mode , style = theme )
234
-
235
220
except pygments .util .ClassNotFound as e :
236
221
raise UsageError (
237
222
f"PYTEST_THEME environment variable has an invalid value: '{ theme } '. "
@@ -251,16 +236,11 @@ def _highlight(
251
236
return source
252
237
253
238
pygments_lexer = self ._get_pygments_lexer (lexer )
254
- if pygments_lexer is None :
255
- return source
256
-
257
239
pygments_formatter = self ._get_pygments_formatter ()
258
- if pygments_formatter is None :
259
- return source
260
-
261
- from pygments import highlight
262
240
263
- highlighted : str = highlight (source , pygments_lexer , pygments_formatter )
241
+ highlighted : str = pygments .highlight (
242
+ source , pygments_lexer , pygments_formatter
243
+ )
264
244
# pygments terminal formatter may add a newline when there wasn't one.
265
245
# We don't want this, remove.
266
246
if highlighted [- 1 ] == "\n " and source [- 1 ] != "\n " :
0 commit comments