Skip to content

Commit

Permalink
Simplify type union error workaround
Browse files Browse the repository at this point in the history
`@responses.activate` is intended to decorate top-level test case functions, not functions passed to other functions and typechecked (in this case, functions passed to `multiprocessing.Pool.map`). Instead, use responses' context manager.
  • Loading branch information
john-kurkowski committed Nov 5, 2023
1 parent a0570ee commit 1e8d3e9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions tests/test_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os.path
from multiprocessing import Pool
from pathlib import Path
from typing import Callable, cast

import pytest
import responses
Expand All @@ -17,20 +16,19 @@ def test_multiprocessing_makes_one_request(tmp_path: Path) -> None:
"""Ensure there aren't duplicate download requests."""
process_count = 3
with Pool(processes=process_count) as pool:
http_request_counts = pool.map(
cast(Callable[[Path], int], _run_extractor), [tmp_path] * process_count
)
http_request_counts = pool.map(_run_extractor, [tmp_path] * process_count)
assert sum(http_request_counts) == 1


@responses.activate
def _run_extractor(cache_dir: Path) -> int:
"""Run the extractor."""
responses.add(responses.GET, PUBLIC_SUFFIX_LIST_URLS[0], status=208, body="uk.co")
extract = TLDExtract(cache_dir=str(cache_dir))
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(responses.GET, PUBLIC_SUFFIX_LIST_URLS[0], status=208, body="uk.co")
extract = TLDExtract(cache_dir=str(cache_dir))

extract("bar.uk.com", include_psl_private_domains=True)
return len(responses.calls)
extract("bar.uk.com", include_psl_private_domains=True)
num_calls = len(rsps.calls)
return num_calls


@responses.activate
Expand Down

0 comments on commit 1e8d3e9

Please sign in to comment.