edgartools icon indicating copy to clipboard operation
edgartools copied to clipboard

Preserve `xsi:nil` state in parsed XBRL facts

Open synfonia-llc opened this issue 3 months ago • 3 comments

Feature Category

  • [x] New API functionality
  • [ ] Performance improvement
  • [ ] Developer experience improvement
  • [ ] Documentation enhancement
  • [ ] Tool/utility addition

Problem Statement

Is your feature request related to a problem? Please describe.

The parsed Fact/FactQuery surface does not expose whether a source fact was explicitly filed with xsi:nil="true". A nil fact and a present, non-nil element with empty lexical content both hydrate as value="", numeric_value=None, with no is_nil or nil key.

Current documentation warns that the parser model can be lossy when exact nil provenance matters, and I did not find a public contract promising nil-state preservation. This is therefore an additive feature request rather than a claim that the current documented object shape is broken.

Who would benefit from this feature?

  • [ ] Beginner Python users working with SEC filings
  • [x] Financial analysts and researchers
  • [x] Advanced developers building financial applications
  • [x] Data scientists working with financial datasets

Proposed Solution

Describe the solution you'd like

Add an explicit boolean or equivalent nil-state field to the parsed fact model and expose it consistently through FactsView, FactQuery.execute(), and DataFrame projections. A source element with xsi:nil="true" should be distinguishable from a non-nil element whose lexical value is empty.

Describe alternatives you've considered

Callers can reparse the filed instance XML and join nodes back to facts by XML id. That preserves the source bit but duplicates parser work, requires source-document access, and is cumbersome for bulk workflows. Using an empty-string sentinel is not sufficient because the two source states already collide on that representation.

Use Case Example

How would you use this feature?

from pathlib import Path

from edgar.xbrl import XBRL

xbrl = XBRL.from_files(instance_file=Path("xbrl.xml"))
rows = (
    xbrl.query()
    .by_concept("us-gaap:CommitmentsAndContingencies", exact=True)
    .execute()
)

for row in rows:
    print(row["fact_id"], row["value"], row["is_nil"])

Implementation Considerations

Complexity Level:

  • [ ] Simple (minor API addition)
  • [x] Moderate (new functionality with existing patterns)
  • [ ] Complex (significant architectural changes)

Backwards Compatibility:

  • [x] This feature maintains backwards compatibility
  • [ ] This feature might break existing code (please explain below)
  • [ ] Unsure about compatibility impact

An additive optional field should preserve existing consumers. DataFrame schemas and serialization tests may need corresponding updates.

Additional Context

