fix(cli): resolve string and future annotations in cli parser (#374) - #613
Open
ManoharPaturi wants to merge 1 commit into
Open
ManoharPaturi wants to merge 1 commit into
ManoharPaturi wants to merge 1 commit into
Conversation
…-NeMo#374) Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com>
This branch has not been deployed
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.
Fixes #374.
Motivation
When recipes or functions are defined in modules with
from __future__ import annotations(or use string annotations / ForwardRefs), parameter annotations are stored as string expressions (e.g.'int','str','list[str]','int | None').Previously,
_maybe_resolve_annotationonly queried_resolve_type_checking_annotation, which scannedif TYPE_CHECKING:blocks in AST. String annotations for built-in types (like'int') or module globals were never resolved to type objects, causingTypeParser.parseto fall through toUnknownTypeError: Unsupported type: int.Solution
nemo_run/cli/cli_parser.py:_resolve_string_annotationwhich resolves string annotations by first checkingbuiltins(e.g.int,str,float,bool), then evaluating in the namespace ofbuiltins,typing, and the target function/class's module globals, and falling back to ASTTYPE_CHECKINGimports._maybe_resolve_annotationto use_resolve_string_annotationfor string andForwardRefannotations, and supporttypes.UnionType(PEP 604X | Y).TypeParser.get_parserto inspectbuiltinswhenannotationis a string matching a built-in type name.test/cli/test_cli_parser.pycovering scalar string annotations, container string annotations, and PEP 604 unions.All 82 tests in
test/cli/test_cli_parser.pypass cleanly.