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