Skip to content

fix: raise a clear TypeError when convert() is given a non-str - #274

Open
Thorsrud22 wants to merge 1 commit into
pycontribs:mainfrom
Thorsrud22:reject-non-str-input
Open

Thorsrud22 wants to merge 1 commit into
pycontribs:mainfrom
Thorsrud22:reject-non-str-input

Conversation

@Thorsrud22

@Thorsrud22 Thorsrud22 commented Sep 11, 2026

Copy link
Copy Markdown

Fixes #242.

Passing a list of lines to Ansi2HTMLConverter.convert() currently fails deep inside the regex pass with AttributeError: 'list' object has no attribute 'replace', which says nothing about what went wrong. Two people reported hitting it, and the reporter's own workaround ("".join(ansi)) shows what the input actually was.

This adds a single isinstance check at the top of prepare() (the common path for both convert() and prepare()) and raises:

TypeError: ansi must be a str, not list; join an iterable of lines first, e.g. ''.join(lines)

Behaviour for str input is unchanged. A regression test is included.

Verified locally: all 42 tests pass, and the isort, black, flake8, pylint and mypy pre-commit hooks pass on the changed files.

@Thorsrud22 Thorsrud22 changed the title Raise a clear TypeError when convert() is given a non-str fix: raise a clear TypeError when convert() is given a non-str Sep 11, 2026
@github-actions github-actions Bot added the fix This issue/PR relates to a bug. label Sep 11, 2026
Passing a list of lines to Ansi2HTMLConverter.convert() (or prepare())
used to fail deep inside the regex pass with
"AttributeError: 'list' object has no attribute 'replace'", which
gives no hint about what was wrong or how to fix it. Two users reported
hitting this (pycontribs#242).

Check the input type once at the entry point and raise a TypeError
that names the actual type and suggests joining the lines first.

Fixes pycontribs#242
@Thorsrud22
Thorsrud22 force-pushed the reject-non-str-input branch 2 times, most recently from 688afb6 to b2f8930 Compare September 16, 2026 18:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix This issue/PR relates to a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'list' object has no attribute 'replace'

1 participant