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

Forward References #45

Open
georgwiese opened this issue Jul 24, 2017 · 3 comments
Open

Forward References #45

georgwiese opened this issue Jul 24, 2017 · 3 comments

Comments

@georgwiese
Copy link

Do you support forward references? I'm getting this behavior:

import enforce
import typing

MyType = typing.List[typing.Union[int, 'MyType']]

@enforce.runtime_validation()
def foo(x: MyType):
  pass

foo([1, 2])    # Good
foo([1, [2]])  # RuntimeTypeError

The error states: Argument 'x' was not of type typing.List<~T>[typing.Union[int, _ForwardRef('MyType')]]. Actual type was typing.List[int, typing.List].

Are forward references not supported, is this a bug, or am I doing something wrong? Thanks :)

@RussBaz
Copy link
Owner

RussBaz commented Jul 25, 2017

Well, as it turns out, forward reference is not supported yet. Sorry. I will look into this as soon as I have a moment.

Enforce can accept strings as type hints but the types need to exist at the moment of decorating.

I think it is not very hard to fix, just gonna take some time. Forward reference need to invoke a validation tree generation when it is encountered for the first time and replace itself with it before proceeding. I think this is the fastest way to implement it at the moment.

Thanks for the report!

@bannsec
Copy link

bannsec commented Dec 4, 2017

Run into this a few times as well. Would love for forward refs to be implemented.

RussBaz added a commit that referenced this issue Mar 7, 2018
…upport for the forward references (#45) and added inline type assertions.
@RussBaz
Copy link
Owner

RussBaz commented Mar 12, 2018

BTW, the dev branch now supports Forward References. If you have some time - please give it a try. Thanks.

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

3 participants