Skip to content

Commit

Permalink
Changes to make ament_flake8 work with v6+ (#459)
Browse files Browse the repository at this point in the history
* Changes to make ament_flake8 work with v6+

* Correct make_file_checker_manager call

* Fix compatibility with flake8 5.

Signed-off-by: Michael Carroll <[email protected]>
Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
mjcarroll authored Dec 6, 2023
1 parent b4be064 commit fc02432
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions ament_flake8/ament_flake8/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def get_flake8_style_guide(argv):
# appropriate options to pass into the standard flake8.legacy.get_style_guide();
# passing argv gets it to determine the options for us.
application = flake8_app.Application()
file_checker_takes_argv = False
if hasattr(application, 'parse_preliminary_options'):
prelim_opts, remaining_args = application.parse_preliminary_options(
argv)
Expand Down Expand Up @@ -170,22 +171,32 @@ def get_flake8_style_guide(argv):
)
application.register_plugin_options()
application.parse_configuration_and_cli(cfg, cfg_dir, remaining_args)
else:
elif hasattr(application, 'parse_preliminary_options_and_args'):
application.parse_preliminary_options_and_args([])
flake8.configure_logging(
application.prelim_opts.verbose, application.prelim_opts.output_file)
application.make_config_finder()
application.find_plugins()
application.register_plugin_options()
application.parse_configuration_and_cli(argv)
else:
# Flake8 >= 6 uses parse_args
# Ref: https://github.com/PyCQA/flake8/commit/0d667a73299971f1cf8ff549c519fffb282b1faf
from flake8.options.parse_args import parse_args
application.plugins, application.options = parse_args(argv)
file_checker_takes_argv = True
application.make_formatter()
try:
# needed in older flake8 versions to populate the listener
application.make_notifier()
except AttributeError:
pass
application.make_guide()
application.make_file_checker_manager()

if file_checker_takes_argv:
application.make_file_checker_manager(argv)
else:
application.make_file_checker_manager()
return StyleGuide(application)


Expand All @@ -196,11 +207,19 @@ def parse_config_file(config_file):
major_release = flake8_version_info[0]

if major_release >= 5:
opts_manager = manager.OptionManager(
version=flake8_version,
plugin_versions='',
parents=[]
)
if major_release == 5:
opts_manager = manager.OptionManager(
version=flake8_version,
plugin_versions='',
parents=[],
)
else:
opts_manager = manager.OptionManager(
version=flake8_version,
plugin_versions='',
parents=[],
formatter_names=[],
)
flake8_options.register_default_options(opts_manager)
cfg, cfg_dir = config.load_config(config_file, [])

Expand Down Expand Up @@ -260,8 +279,13 @@ def custom_format(error):

# Get the names of files checked
report.report = style.check_files(paths)
file_checkers = style._application.file_checker_manager.checkers
report.files = [file_checker.filename for file_checker in file_checkers]

# Flake8 < 6 uses 'checkers' attribute
if hasattr(style._application.file_checker_manager, 'checkers'):
file_checkers = style._application.file_checker_manager.checkers
report.files = [file_checker.filename for file_checker in file_checkers]
else:
report.files = style._application.file_checker_manager.filenames

assert report.report.total_errors == len(report.errors)
return report
Expand Down

0 comments on commit fc02432

Please sign in to comment.