How to use get_noqa_suppressions method in prospector

Best Python code snippet using prospector_python

suppression.py

Source:suppression.py Github

copy

Full Screen

...19from prospector import encoding20_FLAKE8_IGNORE_FILE = re.compile(r"flake8[:=]\s*noqa", re.IGNORECASE)21_PEP8_IGNORE_LINE = re.compile(r"#\s+noqa", re.IGNORECASE)22_PYLINT_SUPPRESSED_MESSAGE = re.compile(r"^Suppressed \'([a-z0-9-]+)\' \(from line \d+\)$")23def get_noqa_suppressions(file_contents):24 """25 Finds all pep8/flake8 suppression messages26 :param file_contents:27 A list of file lines28 :return:29 A pair - the first is whether to ignore the whole file, the30 second is a set of (0-indexed) line numbers to ignore.31 """32 ignore_whole_file = False33 ignore_lines = set()34 for line_number, line in enumerate(file_contents):35 if _FLAKE8_IGNORE_FILE.search(line):36 ignore_whole_file = True37 if _PEP8_IGNORE_LINE.search(line):38 ignore_lines.add(line_number + 1)39 return ignore_whole_file, ignore_lines40_PYLINT_EQUIVALENTS = {41 # TODO: blending has this info already?42 "unused-import": (43 ("pyflakes", "FL0001"),44 ("frosted", "E101"),45 )46}47def _parse_pylint_informational(messages):48 ignore_files = set()49 ignore_messages = defaultdict(lambda: defaultdict(list))50 for message in messages:51 if message.source == "pylint":52 if message.code == "suppressed-message":53 # this is a message indicating that a message was raised54 # by pylint but suppressed by configuration in the file55 match = _PYLINT_SUPPRESSED_MESSAGE.match(message.message)56 suppressed_code = match.group(1)57 line_dict = ignore_messages[message.location.path]58 line_dict[message.location.line].append(suppressed_code)59 elif message.code == "file-ignored":60 ignore_files.add(message.location.path)61 return ignore_files, ignore_messages62def get_suppressions(relative_filepaths, root, messages):63 """64 Given every message which was emitted by the tools, and the65 list of files to inspect, create a list of files to ignore,66 and a map of filepath -> line-number -> codes to ignore67 """68 paths_to_ignore = set()69 lines_to_ignore = defaultdict(set)70 messages_to_ignore = defaultdict(lambda: defaultdict(set))71 # first deal with 'noqa' style messages72 for filepath in relative_filepaths:73 abspath = os.path.join(root, filepath)74 try:75 file_contents = encoding.read_py_file(abspath).split("\n")76 except encoding.CouldNotHandleEncoding as err:77 # TODO: this output will break output formats such as JSON78 warnings.warn("{0}: {1}".format(err.path, err.cause), ImportWarning)79 continue80 ignore_file, ignore_lines = get_noqa_suppressions(file_contents)81 if ignore_file:82 paths_to_ignore.add(filepath)83 lines_to_ignore[filepath] |= ignore_lines84 # now figure out which messages were suppressed by pylint85 pylint_ignore_files, pylint_ignore_messages = _parse_pylint_informational(messages)86 paths_to_ignore |= pylint_ignore_files87 for filepath, line in pylint_ignore_messages.items():88 for line_number, codes in line.items():89 for code in codes:90 messages_to_ignore[filepath][line_number].add(("pylint", code))91 if code in _PYLINT_EQUIVALENTS:92 for equivalent in _PYLINT_EQUIVALENTS[code]:93 messages_to_ignore[filepath][line_number].add(equivalent)94 return paths_to_ignore, lines_to_ignore, messages_to_ignore

Full Screen

Full Screen

test_suppression.py

Source:test_suppression.py Github

copy

Full Screen

...7 with open(path) as testfile:8 return testfile.readlines()9 def test_ignore_file(self):10 file_contents = self._get_file_contents('test_ignore_file/test.py')11 whole_file, _ = get_noqa_suppressions(file_contents)12 self.assertTrue(whole_file)13 def test_ignore_lines(self):14 file_contents = self._get_file_contents('test_ignore_lines/test.py')15 _, lines = get_noqa_suppressions(file_contents)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run prospector automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful