Skip to content

fix: case-insensitive ConfigBox key lookup for __getitem__, __contains__, __delitem__, __setitem__#314

Open
gaoflow wants to merge 1 commit into
cdgriffith:masterfrom
gaoflow:fix-configbox-case-insensitive
Open

fix: case-insensitive ConfigBox key lookup for __getitem__, __contains__, __delitem__, __setitem__#314
gaoflow wants to merge 1 commit into
cdgriffith:masterfrom
gaoflow:fix-configbox-case-insensitive

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

ConfigBox advertises "loosey goosey" case-insensitive key access in its docstring,
but:

  • __getitem__ (dict-style cb['host']) had zero case-insensitive support — only exact match
  • __contains__ ('host' in cb) was not overridden — False for differently-cased keys
  • __delitem__ and __setitem__ likewise had no case-insensitive support
  • __getattr__ only retried with .lower(), missing MixedCase→lowercase resolution

Fix: Added _case_insensitive_key() static helper that scans actual keys
case-insensitively, then overrode the five dunder methods to use it. Each
method tries exact match first (zero overhead for exact-case), then falls back
to case-insensitive scan.

Reproduction:

from box import ConfigBox
cb = ConfigBox({'HOST': 'localhost', 'ServerPort': 8080})
cb['host']          # BoxKeyError — should return 'localhost'
'serverport' in cb  # False — should be True
del cb['HOST']      # BoxKeyError — should succeed

154/154 tests pass (5 toon-related failures pre-existing, missing toon_format).

This pull request was prepared with the assistance of AI, under my
direction and review.

…s__, __delitem__, __setitem__

ConfigBox docstring advertises "loosey goosey" case-insensitive access but
__getitem__ (dict-style), __contains__, __delitem__ and __setitem__ had
no case-insensitive support. __getattr__ only retried with lowered input,
missing MixedCase→lowercase resolution.

Add _case_insensitive_key() helper and override the four dunder methods
to resolve keys case-insensitively. __getattr__ now uses the same key-scan
fallback instead of the broken .lower() retry.
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