Skip to content

fix(sdk): coerce bytearray to bytes in process_request payload#1081

Merged
danh91 merged 1 commit into
mainfrom
danh91/sdk-mypy2-bytes-fix
May 9, 2026
Merged

fix(sdk): coerce bytearray to bytes in process_request payload#1081
danh91 merged 1 commit into
mainfrom
danh91/sdk-mypy2-bytes-fix

Conversation

@danh91
Copy link
Copy Markdown
Member

@danh91 danh91 commented May 9, 2026

Bug

sdk-tests CI job is failing on every new PR after mypy 2.0 was picked up by the runner:

```
modules/sdk/karrio/core/utils/helpers.py:177: error: Dict entry 0 has incompatible type "str": "bytes | bytearray"; expected "str": "bytes" [dict-item]
```

Root Cause

payload is annotated dict[str, bytes] (line 171), but the isinstance(raw, (bytes, bytearray)) branch assigns the narrowed bytes | bytearray:

```python
payload: dict[str, bytes] = {}
...
elif isinstance(raw, (bytes, bytearray)):
payload = dict(data=raw) # bytearray is not bytes
```

mypy 1.x accepted this; mypy 2.0 (now installed by bin/setup-sdk-env) does not.

Fix

One-line coercion to satisfy the declared type:

```python
payload = dict(data=bytes(raw))
```

bytes(bytes_instance) is a cheap no-op-equivalent and bytes(bytearray) produces equivalent bytes. Downstream urllib.request.Request requires bytes-like data anyway, so behavior is unchanged.

Tests

  • Type fix verified against the exact mypy 2.0 error reported in CI logs (sdk-tests (3.12) job 75188611968).
  • No behavior change for any caller path (bytes / bytearray / str / dict / fallback).

Why a separate PR

Spotted while CI was failing on an unrelated docs PR (#1080). Splitting this out so it can land independently and unblock all open PRs.

mypy 2.0 (newly installed in CI) flagged that the bytes|bytearray branch
assigned a bytearray into a payload typed dict[str, bytes]. Coerce with
bytes(raw) — type-correct, and downstream urllib.request.Request wants
bytes anyway.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
karrio-web Ready Ready Preview, Comment May 9, 2026 11:04pm

Request Review

@danh91 danh91 merged commit 8211f50 into main May 9, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant