Skip to content

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

Closed

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions