Skip to content

Commit aad61ac

Browse files
committed
Remove colorless theme monkeypatchfor argparse bug in 3.15.0b1
3.15.0b2 is out and available now in both uv and GitHub Actions.
1 parent 31d0461 commit aad61ac

2 files changed

Lines changed: 0 additions & 43 deletions

File tree

cmd2/argparse_utils.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -423,30 +423,6 @@ def _validate_completion_callable(self: argparse.Action, value: Any) -> Any:
423423
register_argparse_argument_parameter("nargs_range")
424424
register_argparse_argument_parameter("suppress_tab_hint")
425425

426-
############################################################################################################
427-
# Workaround for Python 3.15.0b1 argparse bug
428-
# _ColorlessTheme.__getattr__ incorrectly returns "" for non-public attributes, which breaks
429-
# protocols like copy.deepcopy().
430-
############################################################################################################
431-
432-
if sys.version_info >= (3, 15):
433-
434-
def _ColorlessTheme_getattr( # noqa: N802
435-
_self: argparse._ColorlessTheme, # type: ignore[name-defined]
436-
name: str,
437-
) -> Any:
438-
"""Patched __getattr__ that allows non-public lookups to fail correctly.
439-
440-
This matches the implementation in CPython for their next release.
441-
"""
442-
if name.startswith("_"):
443-
raise AttributeError(name)
444-
return ""
445-
446-
# If the bug still exists, then install the patch.
447-
if getattr(argparse._ColorlessTheme(), "__deepcopy__", None) == "": # type: ignore[attr-defined]
448-
argparse._ColorlessTheme.__getattr__ = _ColorlessTheme_getattr # type: ignore[attr-defined]
449-
450426

451427
############################################################################################################
452428
# Patch _ActionsContainer.add_argument to support more arguments

tests/test_argparse_utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -863,22 +863,3 @@ def test_deprecated_subcommand() -> None:
863863
# Verify it was removed from _deprecated set
864864
assert "old" not in subparsers_action._deprecated # type: ignore[attr-defined]
865865
assert "old_alias" not in subparsers_action._deprecated # type: ignore[attr-defined]
866-
867-
868-
@pytest.mark.skipif(
869-
sys.version_info < (3, 15),
870-
reason="_ColorlessTheme only exists in 3.15+",
871-
)
872-
def test_colorless_theme_monkeypatch() -> None:
873-
"""Test the _ColorlessTheme.__getattr__ monkey patch."""
874-
875-
# If this assertion fails, then the bug no longer exists and our patch wasn't installed.
876-
# We can remove the patch function and this test.
877-
assert argparse._ColorlessTheme.__getattr__ == argparse_utils._ColorlessTheme_getattr
878-
879-
# Our patch raises an Attribute error for non-public.
880-
with pytest.raises(AttributeError):
881-
getattr(argparse._ColorlessTheme(), "_fake") # noqa: B009
882-
883-
with pytest.raises(AttributeError):
884-
getattr(argparse._ColorlessTheme(), "__deepcopy__") # noqa: B009

0 commit comments

Comments
 (0)