Skip to content

Commit 99793d9

Browse files
committed
Add suggestions for pyright changes from review
Based on suggestions from @victorlin in review of #530 1. Assert value instead of type to be consistent with other assertions in the repo. 2. Use type narrowing instead of coercion 3. nextstrain_dot_org/download: Always write raw bytes `iter_content` can potentially return bytes if the `response.encoding` is None,¹ so just always write raw bytes. ¹ <https://github.com/psf/requests/blob/6404f345e562d962abe6700a1c357ec1e7e18232/src/requests/utils.py#L599-L600>
1 parent 8cec798 commit 99793d9

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

nextstrain/cli/pathogens.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,11 @@ def setup(self, dry_run: bool = False, force: bool = False) -> SetupStatus:
454454
""") from err
455455

456456
except requests.exceptions.HTTPError as err:
457-
assert type(err.response) is requests.Response
457+
assert err.response is not None
458458
if 400 <= err.response.status_code <= 499:
459+
auth = err.response.request.headers["Authorization"]
459460
if (err.response.status_code in {401, 403}
460-
and (auth := str(err.response.request.headers["Authorization"]))
461+
and isinstance(auth, str)
461462
and auth.startswith("Basic ")):
462463
user = b64decode(auth.split(" ", 1)[1]).decode("utf-8").split(":", 1)[0]
463464

nextstrain/cli/remote/nextstrain_dot_org.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,8 @@ def download(url: URL, local_path: Path, recursively: bool = False, dry_run: boo
390390
continue
391391

392392
# Stream response data to local file
393-
with destination.open("w") as local_file:
394-
for chunk in response.iter_content(chunk_size = None, decode_unicode = True):
395-
assert type(chunk) is str
393+
with destination.open("wb") as local_file:
394+
for chunk in response.iter_content(chunk_size = None):
396395
local_file.write(chunk)
397396

398397

@@ -825,7 +824,7 @@ def raise_for_status(origin: Origin, response: requests.Response) -> None:
825824
response.raise_for_status()
826825

827826
except requests.exceptions.HTTPError as err:
828-
assert type(err.response) is requests.Response
827+
assert err.response is not None
829828
status = err.response.status_code
830829

831830
if status == 400:

0 commit comments

Comments
 (0)