Skip to content

root property should return None on empty documents instead of raising QueryError #23

@nathanjmcdougall

Description

@nathanjmcdougall

Problem

Currently, accessing doc.root on a document with no YAML value (empty string, comment-only, or --- only) raises QueryError:

`python
import yamltrip

doc = yamltrip.loads('')
doc.root # raises QueryError: 'Path not found'

doc = yamltrip.loads('# just a comment\n')
doc.root # raises QueryError: 'Path not found'

doc = yamltrip.loads('---\n')
doc.root # raises QueryError: 'Path not found'
`

This forces every caller to wrap doc.root in a try/except, even though an empty document is a normal, expected state (e.g. a freshly created config file).

Proposal

Return None when the document has no root value node, consistent with how yamltrip.loads('null\n').root already returns None:

python @property def root(self) -> Any: try: return self[()] except QueryError: return None

Notes

  • () in doc already returns False for empty docs, so the semantics are consistent.
  • This is a Python-only change — no Rust modifications needed.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions