@@ -373,6 +373,54 @@ def test_from_yaml(self):
373373 assert bx .key1 == "value1"
374374 assert bx .Key_2 == Box ()
375375
376+ def test_from_yaml_box_dots_dotted_key_before_non_dotted (self ):
377+ """
378+ Test for issue #305: Dotted keys read from YAML are lost with box_dots / default_box
379+ when the dotted key appears before a non-dotted key with the same prefix.
380+
381+ This test documents the bug where a.b: c followed by a: {d: e} causes a.b to be lost.
382+ """
383+ # Create a temporary YAML file with dotted key before non-dotted key
384+ yaml_content = """a.b: c
385+ a:
386+ d: e
387+ """
388+ yaml_file = Path (tmp_dir , "test_dotted_before.yml" )
389+ yaml_file .write_text (yaml_content )
390+
391+ # Test without box_dots/default_box - should preserve both keys as separate entries
392+ bx1 = Box .from_yaml (filename = yaml_file )
393+ assert "a.b" in bx1
394+ assert bx1 ["a.b" ] == "c"
395+ assert "a" in bx1
396+ assert bx1 ["a" ]["d" ] == "e"
397+
398+ # Test with box_dots=True and default_box=True
399+ # Expected behavior: a.b should be merged into a.b, so a.b == "c" and a.d == "e"
400+ # Current bug: a.b is lost when it appears before a:
401+ bx2 = Box .from_yaml (filename = yaml_file , box_dots = True , default_box = True )
402+ assert "a" in bx2
403+ assert bx2 ["a" ]["d" ] == "e"
404+ # This assertion will fail until the bug is fixed:
405+ # The dotted key a.b should be accessible as either a["b"] or via dotted notation
406+ assert "b" in bx2 ["a" ], "Bug #305: a.b key is lost when it appears before a: in YAML"
407+ assert bx2 ["a" ]["b" ] == "c"
408+
409+ # Test reverse order (non-dotted before dotted) - this should work correctly
410+ yaml_content_reverse = """a:
411+ d: e
412+ a.b: c
413+ """
414+ yaml_file_reverse = Path (tmp_dir , "test_dotted_after.yml" )
415+ yaml_file_reverse .write_text (yaml_content_reverse )
416+
417+ bx3 = Box .from_yaml (filename = yaml_file_reverse , box_dots = True , default_box = True )
418+ assert "a" in bx3
419+ assert bx3 ["a" ]["d" ] == "e"
420+ # In reverse order, the dotted key should be properly merged
421+ assert "b" in bx3 ["a" ]
422+ assert bx3 ["a" ]["b" ] == "c"
423+
376424 def test_bad_from_json (self ):
377425 with pytest .raises (BoxError ):
378426 Box .from_json ()
0 commit comments