Verified environment: EdgarTools import version 5.55.0, Python 3.14.4, Microsoft Windows 11, production edgar/** from main at a0ff18089c844ddfe81bd21a1c7c56f4ed629e55; audit worktree HEAD 70c31469dc7e6d4b386a6f8fe4ce1276d48fca51. The reused editable virtual environment has stale distribution metadata reporting 5.54.0.

Zymeworks accession 0001937653-26-000015 contains two explicit nil facts:

<us-gaap:CommitmentsAndContingencies
    contextRef="c-4" id="f-137" unitRef="usd" xsi:nil="true"/>
<us-gaap:CommitmentsAndContingencies
    contextRef="c-5" id="f-138" unitRef="usd" xsi:nil="true"/>

Both currently return value="", numeric_value=None, no is_nil key, and no nil key. Zevra accession 0001437749-24-026474 contains a non-nil, self-closing ExtensibleList fact (id="c116236184") and exposes the same value-level shape. This empirically demonstrates the collision without assuming a current nil-preservation contract.

XBRL 2.1 section 5.1.1 permits nil-value items, and calculation semantics treat them differently from contributing numeric facts. InstanceParser._extract_facts() currently derives value from text and does not retain the XML Schema-instance nil attribute; the Fact model has no nil-state field.

This is distinct from Draft 69, which concerns extensible-enumeration metadata and resolved values. The ZVRA fact is only a non-nil empty control here.

A live search of the open and closed GitHub issue tracker is still required immediately before submission.


Feature requests are evaluated based on EdgarTools' core principles: Simple yet powerful, accurate financials, beginner-friendly, and joyful UX.

synfonia-llc avatar Sep 01 '26 02:09 synfonia-llc

The xsi:nil distinction matters more than it looks. A fact that's absent and a fact that's explicitly filed as nil are different pieces of information, and once they collapse into the same None, you can't recover the difference.

Three places it usually breaks:

1. Extraction. If the parser does element.text and gets None, it treats the fact as missing. But xsi:nil="true" means the filer reported the fact as nil — deliberately. Check the attribute explicitly:

nil_attr = element.attrib.get(
    "{http://www.w3.org/2001/XMLSchema-instance}nil"
)
is_nil = nil_attr == "true"

2. Merging across filings. When you dedupe facts by concept + context, a nil from a newer filing should override a non-nil from an older one. Otherwise you carry stale values forward and report a number the filer already retracted.

3. Downstream. Consumers need to distinguish "filed as nil" from "not filed". If both render as empty, you lose information that some filings use to signal a deliberate zero or a not-applicable.

A tri-state (value / nil / absent) preserves the distinction without changing the public API in a breaking way — a new optional property on the fact object, defaulting to absent for backward compatibility.

Happy to share how I model this in my own parser if useful.

DOMCHURCH avatar Sep 12 '26 22:09 DOMCHURCH

@dgunning, here is our current thinking on the root cause, proposed behavior, and coordination with other issues.

Root Cause:

Parsed facts and query output expose no nil marker, making an explicit nil item indistinguishable from empty non-nil content. The retained Apple and Zevra sources establish those distinct states; neither establishes zero or a reason for missing information.

Proposed Behavior:

Add Fact.is_nil, read the namespace-qualified source attribute, and carry the flag through source-fact queries, exports and DataFrame projections. Keep nil distinct from empty content, an absent fact and numeric zero while preserving existing instance value parsing. Include the flag in deduplication so a nil/empty pair survives even when the final projection omits it. Document the added output field and intentional extra row for those pairs; older serialized inputs cannot recover discarded source information.

Coordination:

Keep a separate source-nil PR before consumers such as #1751, coordinating parser integration with fraction PR #1803. Share source-marker meaning with inline #1757 while keeping its nil-before-conversion correction separate; statement selection and typed nil remain separate, and #1750’s landed ranking repair needs no follow-on.

Below, please find the full design document. Please let me know your thoughts.

Brian

Attachment: design-public.md, Preserve explicit nil state in parsed XBRL facts — #1254.

AI assistance: Codex; gpt-6.1-sol (High) reproduced the behavior, checked raw facts and wrote this comment; gpt-6-astra (Max) prepared the architecture; separate gpt-6-astra (Max) agents provided adversarial review and dedicated Financial Statement Analyst review of source fidelity and financial meaning.

synfonia-llc avatar Oct 27 '26 11:10 synfonia-llc

Thanks Brian, I read the design doc and I'm happy with it as written: is_nil: bool = False on Fact, read by namespace rather than prefix, no meaning attached beyond "the filer marked it nil", and carried through the fact-level query and DataFrame outputs but not the statement frames. Keeping a nil/empty pair as two rows is the right call, as long as the upgrade note spells it out like you suggest. The Apple FY2023 fixture (f-194/f-195/f-403, with f-397 as the control) is good ground truth, since it needs no new cassette.

Two small things:

  1. #1803 has landed since you wrote this, and it already added _is_nil() in edgar/xbrl/parsers/instance.py. I reused it rather than adding a second helper. The only difference from your spec is that it lowercases, so TRUE counts as nil. I'm fine with being lenient there, and I'd rather have one rule than two that disagree. #1757 will use the same rule when we get to the inline path.
  2. Since is_nil goes in as the last core column, the dimension columns move one place to the right, so I checked for anything reading the fact frames by column position. Nothing does: every picker selects by name or by the dim_ prefix.

On the earlier comment about letting a newer nil override an older value: I agree with your doc that it's out of scope. Nil says the filer reported no value; it doesn't say an earlier number was wrong, and it isn't zero.

I've picked this one up following your doc, with the two changes above: #1917. #1757 will come after it and use the same helper.

dgunning avatar Nov 11 '26 13:11 dgunning