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

Use default values in POSIX-style variable expansions #143

Closed
wants to merge 1 commit into from

Conversation

gergelypolonkai
Copy link

An attempt to continue #30 with default values

It’s not perfect, but it works as i expect it to work. For example, using k in _replacement() feels a bit hacky.

What do you think?

If you like the basic idea, i can add tests, documentation, etc. to this PR.

@coveralls
Copy link

coveralls commented Oct 4, 2018

Coverage Status

Coverage decreased (-0.1%) to 92.446% when pulling cc8689d on gergelypolonkai:master into 40ed1b1 on theskumar:master.


# If name is the same as k, we need to handle the situation special, or we will have ${K}
# or ${K:-default} as the return value. Note that k comes from the for loop below
if name == k:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k isn't defined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is; it comes from the for loop that iterates over values right after the definition of _re_sub_callback(). Also see the end of the comment above this line.

That’s exactly the reason i wrote it feels hacky.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is it in the scope of the parent function? 🤢

# or ${K:-default} as the return value. Note that k comes from the for loop below
if name == k:
# If we have a default set, return it
if default:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about return default or ""?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the original line of code; i don’t know why i changed it, but i will put that back in place.

@@ -181,21 +181,42 @@ def unset_key(dotenv_path, key_to_unset, quote_mode="always"):


def resolve_nested_variables(values):
def _replacement(name):
def _replacement(name, default=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about some unit tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like the general idea, i can go on and write the tests and expand the documentation with examples and caveats.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like the general idea, i can go on and write tests and expand the documentation with the new functionality and some caveats.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests would help to clarify what the general idea is.

Note that I'm not a contributor to this library, so my opinion doesn't carry any weight.

@gergelypolonkai
Copy link
Author

It seems the tests fail on 3.3 regardless of my change. I wonder why it says anything about IPython…


# If all the above failed, try getting name from the environments, revert to the value
# from values, and if it’s not in values yet, revert to the default value
return os.getenv(name, values.get(name, default or ''))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already tried to get name from os.environ. Why try again here?

@theskumar theskumar force-pushed the master branch 2 times, most recently from f628767 to f9863d3 Compare December 16, 2018 13:18
@bbc2
Copy link
Collaborator

bbc2 commented Apr 16, 2020

Sorry I never really looked into this PR. It is now superseded by #248.

@bbc2 bbc2 closed this Apr 16, 2020
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.

4 participants