Skip to content

Support forward references defined from the global scope #1

Open

Description

@NiklasRosenstein

Example:

PlainType = t.Union[str, bool, int, float, "Expression"]
ValueType = t.Union[PlainType, t.List["PlainType"], t.Dict[str, "PlainType"]]

@dataclasses.dataclass
class Attribute(Node):
    key: str
    value: ValueType

When resolving the schema members of Attribute, we should be able to correctly interpret the "Expression" and "PlainType" forward references. What we currently get is

  File "/Users/niklas.rosenstein/gitme/kraken/.venvs/3.10.2/lib/python3.10/site-packages/typeapi/model.py", line 277, in evaluate
    raise RuntimeError(f'no module or namespace to evaluate {self}')
RuntimeError: no module or namespace to evaluate ForwardRef('Expression')

A workaround for now is to explicitly pass the module name to a typing.ForwardRef() object:

PlainType = t.Union[str, bool, int, float, t.ForwardRef("Expression", module=__name__)]
ValueType = t.Union[PlainType, t.List[t.ForwardRef("PlainType", module=__name__)], t.Dict[str, t.ForwardRef("PlainType", module=__name__)]]

I think that unfortunately we won't be able to tell where the forward reference was defined originally (i.e. if it was imported from another module, we won't be able to detect that to use that module's scope for evaluation), but at least within the same module of the type that we're evaluating it's members from we should try to resolve it in that type's module.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions