# Buggy code
def _update_defaults(self, defaults):
    """Updates the given defaults with values from the config files and
    the environ. Does a little special handling for certain types of
    options (lists)."""

    # Accumulate complex default state.
    self.values = optparse.Values(self.defaults)
    late_eval = set()
    # Then set the options with those values
    for key, val in self._get_ordered_configuration_items():
        # '--' because configuration supports only long names
        option = self.get_option("--" + key)

        # Ignore options not present in this parser. E.g. non-globals put
        # in [global] by users that want them to apply to all applicable
        # commands.
        if option is None:
            continue

        if option.action in ("store_true", "store_false", "count"):
            val = strtobool(val)
        elif option.action == "append":
            val = val.split()
            val = [self.check_default(option, key, v) for v in val]
        elif option.action == "callback":
            late_eval.add(option.dest)
            opt_str = option.get_opt_string()
            val = option.convert_value(opt_str, val)
            # From take_action
            args = option.callback_args or ()
            kwargs = option.callback_kwargs or {}
            option.callback(option, opt_str, val, self, *args, **kwargs)
        else:
            val = self.check_default(option, key, val)

        defaults[option.dest] = val

    for key in late_eval:
        defaults[key] = getattr(self.values, key)
    self.values = None
    return defaults


# Traceback (ValueError)
$ env PIP_REQUIRE_VIRTUALENV='blah' PYTHONPATH=src python -m pip install twisted
Traceback (most recent call last):
File "/home/moshez/.pyenv/versions/3.6.4/lib/python3.6/runpy.py", line 193, in
_run_module_as_main
...
values = self.get_default_values()
File "/home/moshez/src/pip/src/pip/_internal/baseparser.py", line 230, in get_default_values
defaults = self._update_defaults(self.defaults.copy())  # ours
File "/home/moshez/src/pip/src/pip/_internal/baseparser.py", line 195, in _update_defaults
val = strtobool(val)
File "/home/moshez/.pyenv/versions/3.6.4/lib/python3.6/distutils/util.py", line 317, in strtobool
raise ValueError("invalid truth value %r" % (val,))
ValueError: invalid truth value 'blah'

# Fixed code
def _update_defaults(self, defaults):
    """Updates the given defaults with values from the config files and
    the environ. Does a little special handling for certain types of
    options (lists)."""

    # Accumulate complex default state.
    self.values = optparse.Values(self.defaults)
    late_eval = set()
    # Then set the options with those values
    for key, val in self._get_ordered_configuration_items():
        # '--' because configuration supports only long names
        option = self.get_option("--" + key)

        # Ignore options not present in this parser. E.g. non-globals put
        # in [global] by users that want them to apply to all applicable
        # commands.
        if option is None:
            continue

        if option.action in ("store_true", "store_false", "count"):
            try:
                val = strtobool(val)
            except ValueError:
                error_msg = invalid_config_error_message(option.action, key, val)
                self.error(error_msg)

        elif option.action == "append":
            val = val.split()
            val = [self.check_default(option, key, v) for v in val]
        elif option.action == "callback":
            late_eval.add(option.dest)
            opt_str = option.get_opt_string()
            val = option.convert_value(opt_str, val)
            # From take_action
            args = option.callback_args or ()
            kwargs = option.callback_kwargs or {}
            option.callback(option, opt_str, val, self, *args, **kwargs)
        else:
            val = self.check_default(option, key, val)

        defaults[option.dest] = val

    for key in late_eval:
        defaults[key] = getattr(self.values, key)
    self.values = None
    return defaults