8000 Fix mypy error: cast_unicode annotation exposes None passed from tbtools · ipython/ipython@09f3f74 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09f3f74

Browse files
committed
Fix mypy error: cast_unicode annotation exposes None passed from tbtools
cast_unicode was annotated as accepting str | bytes, but tbtools.py _tokens_filename passes file: str | None to it without a None guard. This would crash at runtime if file is None since cast_unicode passes the value through to compress_user which calls .startswith() on it. Fix: add `file or ""` guard at the tbtools.py call site, which is the correct behaviour (treat a missing filename as an empty string). The annotation cast_unicode(s: str | bytes) -> str is correct. https://claude.ai/code/session_01L2i6WEqHEX3HyCMWgimmEp
1 parent b580127 commit 09f3f74

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

IPython/core/tbtools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _tokens_filename(
203203
]
204204
else:
205205
name = util_path.compress_user(
206-
py3compat.cast_unicode(file, util_path.fs_encoding)
206+
py3compat.cast_unicode(file or "", util_path.fs_encoding)
207207
)
208208
if lineno is None:
209209
return [

IPython/utils/py3compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def encode(u: str, encoding: Optional[str]=None) -> bytes:
2121
return u.encode(encoding, "replace")
2222

2323

24-
def cast_unicode(s: str, encoding: Optional[str]=None) -> str:
24+
def cast_unicode(s: str | bytes, encoding: Optional[str]=None) -> str:
2525
if isinstance(s, bytes):
2626
return decode(s, encoding)
2727
return s

0 commit comments

Comments
 (0)
0