@@ -381,6 +381,10 @@ def install_requirements_for_venv(venv_info: VenvInfo, args: TestConfig, externa
381
381
382
382
383
383
def setup_virtual_environments (distributions : dict [str , PackageDependencies ], args : TestConfig , tempdir : Path ) -> None :
384
+ """Logic necessary for testing stubs with non-types dependencies in isolated environments."""
385
+ # STAGE 1: Determine which (if any) stubs packages require virtual environments.
386
+ # Group stubs packages according to their external-requirements sets
387
+
384
388
# We don't actually need pip if there aren't any external dependencies
385
389
no_external_dependencies_venv = VenvInfo (pip_exe = "" , python_exe = sys .executable )
386
390
external_requirements_to_distributions : defaultdict [frozenset [str ], list [str ]] = defaultdict (list )
@@ -394,11 +398,13 @@ def setup_virtual_environments(distributions: dict[str, PackageDependencies], ar
394
398
else :
395
399
_DISTRIBUTION_TO_VENV_MAPPING [distribution_name ] = no_external_dependencies_venv
396
400
401
+ # Exit early if there are no stubs packages that have non-types dependencies
397
402
if num_pkgs_with_external_reqs == 0 :
398
403
if args .verbose :
399
404
print (colored ("No additional venvs are required to be set up" , "blue" ))
400
405
return
401
406
407
+ # STAGE 2: Setup a virtual environment for each unique set of external requirements
402
408
requirements_sets_to_venvs : dict [frozenset [str ], VenvInfo ] = {}
403
409
404
410
if args .verbose :
@@ -409,7 +415,8 @@ def setup_virtual_environments(distributions: dict[str, PackageDependencies], ar
409
415
f"distribution{ 's' if num_pkgs_with_external_reqs != 1 else '' } ... "
410
416
)
411
417
print (colored (msg , "blue" ), end = "" , flush = True )
412
- venv_start_time = time .perf_counter ()
418
+
419
+ venv_start_time = time .perf_counter ()
413
420
414
421
with concurrent .futures .ThreadPoolExecutor () as executor :
415
422
venv_info_futures = [
@@ -420,10 +427,14 @@ def setup_virtual_environments(distributions: dict[str, PackageDependencies], ar
420
427
requirements_set , venv_info = venv_info_future .result ()
421
428
requirements_sets_to_venvs [requirements_set ] = venv_info
422
429
430
+ venv_elapsed_time = time .perf_counter () - venv_start_time
431
+
423
432
if args .verbose :
424
- venv_elapsed_time = time .perf_counter () - venv_start_time
425
433
print (colored (f"took { venv_elapsed_time :.2f} seconds" , "blue" ))
426
- pip_start_time = time .perf_counter ()
434
+
435
+ # STAGE 3: For each {virtual_environment: requirements_set} pairing,
436
+ # `pip install` the requirements set into the virtual environment
437
+ pip_start_time = time .perf_counter ()
427
438
428
439
# Limit workers to 10 at a time, since this makes network requests
429
440
with concurrent .futures .ThreadPoolExecutor (max_workers = 10 ) as executor :
@@ -433,11 +444,14 @@ def setup_virtual_environments(distributions: dict[str, PackageDependencies], ar
433
444
]
434
445
concurrent .futures .wait (pip_install_futures )
435
446
447
+ pip_elapsed_time = time .perf_counter () - pip_start_time
448
+
436
449
if args .verbose :
437
- pip_elapsed_time = time .perf_counter () - pip_start_time
438
450
msg = f"Combined time for installing requirements across all venvs: { pip_elapsed_time :.2f} seconds"
439
451
print (colored (msg , "blue" ))
440
452
453
+ # STAGE 4: Populate the _DISTRIBUTION_TO_VENV_MAPPING
454
+ # so that we have a simple {distribution: venv_to_use} mapping to use for the rest of the test.
441
455
for requirements_set , distribution_list in external_requirements_to_distributions .items ():
442
456
venv_to_use = requirements_sets_to_venvs [requirements_set ]
443
457
_DISTRIBUTION_TO_VENV_MAPPING .update (dict .fromkeys (distribution_list , venv_to_use ))
@@ -462,6 +476,9 @@ def test_third_party_stubs(code: int, args: TestConfig, tempdir: Path) -> TestRe
462
476
):
463
477
distributions_to_check [distribution ] = get_recursive_requirements (distribution )
464
478
479
+ # If it's the first time test_third_party_stubs() has been called during this session,
480
+ # setup the necessary virtual environments for testing the third-party stubs.
481
+ # It should only be necessary to call setup_virtual_environments() once per session.
465
482
if not _DISTRIBUTION_TO_VENV_MAPPING :
466
483
setup_virtual_environments (distributions_to_check , args , tempdir )
467
484
0 commit comments