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

Improve rex env binding #1138

Merged

Conversation

davidlatwe
Copy link
Contributor

This PR address following two issues.

Can't check if env contains var with keyword in

In package.py, this won't work

# package.py
def commands():
    if "FOO" in env:
        print("A")
    else:
        print("B")  # cannot be reached

Because "FOO" in env or "FOO" in env.keys() always return True, no matter the env actually contains the var or not.

Commit ffbbfbc fixed this.

Can't use keyword or to get default value

Sometimes I'd like to set default value like this

# package.py
def command():
    foo = str(env.get("FOO") or "hello, foo.")

But if key "FOO" not in env, RexUndefinedVariableError is raised.

In regular dict, the get() method can take default value if the key not exists. However, the EnvironmentDict.__getitem__ always return an EnvironmentVariable instance, so the default value never had a chance. So the alternative is using or. But if key not exists, error raised when it's being evaluated in EnvironmentVariable.__bool__() method.

I think __bool__ should never raise error, hence 658322f.

@nerdvegas nerdvegas merged commit 59b3c57 into AcademySoftwareFoundation:master Oct 19, 2021
@davidlatwe davidlatwe deleted the fix-rex-env-contains branch October 19, 2021 04:26
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

Successfully merging this pull request may close these issues.

None yet

2 participants