Skip to content

Raise a clear TypeError for non-str/non-bytes input to slugify()#183

Open
rantkiglorelgmx-lab wants to merge 2 commits into
un33k:masterfrom
rantkiglorelgmx-lab:fix/non-str-input-type-error
Open

Raise a clear TypeError for non-str/non-bytes input to slugify()#183
rantkiglorelgmx-lab wants to merge 2 commits into
un33k:masterfrom
rantkiglorelgmx-lab:fix/non-str-input-type-error

Conversation

@rantkiglorelgmx-lab

Copy link
Copy Markdown

What

slugify() currently fails with an opaque error for input that is neither str nor bytes (e.g. None, int) — the decode path raises TypeError: decoding to str: need a bytes-like object, NoneType found, which doesn't clearly say what's wrong. This adds an explicit, clear guard at the top of the function:

if not isinstance(text, (str, bytes)):
    raise TypeError(f'text must be str or bytes, not {type(text).__name__}')

Backward compatibility

Add-only — every input that previously worked is unaffected. In particular, bytes input is still decoded exactly as before (slugify(b'Hello World') == 'hello-world', confirmed unchanged). Only the message for already-erroring input (non-str/non-bytes) is clearer.

Verification

  • New test asserts the clear message via assertRaisesRegex, and separately asserts bytes input still slugifies correctly (regression lock).
  • Non-vacuity: reverted the source fix only — the new test fails on the old code (message mismatch), confirming it isn't a vacuous test.
  • Full suite green: 83 passed.

Co-authored by Claude (Anthropic) as part of an automated hardening pass; independently re-verified (non-vacuity, backward-compatibility, coverage) before submission.

hermes-canonical added 2 commits June 30, 2026 21:33
(cherry picked from commit 5804929c5a5d5fd07b3fd5d81af45c293fd0db54)
@jquast

jquast commented Jul 4, 2026

Copy link
Copy Markdown

But the function is typed already and would be caught by type checkers.

If a developer (or AI bot) is having trouble with "opaque errors" because of using the wrong type, then they could use a type checker themselves, instead of depending on all upstream dependencies to add if isinstance() checks to every function, this could go on forever, billions of lines of code to add to all public api functions of all popular python projects that negatively impact performance of all users for the very small benefit of a few confused developers.

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.

2 participants