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

pwkit/kwargv.py: fix fixup processing of args with non-None defaults #24

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions ci/azure-job-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,20 @@ steps:
- bash: |
set -euo pipefail

if [[ $AGENT_OS == Windows_NT ]] ; then
if [[ $AGENT_OS == Darwin ]] ; then
# As of macos-14, these no longer have Anaconda built in.
CONDA="$TMPDIR/conda"
curl -fsSL https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-x86_64.sh >miniforge.sh
bash miniforge.sh -f -b -p "$CONDA"
rm -f miniforge.sh
condabin="$CONDA/bin"
elif [[ $AGENT_OS == Windows_NT ]] ; then
CONDA=$(echo "$CONDA" | sed -e 's|\\|\/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/\L\1\E/\2|')
condabin="$CONDA/Scripts"
else
condabin="$CONDA/bin"
fi

if [[ $AGENT_OS == Darwin ]] ; then
sudo chown -R $USER $CONDA
fi

cat >activate-conda.sh <<EOF
eval "\$($condabin/conda shell.bash hook)"
conda activate
Expand Down
18 changes: 8 additions & 10 deletions pwkit/kwargv.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,14 @@ def __init__(self):
ki.default = None
elif ki.repeatable:
ki.default = []
elif ki.fixupfunc is not None and ki.default is not None:
# kinda gross structure here, oh well.
elif ki.fixupfunc is not None:
# Make sure to process the default through the fixup, if it
# exists. This helps code use "interesting" defaults with types
# that you might prefer to use when launching a task
# programmatically; e.g. a default output stream that is
# `sys.stdout`, not "-". Note, however, that the fixup will
# always get called for the default value, so it shouldn't do
# anything too expensive.
ki.default = ki.fixupfunc(ki.default)

kwinfos[kw] = ki
Expand Down Expand Up @@ -472,14 +478,6 @@ def parse(self, args=None):
'required keyword argument "%s" was not provided', kw
)

# If there's a fixup, process it even if the keyword wasn't
# provided. This lets code use "interesting" defaults with
# types that you might prefer to use when launching a task
# programmatically; e.g. a default output stream that is
# `sys.stdout`, not "-".
if ki.fixupfunc is not None:
self.set_one(ki._attrname, ki.fixupfunc(None))

return self # convenience

def parse_or_die(self, args=None):
Expand Down