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 on classes #72

Open
obeleh opened this issue Jul 27, 2018 · 0 comments
Open

Forward references on classes #72

obeleh opened this issue Jul 27, 2018 · 0 comments

Comments

@obeleh
Copy link

obeleh commented Jul 27, 2018

I know this is similar to #45 but bare with me, it is different

I have this situation:

from enforce import runtime_validation


class A:

    @runtime_validation
    def clone(self) -> 'A':
        return A()

A().clone()

which results in:

NameError: name 'A' is not defined

I've noticed that Forward references are actually implemented but in this case the reference is followed at "compile time" of class A and at that point class A doesn't exist yet.

Basically you want to use a late bind like this:

from enforce import runtime_validation


def late_bind(method):
    checked_meth = None

    def wrap(*args, **kwargs):
        nonlocal checked_meth
        if checked_meth is None:
            checked_meth = runtime_validation(method)
        return checked_meth(*args, **kwargs)

    return wrap


class B:

    @late_bind
    def clone(self) -> 'B':
        return 'bla'


B().clone()
@obeleh obeleh mentioned this issue Jul 27, 2018
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