Skip to content

feat: Generate context types from schema #2133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: maint/1.10.0
Choose a base branch
from

Conversation

effigies
Copy link
Collaborator

@effigies effigies commented Jun 7, 2025

This PR follows-up #2115 by procedurally generating Python types from the schema.meta.context object. This uses structural subtyping to allow for a variety of implementations to satisfy type-checkers, while also providing default standard library dataclass implementations.

The dataclass fields are annotated with protocols instead of one another, so that you can flexibly mix-and-match the provided dataclasses and your own classes, for example:

from functools import cached_property
import json

import attrs
from bidsschematools.types import context as ctx

@attrs.define
class Dataset:
    tree: FileTree
    ignored: list[str]

    @cached_property
    def dataset_description(self) -> dict[str, Any]:
        with open(tree['dataset_description.json']) as fobj:
            return json.load(fobj)

    @cached_property
    def modalities(self) -> list[str]:
        ...

    @cached_property
    def datatypes(self) -> list[str]:
        ...

    @cached_property
    def subjects(self) -> ctx.Subjects:
        ...

Dataset is assignable to a variable annotated as bidsschematools.types.protocols.Dataset, but not bidsschematools.types.context.Dataset.

Protocols doc: https://bidsschematools--2133.org.readthedocs.build/en/2133/api/bidsschematools.types.protocols.html
Dataclasses doc: https://bidsschematools--2133.org.readthedocs.build/en/2133/api/bidsschematools.types.context.html

As with #2115, the goal here is to remain dynamic in editable installation mode, constructing and execing the module on-the-fly, while freezing a copy for optimal loading when performing full installations or building packages.

@effigies effigies added the schema-code Updates or changes to the code used to parse, filter, and render the schema. label Jun 7, 2025
@effigies effigies force-pushed the feat/context-types branch from 9c99674 to bf537b3 Compare June 7, 2025 15:21
@effigies effigies force-pushed the feat/context-types branch from bf537b3 to dcaba25 Compare June 7, 2025 15:24
@effigies
Copy link
Collaborator Author

effigies commented Jun 7, 2025

@effigies effigies marked this pull request as ready for review June 7, 2025 16:30
@effigies effigies added the exclude-from-changelog This item will not feature in the automatically generated changelog label Jun 7, 2025
@effigies effigies force-pushed the feat/context-types branch 4 times, most recently from 3432cb1 to f0a243f Compare June 11, 2025 03:08
@effigies effigies requested a review from ubdbra001 June 11, 2025 04:25
@effigies
Copy link
Collaborator Author

@ubdbra001 Just a heads up on this. This is going to replace much of bids-standard/python-validator#24, which will instead do something like what I have above.

@effigies effigies force-pushed the feat/context-types branch from 56fdb92 to c4f00a7 Compare June 17, 2025 13:42
@effigies effigies requested review from rwblair and bendhouseart June 21, 2025 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exclude-from-changelog This item will not feature in the automatically generated changelog schema-code Updates or changes to the code used to parse, filter, and render the schema.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant