11# -*- coding: utf-8 -*-
2- # Local replacement for the pytest-black plugin that supports pytest 7 +.
2+ # Local replacement for the pytest-black plugin that supports pytest 8.4 +.
33# pytest-black 0.6.0 uses the deprecated `path` argument in pytest_collect_file,
44# which was removed in pytest 8.1. This conftest provides a compatible implementation.
55
99
1010import pytest
1111
12- PYTEST_VER = tuple (int (x ) for x in pytest .__version__ .split ("." )[:2 ])
13-
1412try :
15- import tomllib
13+ import tomli
1614except ImportError :
17- try :
18- import tomli as tomllib # type: ignore[no-redef]
19- except ImportError :
20- tomllib = None # type: ignore[assignment]
15+ tomli = None # type: ignore[assignment]
2116
2217HISTKEY = "black/mtimes"
2318
@@ -42,28 +37,14 @@ def pytest_unconfigure(config):
4237 config .cache .set (HISTKEY , config ._blackmtimes )
4338
4439
45- if PYTEST_VER >= (8 , 1 ):
46-
47- def pytest_collect_file (file_path , parent ):
48- config = parent .config
49- if (
50- config .option .black
51- and file_path .suffix in (".py" , ".pyi" )
52- and file_path .name != "conftest.py"
53- ):
54- return BlackFile .from_parent (parent , path = file_path )
55-
56- elif PYTEST_VER >= (7 , 0 ):
57-
58- def pytest_collect_file (file_path , path , parent ): # type: ignore[misc] # noqa: ARG001
59- # `path` must match the pytest 7.x hookspec; use file_path (pathlib.Path) in body.
60- config = parent .config
61- if (
62- config .option .black
63- and file_path .suffix in (".py" , ".pyi" )
64- and file_path .name != "conftest.py"
65- ):
66- return BlackFile .from_parent (parent , path = file_path )
40+ def pytest_collect_file (file_path , parent ):
41+ config = parent .config
42+ if (
43+ config .option .black
44+ and file_path .suffix in (".py" , ".pyi" )
45+ and file_path .name != "conftest.py"
46+ ):
47+ return BlackFile .from_parent (parent , path = file_path )
6748
6849
6950class BlackFile (pytest .File ):
@@ -77,9 +58,9 @@ def __init__(self, **kwargs):
7758 super ().__init__ (** kwargs )
7859 self .add_marker ("black" )
7960 try :
80- if tomllib is not None :
61+ if tomli is not None :
8162 with open ("pyproject.toml" , "rb" ) as toml_file :
82- settings = tomllib .load (toml_file )["tool" ]["black" ]
63+ settings = tomli .load (toml_file )["tool" ]["black" ]
8364 if "include" in settings :
8465 settings ["include" ] = self ._re_fix_verbose (settings ["include" ])
8566 if "exclude" in settings :
0 commit comments