abseil/abseil-py

Importing TensorFlow causes argparse_flags.ArgumentParser to fail when specifying an argument_default

kentslaney opened this issue · 3 comments

Basically just the title. This code fails:

import tensorflow as tf
from absl.flags.argparse_flags import ArgumentParser

ArgumentParser(argument_default=42)

Here it is in Colab.

Edit: I should clarify, it only fails if tensorflow is imported

yilei commented

Thanks for filing the bug. Looks like argument_default= simply doesn't work if you have any absl.flags flag defined. This is because a default= argument is passed to custom actions when argument_default= is supplied, but our _FlagAction and _BooleanFlagAction don't support it. Since absl.flags use dest=argparse.SUPPRESS and the default value is never seen through the argparse namespace, we can simply add a default= argument and ignore it.

Is there a workaround?

yilei commented

There isn't a good workaround before we put out a fix, but you could monkey patch these methods to have an extra default= argument:

def __init__(self, option_strings, dest, help, metavar, flag_instance): # pylint: disable=redefined-builtin

def __init__(self, option_strings, dest, help, metavar, flag_instance): # pylint: disable=redefined-builtin