Skip to content

Remove artificial defaults from text nodes - #1240

Merged
tefra merged 1 commit into
mainfrom
text-node-fallback
Oct 16, 2026
Merged

tefra merged 1 commit into
mainfrom
text-node-fallback

Conversation

@tefra

@tefra tefra commented Oct 16, 2026

Copy link
Copy Markdown
Owner

📒 Description

The generator used to give every str/bytes text field an artificial default="". That produced odd models such as a pattern restriction whose default doesn't match the pattern:

value: str = field(
    default="",
    metadata={
        "pattern": r"0x[0-9a-z]*",
    }
)

Text fields are now generated without a default, and the parsers bind the empty value instead. This replaces #1210, which made the same change in a single commit and had side effects in the parsers.

Breaking change: generated simple-content classes now need value passed in, e.g. Comment() becomes Comment(value="").

🔗 What I've Done

  • ParserUtils.accepts_empty_text: missing text binds as ""/b"" only when the text field is required, has no default and is str/bytes. Every other field keeps its default or stays unset, as before.
  • ElementNode.bind_text and DictDecoder.bind_dataclass use that rule. The dict decoder writes to its params and leaves the input dict untouched.
  • SanitizeAttributesDefaultValue no longer sets default="" on text nodes. The fixtures were regenerated.
  • Removed the unused default and tokens_factory params from ParserUtils.parse_var.

💬 Comments

Side effects of #1210 that this version avoids:

  • an empty element with an int/date/enum text field no longer gets a ConverterWarning and a raw "";
  • callable (e.g. enum) defaults are kept;
  • fixed init=False text is not validated against "";
  • the element tail still reaches a wildcard field;
  • a null text value in the dict decoder still falls back to the field default.

Regression suites, run locally with dataclasses output:

  • samples: all suites pass, and the XML comparisons and schema validations are unchanged.
  • W3C: build 14687 passed, xsd 14600 passed. The skip list is identical to main (87).
  • The only output changes are the removed default=""/default=b"" lines, and the pycode dumps now writing value='' explicitly.

🛫 Checklist

Generated str/bytes text fields no longer get default="". The parsers
bind an empty value instead when the text is missing, but only for
required str/bytes text fields without a default, so other fields keep
their default or stay unset.

Drop the unused default and tokens_factory params from
ParserUtils.parse_var.
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.97%. Comparing base (cf7afe8) to head (7f0a2e5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1240   +/-   ##
=======================================
  Coverage   99.97%   99.97%           
=======================================
  Files         117      117           
  Lines        9414     9421    +7     
  Branches     1447     1448    +1     
=======================================
+ Hits         9412     9419    +7     
  Misses          1        1           
  Partials        1        1           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tefra
tefra merged commit 4fae5cc into main Oct 16, 2026
14 checks passed
@tefra
tefra deleted the text-node-fallback branch October 16, 2026 11:19
tefra added a commit to tefra/xsdata-w3c-tests that referenced this pull request Oct 16, 2026
Generated text fields no longer get default="", so models drop the
default and the pycode dumps write value='' explicitly.

Depends on tefra/xsdata#1240
tefra added a commit to tefra/xsdata-samples that referenced this pull request Oct 16, 2026
Generated text fields no longer get default="", so models drop the
default and the pycode dumps write value='' explicitly.

Depends on tefra/xsdata#1240
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