Skip to content

Commit c41da72

Browse files
authored
refactor: Decouple discovery from creator plugins (#2949)
1 parent 02ae86f commit c41da72

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

docs/changelog/2074c.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Decouple discovery from creator plugins - by :user:`esafak`.

src/virtualenv/discovery/py_info.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _get_path_extensions():
3030
_CONF_VAR_RE = re.compile(r"\{\w+\}")
3131

3232

33-
class PythonInfo: # noqa: PLR0904
33+
class PythonInfo:
3434
"""Contains information for a Python interpreter."""
3535

3636
def __init__(self) -> None: # noqa: PLR0915
@@ -135,7 +135,6 @@ def abs_path(v):
135135
self.system_stdlib = self.sysconfig_path("stdlib", confs)
136136
self.system_stdlib_platform = self.sysconfig_path("platstdlib", confs)
137137
self.max_size = getattr(sys, "maxsize", getattr(sys, "maxint", None))
138-
self._creators = None
139138

140139
@staticmethod
141140
def _get_tcl_tk_libs():
@@ -311,13 +310,6 @@ def sysconfig_path(self, key, config_var=None, sep=os.sep):
311310
config_var = base
312311
return pattern.format(**config_var).replace("/", sep)
313312

314-
def creators(self, refresh=False): # noqa: FBT002
315-
if self._creators is None or refresh is True:
316-
from virtualenv.run.plugin.creators import CreatorSelector # noqa: PLC0415
317-
318-
self._creators = CreatorSelector.for_interpreter(self)
319-
return self._creators
320-
321313
@property
322314
def system_include(self):
323315
path = self.sysconfig_path(
@@ -467,8 +459,7 @@ def _to_json(self):
467459
return json.dumps(self._to_dict(), indent=2)
468460

469461
def _to_dict(self):
470-
data = {var: (getattr(self, var) if var != "_creators" else None) for var in vars(self)}
471-
462+
data = {var: getattr(self, var) for var in vars(self)}
472463
data["version_info"] = data["version_info"]._asdict() # namedtuple to dictionary
473464
return data
474465

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from virtualenv.discovery.py_info import PythonInfo
1616
from virtualenv.info import IS_GRAALPY, IS_PYPY, IS_WIN, fs_supports_symlink
1717
from virtualenv.report import LOGGER
18+
from virtualenv.run.plugin.creators import CreatorSelector
1819

1920

2021
def pytest_addoption(parser):
@@ -308,7 +309,7 @@ def special_name_dir(tmp_path, special_char_name):
308309

309310
@pytest.fixture(scope="session")
310311
def current_creators(session_app_data):
311-
return PythonInfo.current_system(session_app_data).creators()
312+
return CreatorSelector.for_interpreter(PythonInfo.current_system(session_app_data))
312313

313314

314315
@pytest.fixture(scope="session")

tests/unit/create/test_creator.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from virtualenv.discovery.py_info import PythonInfo
3232
from virtualenv.info import IS_PYPY, IS_WIN, fs_is_case_sensitive
3333
from virtualenv.run import cli_run, session_via_cli
34+
from virtualenv.run.plugin.creators import CreatorSelector
3435

3536
CURRENT = PythonInfo.current_system()
3637

@@ -93,9 +94,9 @@ def system(session_app_data):
9394
return get_env_debug_info(Path(CURRENT.system_executable), DEBUG_SCRIPT, session_app_data, os.environ)
9495

9596

96-
CURRENT_CREATORS = [i for i in CURRENT.creators().key_to_class if i != "builtin"]
97+
CURRENT_CREATORS = [i for i in CreatorSelector.for_interpreter(CURRENT).key_to_class if i != "builtin"]
9798
CREATE_METHODS = []
98-
for k, v in CURRENT.creators().key_to_meta.items():
99+
for k, v in CreatorSelector.for_interpreter(CURRENT).key_to_meta.items():
99100
if k in CURRENT_CREATORS:
100101
if v.can_copy:
101102
if k == "venv" and CURRENT.implementation == "PyPy" and CURRENT.pypy_version_info >= [7, 3, 13]:
@@ -400,7 +401,10 @@ def test_create_long_path(tmp_path):
400401

401402

402403
@pytest.mark.slow
403-
@pytest.mark.parametrize("creator", sorted(set(PythonInfo.current_system().creators().key_to_class) - {"builtin"}))
404+
@pytest.mark.parametrize(
405+
"creator",
406+
sorted(set(CreatorSelector.for_interpreter(PythonInfo.current_system()).key_to_class) - {"builtin"}),
407+
)
404408
@pytest.mark.usefixtures("session_app_data")
405409
def test_create_distutils_cfg(creator, tmp_path, monkeypatch):
406410
result = cli_run(

tests/unit/create/via_global_ref/test_build_c_ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
from virtualenv.discovery.py_info import PythonInfo
1212
from virtualenv.run import cli_run
13+
from virtualenv.run.plugin.creators import CreatorSelector
1314

1415
CURRENT = PythonInfo.current_system()
15-
CREATOR_CLASSES = CURRENT.creators().key_to_class
16+
CREATOR_CLASSES = CreatorSelector.for_interpreter(CURRENT).key_to_class
1617

1718

1819
def builtin_shows_marker_missing():

0 commit comments

Comments
 (0)