def add_options(cls, option_manager: _OptionManager): option_manager.add_option( f'--{cls.GREEDY_OPTION_NAME}', default=cls.NO_GREEDY, choices=cls.GREEDY_CHOICES, parse_from_config=True, )
def add_options(cls, option_manager: _OptionManager): option_manager.add_option( f'--{cls.OPTION_NAME}', action='store_true', default=False, parse_from_config=True, )
def add_options(option_manager: OptionManager): """Add the offline option.""" option_manager.add_option( Nitpick.format_flag(Nitpick.Flags.OFFLINE), action="store_true", # dest="offline", help=Nitpick.Flags.OFFLINE.value, )
def parse_options(allowed_test_directories, allowed_test_arguments_count, allowed_assert_count): options = OptionManager() options.allowed_test_directories = allowed_test_directories options.allowed_test_arguments_count = allowed_test_arguments_count options.allowed_assert_count = allowed_assert_count FinePytestChecker.parse_options(options)
def add_options(option_manager: OptionManager): option_manager.add_option( '--some-fancy-var', # converted to some_fancy_var in options type=int, metavar='n', # shows up in help text default=0, parse_from_config=True, help='Fancy var description. (Default: %(default)s)')
def add_options(cls, parser: OptionManager) -> None: """Add custom configuration option(s) to flake8.""" parser.add_option( "--suppress-none-returning", default=False, action="store_true", parse_from_config=True, help= ("Suppress ANN200-level errors for functions that contain no return statement or " "contain only bare return statements. (Default: False)"), )
def _parse_options(manager: OptionManager, args: List[str]) -> Config: namespace, remaining_args = manager.parse_args(args) return CyclomaticComplexityAdjustableChecker.parse_options_to_config( manager, namespace, remaining_args, )
def option_parser(): """Returns option parser that can be used for tests.""" parser = OptionManager( prog=version.pkg_name, version=version.pkg_version, ) config.Configuration().register_options(parser) return parser
def test_config_parsed(self): default_config = get_config().get_default_instance() parser = OptionManager('', '') DarglintChecker.add_options(parser) options, args = parser.parse_args([]) DarglintChecker.parse_options(options) self.assertEqual(default_config.style, DarglintChecker.config.style) argv = ['--docstring-style=numpy', '--strictness=short'] options, args = parser.parse_args(argv) DarglintChecker.config = default_config DarglintChecker.parse_options(options) self.assertEqual(DarglintChecker.config.style, DocstringStyle.NUMPY) self.assertEqual(DarglintChecker.config.strictness, Strictness.SHORT_DESCRIPTION)
def test__add_options__should_pass(self): flake8_opt_mgr = OptionManager( prog="flake8", version=flake8.__version__, ) plugin = Flake8Argparse(None, SAMPLE_FILE_PATH) plugin.add_options(flake8_opt_mgr) assert len(flake8_opt_mgr.options) == 2
def add_options(cls, option_manager: OptionManager): option_manager.add_option( "--fdp-decorator-order", parse_from_config=True, comma_separated_list=True, default="", help= "The order that decorators should be in for each resource function. Omitted names will be ignored.", ) option_manager.add_option( "--fdp-api-doc-keyword-order", parse_from_config=True, comma_separated_list=True, default="", help= "The order that keywords should be in for each @api.doc(). Omitted names will be ignored.", )
def test__parse_options(self): flake8_opt_mgr = OptionManager() plugin = Flake8Argparse(None, SAMPLE_FILE_PATH) args = SysArgs(illegal_import_dir=SAMPLE_FILE_DIR, illegal_import_packages='os,json') plugin.parse_options(flake8_opt_mgr, args, extra_args=None) assert plugin.illegal_import_dir == args.illegal_import_dir assert plugin.illegal_import_packages == ['os', 'json']
def add_options(cls, parser: OptionManager) -> None: """ ``flake8`` api method to register new plugin options. See :class:`.Configuration` docs for detailed options reference. Arguments: parser: ``flake8`` option parser instance. """ parser.add_option( '--eradicate-aggressive', default=False, help=('Enables aggressive mode for eradicate; ' 'this may result in false positives'), action='store_true', parse_from_config=True, )
def parse_options(**config): options = OptionManager() options.allowed_test_directories = config.get('allowed_test_directories', None) options.allowed_test_arguments_count = config.get( 'allowed_test_arguments_count', None) options.allowed_assert_count = config.get('allowed_assert_count', None) options.xfail_check_until = config.get('xfail_check_until', False) options.xfail_check_reason = config.get('xfail_check_reason', False) options.force_unique_test_names = config.get('force_unique_test_names', False) options.force_usefixtures = config.get('force_usefixtures', False) FinePytestChecker.parse_options(options)
def add_options(cls, option_manager: OptionManager) -> None: option_manager.add_option( '--pytest-fixture-no-parentheses', action='store_true', parse_from_config=True, default=not DEFAULT_CONFIG.fixture_parentheses, help='Omit parentheses for @pytest.fixture decorators' ' without parameters. (Default: %default)', ) option_manager.add_option( '--pytest-raises-require-match-for', comma_separated_list=True, parse_from_config=True, default=DEFAULT_CONFIG.raises_require_match_for, help='List of exceptions for which flake8-pytest-style requires' ' a match= argument in pytest.raises(). (Default: %default)', ) option_manager.add_option( '--pytest-parametrize-names-type', choices=enum_choices(ParametrizeNamesType), parse_from_config=True, default=DEFAULT_CONFIG.parametrize_names_type.value, help='Preferred type for multiple parameter names in' ' @pytest.mark.parametrize. (Default: %default)', ) option_manager.add_option( '--pytest-parametrize-values-type', choices=enum_choices(ParametrizeValuesType), parse_from_config=True, default=DEFAULT_CONFIG.parametrize_values_type.value, help='Preferred type for values in @pytest.mark.parametrize.' ' (Default: %default)', ) option_manager.add_option( '--pytest-parametrize-values-row-type', choices=enum_choices(ParametrizeValuesRowType), parse_from_config=True, default=DEFAULT_CONFIG.parametrize_values_row_type.value, help='Preferred type for each row in @pytest.mark.parametrize' ' in case of multiple parameters. (Default: %default)', )
def test__parse_options__should_pass(self): flake8_opt_mgr = OptionManager(prog="flake8", version=flake8.__version__) plugin = Flake8Argparse(None, SAMPLE_FILE_PATH) args = SysArgs( test_func_name_validator_module=SAMPLE_VALIDATOR_MODULE_PATH, test_func_name_validator_regex="test_.*", ) plugin.parse_options(flake8_opt_mgr, args, extra_args=None) assert (plugin.test_func_name_validator_regex == args.test_func_name_validator_regex) assert (plugin.test_func_name_validator_module == args.test_func_name_validator_module)
def parse_args(plugin_class, args): """ Return a new plugin class with the argument options parsed. Parameters ---------- plugin_class : type The object to be used as a flake8 extension. args : list of str Argument options to be parsed. Returns ------- new_plugin_class : subclass of plugin_class """ class TmpPlugin(plugin_class): pass prog = "flake8" manager = OptionManager(prog, TmpPlugin.version) TmpPlugin.add_options(manager) options, _ = manager.parse_args(args) TmpPlugin.parse_options(options) return TmpPlugin
def run_validator_for_test_file(filename, max_annotations_complexity=None, strict_mode=False, attributes_order=None): test_file_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'test_files', filename, ) with open(test_file_path, 'r') as file_handler: raw_content = file_handler.read() tree = ast.parse(raw_content) options = OptionManager() options.use_class_attributes_order_strict_mode = strict_mode options.class_attributes_order = attributes_order ClassAttributesOrderChecker.parse_options(options) checker = ClassAttributesOrderChecker(tree=tree, filename=filename) if max_annotations_complexity: checker.max_annotations_complexity = max_annotations_complexity return list(checker.run())
def add_options(cls, parser: OptionManager) -> None: for option in ('--max-mccabe-complexity', '--max-adjustable-complexity'): parser.add_option( option, type=int, dest='max_mccabe_complexity', parse_from_config=True, help='Max mccabe complexity', default=DEFAULT_CONFIG.max_mccabe_complexity, ) parser.add_option( '--per-path-max-adjustable-complexity', dest='max_complexity_per_path', comma_separated_list=True, parse_from_config=True, help=( 'Comma-separated list of pairs of files or directories to ' 'check and the desired max complexity value within that path.' ), default='', ) parser.add_option( '--var-names-extra-blacklist', dest='var_names_extra_blacklist', comma_separated_list=True, parse_from_config=True, help=('Comma-separated list of bad variable names to blacklist. ' 'Each variable will affect the max allowed complexity.'), default='', ) parser.add_option( '--var-names-whitelist', dest='var_names_whitelist', comma_separated_list=True, parse_from_config=True, help=('Comma-separated list of bad variable names to whitelist. '), default='', )
def add_options(cls, parser: OptionManager) -> None: parser.add_option( '--allowed-test-directories', comma_separated_list=True, parse_from_config=True, help='Comma-separated list of allowed test directories', ) parser.add_option( '--allowed-test-arguments-count', type=int, parse_from_config=True, help='Allowed arguments in test signature', ) parser.add_option( '--allowed-assert-count', type=int, parse_from_config=True, help='Allowed assert statement count in test', )
def add_options(cls, parser: OptionManager) -> None: """ ``flake8`` api method to register new plugin options. See :class:`.Configuration` docs for detailed options reference. Arguments: parser: ``flake8`` option parser instance. """ parser.add_option( '--eradicate-aggressive', default=False, help=('Enables aggressive mode for eradicate; ' 'this may result in false positives'), action='store_true', parse_from_config=True, ) parser.add_option( '--eradicate-whitelist', default=False, help=('String of "#" separated comment beginnings to whitelist ' 'for eradicate. ' 'Single parts are interpreted as regex. ' 'OVERWRITING the default whitelist: {0}').format( Eradicator.DEFAULT_WHITELIST), action='store', parse_from_config=True, ) parser.add_option( '--eradicate-whitelist-extend', default=False, help=('String of "#" separated comment beginnings to whitelist ' 'for eradicate. ' 'Single parts are interpreted as regex. ' 'Overwrites --eradicate-whitelist. ' 'EXTENDING the default whitelist: {0} ').format( Eradicator.DEFAULT_WHITELIST), action='store', parse_from_config=True, )
def add_options(cls, options_manager: OptionManager): options_manager.add_option('--app-import-names', default='', type=str, comma_separated_list=True, parse_from_config=True)
def get_option_manager() -> OptionManager: manager = OptionManager(prog='flake8', version=flake8.__version__) CyclomaticComplexityAdjustableChecker.add_options(manager) return manager
def test__add_options(self): flake8_opt_mgr = OptionManager() plugin = Flake8Argparse(None, SAMPLE_FILE_PATH) plugin.add_options(flake8_opt_mgr) assert len(flake8_opt_mgr.options) == 2
def add_options(option_manager: OptionManager): """Add the offline option.""" option_manager.add_option(OptionEnum.OFFLINE.as_flake8_flag(), action="store_true", help=OptionEnum.OFFLINE.value)
def register_options(self, parser: OptionManager) -> None: """Registers options for our plugin.""" for option in self.options: parser.add_option(**attr.asdict(option))
def add_options(cls, parser: OptionManager) -> None: parser.add_option( '--allowed-test-directories', comma_separated_list=True, parse_from_config=True, default=['test_unit', 'test_integration'], help='Comma-separated list of allowed test directories', ) parser.add_option( '--allowed-test-arguments-count', type=int, parse_from_config=True, default=6, help='Allowed arguments in test signature', ) parser.add_option( '--allowed-assert-count', type=int, parse_from_config=True, default=6, help='Allowed assert statement count in test', ) parser.add_option( '--xfail-check-until', action='store_true', default=True, parse_from_config=True, help='Check that xfail has until parameter', ) parser.add_option( '--xfail-check-reason', action='store_true', default=True, parse_from_config=True, help='Check that xfail has reason parameter', ) parser.add_option( '--force-unique-test-names', action='store_true', parse_from_config=True, default=True, help='Enforce unqiue test names', ) parser.add_option( '--force-usefixtures', action='store_true', parse_from_config=True, default=True, help='Enforce usefixtures validator', )
def register_options(self, parser: OptionManager) -> None: """Registers options for our plugin.""" for option in self._options: parser.add_option(**option.asdict_no_none())
def add_options(cls, option_manager: OptionManager) -> None: """Parse plugin options.""" option_manager.add_option( '--type-checking-exempt-modules', comma_separated_list=True, parse_from_config=True, default=[], help= 'Skip TC001, TC002, and TC003 checks for specified modules or libraries.', ) # Third-party library options option_manager.add_option( '--type-checking-pydantic-enabled', action='store_true', parse_from_config=True, default=False, help='Prevent flagging of annotations for class definitions.', ) option_manager.add_option( '--type-checking-pydantic-enabled-baseclass-passlist', comma_separated_list=True, parse_from_config=True, default=[], help= 'Names of base classes to not treat as pydantic models. For example `NamedTuple` or `TypedDict`.', ) option_manager.add_option( '--type-checking-fastapi-enabled', action='store_true', parse_from_config=True, default=False, help='Prevent flagging of annotations for decorated functions.', ) option_manager.add_option( '--type-checking-fastapi-dependency-support-enabled', action='store_true', parse_from_config=True, default=False, help='Prevent flagging of annotations for any function.', ) option_manager.add_option( '--type-checking-cattrs-enabled', action='store_true', parse_from_config=True, default=False, help='Prevent flagging of annotations on attrs class definitions.', )
def aggregate_options( manager: OptionManager, config_finder: config.ConfigFileFinder, argv: List[str], ) -> Tuple[argparse.Namespace, List[str]]: """Aggregate and merge CLI and config file options. :param flake8.options.manager.OptionManager manager: The instance of the OptionManager that we're presently using. :param flake8.options.config.ConfigFileFinder config_finder: The config file finder to use. :param list argv: The list of remaining command-line argumentsthat were unknown during preliminary option parsing to pass to ``manager.parse_args``. :returns: Tuple of the parsed options and extra arguments returned by ``manager.parse_args``. :rtype: tuple(argparse.Namespace, list) """ # Get defaults from the option parser default_values, _ = manager.parse_args([]) # Make our new configuration file mergerator config_parser = config.MergedConfigParser(option_manager=manager, config_finder=config_finder) # Get the parsed config parsed_config = config_parser.parse() # Extend the default ignore value with the extended default ignore list, # registered by plugins. extended_default_ignore = manager.extended_default_ignore.copy() LOG.debug("Extended default ignore list: %s", list(extended_default_ignore)) extended_default_ignore.update(default_values.ignore) default_values.ignore = list(extended_default_ignore) LOG.debug("Merged default ignore list: %s", default_values.ignore) extended_default_select = manager.extended_default_select.copy() LOG.debug("Extended default select list: %s", list(extended_default_select)) default_values.extended_default_select = extended_default_select # Merge values parsed from config onto the default values returned for config_name, value in parsed_config.items(): dest_name = config_name # If the config name is somehow different from the destination name, # fetch the destination name from our Option if not hasattr(default_values, config_name): dest_name = config_parser.config_options[config_name].dest LOG.debug( 'Overriding default value of (%s) for "%s" with (%s)', getattr(default_values, dest_name, None), dest_name, value, ) # Override the default values with the config values setattr(default_values, dest_name, value) # Finally parse the command-line options return manager.parse_args(argv, default_values)