✨ ProcessBuilder: add get_schema method#7252
Open
GeigerJ2 wants to merge 15 commits intoaiidateam:mainfrom
Open
✨ ProcessBuilder: add get_schema method#7252GeigerJ2 wants to merge 15 commits intoaiidateam:mainfrom
ProcessBuilder: add get_schema method#7252GeigerJ2 wants to merge 15 commits intoaiidateam:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7252 +/- ##
==========================================
+ Coverage 79.85% 79.89% +0.05%
==========================================
Files 566 566
Lines 43896 43980 +84
==========================================
+ Hits 35047 35132 +85
+ Misses 8849 8848 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
get_schema method to ProcessBuilderget_schema method to ProcessBuilder
Move `get_schema` from a function injected into the dynamic subclass to a proper method on `ProcessBuilderNamespace`. This prevents port name shadowing and enables IDE autocompletion and type checking. Key changes: - Add `_RESERVED_METHOD_NAMES` guard that raises `RuntimeError` if a port name conflicts with a reserved method (currently only `get_schema`) - Extract `_format_valid_type` as a `@staticmethod` on the class - Extract `_build_schema_dict` as a private recursive helper method - Rename `format` parameter to `mode` to avoid shadowing the builtin - Remove `format='keys'` mode (redundant with compact) - Remove metadata-to-end reordering (collapsed by default anyway) - Use `Literal` type aliases (`SchemaMode`, `SchemaShow`) instead of `str` with runtime validation - Deduplicate type names in `_format_valid_type` (e.g. UpfData from different packages sharing the same `__name__`) - Handle empty dynamic namespaces (e.g. monitors, pseudos) with `Namespace(Type)` in compact mode and full info dict in verbose mode - Rewrite tests with precise YAML assertions instead of string checks
- Show actual default values in verbose mode instead of just `has_default: true`, with callable defaults displayed as `<callable>` - Fix show='set' incorrectly showing collapsed namespaces that contain only empty sub-namespaces (e.g. stash, unstash) by recursing to check for actual user-set values - Restore port name conflict check that was lost during merge - Add clarifying comments for __dir__ override, dict.fromkeys deduplication, .value unwrapping, and empty namespace handling
get_schema method to ProcessBuilderProcessBuilder: add get_schema method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR hopefully manages to eliminate many of the usability concerns we have received from users about working with builders.
Rendered docs for the new method:
https://aiida--7252.org.readthedocs.build/projects/aiida-core/en/7252/topics/processes/usage.html#inspecting-the-input-schema
It adds a
get_schema()method toProcessBuilderNamespacethat returns a nice, YAML-formatted overview of all available inputs, so one can easily explore its structure, required inputs, types of the parameters, what has already been set, etc. Dogfooding on some real-world workchains very welcome @npaulish, @mbercx, @Minotakm =)Motivation
(LLM-style headings, but I verified the whole text here, pinky promise)
When working with complex workflows (especially those with deeply nested namespaces like
PwBandsWorkChain), it's difficult to quickly see:The new
get_schema()method provides a clean, readable output that helps users understand the input structure at a glance.Parameters
The following arguments are provided to customize what is being shown:
mode'compact' | 'verbose''compact''compact'showsx: Int (required),'verbose'includes type, help text, and other metadatashow'all' | 'required' | 'set''all''all'shows everything,'required'only required,'set'only what's been setcollapsetuple[str, ...]('metadata',){...}max_depthint | NoneNoneType names are displayed in a compact format (e.g.,
Intinstead of<class 'aiida.orm.nodes.data.int.Int'>) for better readability. For types that accept multiple valid types, they are shown asInt | Float. Dynamic namespaces (e.g.monitors,pseudos) with no static children are displayed asNamespace(Type)in compact mode, and as a structured dict withtype,entry_type,help, andrequiredfields in verbose mode.Notes on the implementation
get_schemais defined as a regular method onProcessBuilderNamespace, not as a dynamic property on the per-instance subclass. Without a safeguard, a process that defines a port namedget_schemawould cause the dynamic subclass to create a property that shadows the inherited method (properties are data descriptors and take precedence in Python's MRO). To prevent this,_RESERVED_METHOD_NAMESis a class-levelfrozensetchecked in__init__— if a port name conflicts, aRuntimeErroris raised at builder construction time. Only methods introduced byProcessBuilderNamespaceitself are reserved here; inheritedMutableMappingmethods (get,keys,values,items, ...) are intentionally excluded because existing processes already use some of these as port names (e.g.valuesinExampleWorkChain).The recursive schema building logic lives in
_build_schema_dict, a private method that walks thePortNamespacetree._format_valid_typeis a@staticmethodthat formats type annotations compactly, with deduplication to handle cases where different packages export classes with the same__name__(e.g.aiida.orm.UpfDataandaiida_pseudo.data.pseudo.upf.UpfData).Type aliases
SchemaModeandSchemaShoware defined at module level usingLiteraltypes, providing static type checking for themodeandshowparameters.