Update compatibility docs

This commit is contained in:
Ian Cordasco 2016-06-26 06:41:47 -05:00
parent 6b7855e102
commit 5a9b7c27ab
No known key found for this signature in database
GPG key ID: 656D3395E4A9791A

View file

@ -106,6 +106,8 @@ options with |Flake8| and have it work on |Flake8| 2.x and 3.x.
.. code-block:: python
import optparse
option_args = ('-X', '--example-flag')
option_kwargs = {
'type': 'string',
@ -115,7 +117,7 @@ options with |Flake8| and have it work on |Flake8| 2.x and 3.x.
try:
# Flake8 3.x registration
parser.add_option(*option_args, **option_kwargs)
except TypeError:
except (optparse.OptionError, TypeError):
# Flake8 2.x registration
parse_from_config = option_kwargs.pop('parse_from_config', False)
parser.add_option(*option_args, **option_kwargs)
@ -127,13 +129,17 @@ Or, you can write a tiny helper function:
.. code-block:: python
import optparse
def register_opt(parser, *args, **kwargs):
try:
# Flake8 3.x registration
parser.add_option(*args, **kwargs)
except TypeError:
except (optparse.OptionError, TypeError):
# Flake8 2.x registration
parse_from_config = kwargs.pop('parse_from_config', False)
kwargs.pop('comma_separated_list', False)
kwargs.pop('normalize_paths', False)
parser.add_option(*args, **kwargs)
if parse_from_config:
parser.config_options.append(args[-1].lstrip('-'))