Skip to content

Schemaview: add helper to return extra_slots of a class's ancestors #4145

Open

Description

@noelmcloughlin

What is your feature request?

A class's own extra_slots is not inherited in the metamodel, but its inherited by generated OO code. A Java subclass inherits its parent's handling of extra data. A Pydantic subclass inherits its parent's model_config and pydantic_extra.

So generator has to read ancestors' extra_slots to decide what each generated class must state for itself. At least two generators repeat the pattern today:

JavaGenerator.needs_extra_slots skips the extra-slots code when an ancestor allows extra data.

def needs_extra_slots(self, klass: ClassDefinition) -> bool:

PydanticGenerator._get_extra_for_class (#3630) writes a class's own extra_slots setting when it differs from what the class would inherit. For example, if the parent accepts extra values of one type, an allowed: true subclass declares it accepts extra values of any type. If the parent allows extra data, a subclass with no extra_slots will forbid extra data.

A SchemaView method could do the walk once for both, for example:

def class_ancestors_extra_slots(
    self, class_name: CLASS_NAME, mixins: bool = True
) -> dict[CLASS_NAME, ExtraSlotsExpression]:
    """The extra_slots of each ancestor that sets one, in class_ancestors order."""
Each generator would keep its own rule for what to emit. Materializing extra_slots in induced_class would not help, because the metamodel does not pass the value down.

Additional context

Raised in review of #3630.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions