pyxsd.stubs¶
Inventory of the generated classes a stub file must declare.
A compiled schema’s classes are created at runtime from schema
bookkeeping, not authored as Python names: a named type keeps its XSD
local name, but an anonymous inline type is called shipTo|complexType,
a strict-namespace compile registers one class under both a bare and a
Clark name, and two namespaces may declare the same local name. A stub
file needs one plain Python identifier per class.
The anti-goal is a stub that lies: this module never presents a stub
name as the schema’s own when it is not one. Every name that had to be
synthesized, sanitized or disambiguated is recorded in the entry’s
note (the real bookkeeping name, the namespace a colliding class
belongs to), and er points at the type declaration the class was
built for whenever the component table still has it — None
otherwise, never a guess. Nothing here calls schema.require_valid():
an error-bearing schema still compiles classes, and a stub may still be
requested for it.
On top of the inventory, _members resolves one class’s
renderable members: its own element and attribute descriptors in
declaration order, members attributed from a base class the inventory
does not list (an overlay’s generated base), and public functions and
properties an overlay adds beyond the generated descriptors. Element
occurrence is folded through the compiled particle tree, because a
single descriptor’s own minOccurs/maxOccurs lie whenever an
ancestor group is optional or repeated — a choice branch, for example,
is absent whenever another branch is taken. Every member carries the
annotation a stub renders and a comment recording what the annotation
cannot say (defaults, nillable, prohibited uses, unresolvable types).
Member annotations model valid parsed documents and degrade to Any
with a comment rather than naming a type the stub cannot import: a union
member (a policy degradation — a parsed union value is an instance of
the union’s own bookkeeping class, and the comment names the member
types its value space draws from), a recursive back edge (a type
re-entered while its own class resolves), and an unresolvable
declaration all become Any with the reason in the comment. A
reference to a substitution-group head keeps the head’s type, takes the
None arm (a member occurrence satisfies the particle without
filling the head accessor) and comments the member names the runtime
binds instead.
Constructs that reach the instance but have no member to hang on —
mixed content, wildcards, open content — become comment lines on the
class block. The member model is identical in both formatters;
compact records the requested mode, and the compact renderer
rewrites annotation spellings through its own alias table afterwards.
emit_stub() renders the model as the full .pyi document: a
provenance and consumption header, imports, one class block per
inventory entry (mirroring the XSD derivation the runtime mirrors, with
simple types as their datatype subclasses and facets as comments), and
the global elements as module-level aliases. compact=True renders
the agent view instead: a two-line header (provenance and alias
legend), one line per class and per member, facet-only simple types
dropped with their facets inlined onto the members that use them, and a
single trailing comment line listing the root elements. Rendering is
total in both modes: a schema that compiled always emits, and every
construct the stub cannot type truthfully becomes Any with a
comment instead of an exception.
Functions¶
|
Returns the stub text for the classes schema compiled. |
Module Contents¶
- pyxsd.stubs.emit_stub(schema, *, compact=False, component_ids=None, depth=None)¶
Returns the stub text for the classes schema compiled.
The text is generated from the live schema at call time (the class table, the declarations behind it, and the compiled particle model), so re-emitting after an overlay is loaded shows the overlay’s members — nothing is cached at compile time.
Full mode is an importable-quality
.pyidocument: a provenance and consumption header, imports, one class block per generated class, and a root-element alias section. Annotations name the real runtime classes, so a member typedIntreads back apyxsd.xsd_data_types.Int.compactselects the agent-oriented compact formatter, which shares this module’s member model: a two-line header (provenance and an alias legend mapping the short spellings back to the pyxsd classes), one line per class and per member, facet-only simple types folded onto the members that use them, and a trailing comment line listing the root elements. The compact text is a description, not importable Python; every annotation spelling it uses is a rendered class name, a pyxsd builtin’s own name, or a legend alias. Degradation comments are shared through the model and render verbatim in both modes (see_render_compact()).component_idsselects native declaration IDs;depthbounds outgoing declaration dependencies (None follows all reachable ones). An empty selection emits no classes or root aliases. Explicitly selected simple types are not folded away in compact mode. Unknown IDs raise KeyError; negative depths raise ValueError. Selection does not modify the schema’s class table.Unfiltered rendering never raises on a schema that compiled: a construct a stub cannot type truthfully (an unresolvable type, a member with no Python accessor) degrades to
Anywith a comment, and an error-bearing schema still emits. This never callsschema.require_valid().