def __init__(self, tree, file_tokens, filename): """Initialize the PyFlakes plugin with an AST tree and filename.""" filename = utils.normalize_path(filename) with_doctest = self.with_doctest included_by = [ include for include in self.include_in_doctest if include != "" and filename.startswith(include) ] if included_by: with_doctest = True for exclude in self.exclude_from_doctest: if exclude != "" and filename.startswith(exclude): with_doctest = False overlaped_by = [ include for include in included_by if include.startswith(exclude) ] if overlaped_by: with_doctest = True super(FlakesChecker, self).__init__( tree, filename=filename, withDoctest=with_doctest, file_tokens=file_tokens, )
def normalize(self, value: Any, *normalize_args: str) -> Any: """Normalize the value based on the option configuration.""" if self.comma_separated_list and isinstance(value, str): value = utils.parse_comma_separated_list(value) if self.normalize_paths: if isinstance(value, list): value = utils.normalize_paths(value, *normalize_args) else: value = utils.normalize_path(value, *normalize_args) return value
def test_style_guide_manager_pre_file_ignores_parsing(): """Verify how the StyleGuideManager creates a default style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) assert len(guide.style_guides) == 5 expected = [ utils.normalize_path(p) for p in [ "first_file.py", "second_file.py", "third_file.py", "sub_dir/*", ] ] assert expected == [g.filename for g in guide.style_guides[1:]]
def __init__(self, options, formatter, stats, filename=None, decider=None): """Initialize our StyleGuide. .. todo:: Add parameter documentation. """ self.options = options self.formatter = formatter self.stats = stats self.decider = decider or DecisionEngine(options) self.filename = filename if self.filename: self.filename = utils.normalize_path(self.filename) self._parsed_diff = {}
def __init__( self, options, formatter, stats, filename=None, decider=None ): """Initialize our StyleGuide. .. todo:: Add parameter documentation. """ self.options = options self.formatter = formatter self.stats = stats self.decider = decider or DecisionEngine(options) self.filename = filename if self.filename: self.filename = utils.normalize_path(self.filename) self._parsed_diff = {}
def _flake8_normalize(value, *args, **kwargs): comma_separated_list = kwargs.pop("comma_separated_list", False) normalize_paths = kwargs.pop("normalize_paths", False) if kwargs: raise TypeError("Unexpected keyword args: {}".format(kwargs)) if comma_separated_list and isinstance(value, utils.string_types): value = utils.parse_comma_separated_list(value) if normalize_paths: if isinstance(value, list): value = utils.normalize_paths(value, *args) else: value = utils.normalize_path(value, *args) return value
def _flake8_normalize(value, *args, **kwargs): # type: (str, *str, **bool) -> Union[str, List[str]] comma_separated_list = kwargs.pop("comma_separated_list", False) normalize_paths = kwargs.pop("normalize_paths", False) if kwargs: raise TypeError("Unexpected keyword args: {}".format(kwargs)) ret = value # type: Union[str, List[str]] if comma_separated_list and isinstance(ret, utils.string_types): ret = utils.parse_comma_separated_list(value) if normalize_paths: if isinstance(ret, utils.string_types): ret = utils.normalize_path(ret, *args) else: ret = utils.normalize_paths(ret, *args) return ret
def _flake8_normalize(value: str, *args: str, **kwargs: bool) -> Union[str, List[str]]: comma_separated_list = kwargs.pop("comma_separated_list", False) normalize_paths = kwargs.pop("normalize_paths", False) if kwargs: raise TypeError(f"Unexpected keyword args: {kwargs}") ret: Union[str, List[str]] = value if comma_separated_list and isinstance(ret, str): ret = utils.parse_comma_separated_list(value) if normalize_paths: if isinstance(ret, str): ret = utils.normalize_path(ret, *args) else: ret = utils.normalize_paths(ret, *args) return ret
def __init__( self, options, # type: argparse.Namespace formatter, # type: base_formatter.BaseFormatter stats, # type: statistics.Statistics filename=None, # type: Optional[str] decider=None, # type: Optional[DecisionEngine] ): """Initialize our StyleGuide. .. todo:: Add parameter documentation. """ self.options = options self.formatter = formatter self.stats = stats self.decider = decider or DecisionEngine(options) self.filename = filename if self.filename: self.filename = utils.normalize_path(self.filename) self._parsed_diff = {} # type: Dict[str, Set[int]]
def test_normalize_path(value, expected): """Verify that we normalize paths provided to the tool.""" assert utils.normalize_path(value) == expected
(['E1', 'E2'], 'F401', 'sub_dir/file.py', 0), ]) def test_style_guide_manager_pre_file_ignores(ignores, violation, filename, handle_error_return): """Verify how the StyleGuideManager creates a default style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(ignore=ignores, select=['E', 'F', 'W'], per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) assert (guide.handle_error(violation, filename, 1, 1, "Fake text") == handle_error_return) @pytest.mark.parametrize('filename,expected', [ ('first_file.py', utils.normalize_path('first_file.py')), ('second_file.py', utils.normalize_path('second_file.py')), ('third_file.py', utils.normalize_path('third_file.py')), ('fourth_file.py', None), ('sub_dir/__init__.py', utils.normalize_path('sub_dir/*')), ('other_dir/__init__.py', None), ]) def test_style_guide_manager_style_guide_for(filename, expected): """Verify the style guide selection function.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) file_guide = guide.style_guide_for(filename) assert file_guide.filename == expected
def test_normalize_path(value, expected): """Verify that we normalize paths provided to the tool.""" assert utils.normalize_path(value) == expected
(['E1', 'E2'], 'F401', 'sub_dir/file.py', 0), ]) def test_style_guide_manager_pre_file_ignores(ignores, violation, filename, handle_error_return): """Verify how the StyleGuideManager creates a default style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(ignore=ignores, select=['E', 'F', 'W'], per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) assert (guide.handle_error(violation, filename, 1, 1, "Fake text") == handle_error_return) @pytest.mark.parametrize('filename,expected', [ ('first_file.py', utils.normalize_path('first_file.py')), ('second_file.py', utils.normalize_path('second_file.py')), ('third_file.py', utils.normalize_path('third_file.py')), ('fourth_file.py', None), ('sub_dir/__init__.py', utils.normalize_path('sub_dir/*')), ('other_dir/__init__.py', None), ]) def test_style_guide_manager_style_guide_for(filename, expected): """Verify the style guide selection function.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) file_guide = guide.style_guide_for(filename) assert file_guide.filename == expected
"""Verify how the StyleGuideManager creates a default style guide.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options( ignore=ignores, select=["E", "F", "W"], per_file_ignores=PER_FILE_IGNORES_UNPARSED, ) guide = style_guide.StyleGuideManager(options, formatter=formatter) assert (guide.handle_error(violation, filename, 1, 1, "Fake text") == handle_error_return) @pytest.mark.parametrize( "filename,expected", [ ("first_file.py", utils.normalize_path("first_file.py")), ("second_file.py", utils.normalize_path("second_file.py")), ("third_file.py", utils.normalize_path("third_file.py")), ("fourth_file.py", None), ("sub_dir/__init__.py", utils.normalize_path("sub_dir/*")), ("other_dir/__init__.py", None), ], ) def test_style_guide_manager_style_guide_for(filename, expected): """Verify the style guide selection function.""" formatter = mock.create_autospec(base.BaseFormatter, instance=True) options = create_options(per_file_ignores=PER_FILE_IGNORES_UNPARSED) guide = style_guide.StyleGuideManager(options, formatter=formatter) file_guide = guide.style_guide_for(filename) assert file_guide.filename == expected