Skip to content
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

Support forward references defined from the global scope #1

Open
NiklasRosenstein opened this issue May 9, 2022 · 0 comments
Open

Comments

@NiklasRosenstein
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant