Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, test_file):
super(LintModuleTest, self).__init__('_runTest')
test_reporter = TestReporter()
self._linter = lint.PyLinter()
self._linter.set_reporter(test_reporter)
self._linter.config.persistent = 0
checkers.initialize(self._linter)
self._linter.disable('I')
try:
self._linter.load_file_configuration(test_file.option_file)
except NoFileError:
pass
self._test_file = test_file
def patch_linter_transform():
"""Patch PyLinter class to apply message transform with source code."""
old_get_ast = PyLinter.get_ast
def new_get_ast(self, filepath, modname):
ast = old_get_ast(self, filepath, modname)
if ast is not None:
with open(filepath, encoding='utf-8') as f:
source_code = f.readlines()
ending_transformer = TransformVisitor()
register_transforms(source_code, ending_transformer)
ending_transformer.visit(ast)
return ast
PyLinter.get_ast = new_get_ast
def handle_app(self, app, **options):
name = app.__name__.rsplit('.', 1)[0]
from pylint import checkers, lint
from django_lint import AstCheckers
linter = lint.PyLinter()
linter.set_option('reports', options['report'])
"""
checkers.initialize(linter)
for msg in ('C0111', 'C0301'):
linter.disable_message(msg)
"""
# Register custom checkers
AstCheckers.register(linter)
linter.check([name, 'settings'])
def main():
arg_parser = ArgumentParser(
description='Simple extension for pylint to check django projects for '
'common mistakes.')
arg_parser.add_argument('targets', metavar='TARGET', nargs='+',
help='python package or module')
args = arg_parser.parse_args()
linter = lint.PyLinter()
reporters.initialize(linter)
linter._load_reporter()
register(linter)
with lint.fix_import_path(args.targets):
linter.check(args.targets)
return linter.msg_status
self.msgs_store = MessageDefinitionStore()
self.reporter = None
self._reporter_name = None
self._reporters = {}
self._checkers = collections.defaultdict(list)
self._pragma_lineno = {}
self._ignore_file = False
# visit variables
self.file_state = FileState()
self.current_name = None
self.current_file = None
self.stats = None
# init options
self._external_opts = options
self.options = options + PyLinter.make_options()
self.option_groups = option_groups + PyLinter.option_groups
self._options_methods = {"enable": self.enable, "disable": self.disable}
self._bw_options_methods = {
"disable-msg": self.disable,
"enable-msg": self.enable,
}
full_version = "pylint %s\nastroid %s\nPython %s" % (
version,
astroid_version,
sys.version,
)
MessagesHandlerMixIn.__init__(self)
reporters.ReportsHandlerMixIn.__init__(self)
super(PyLinter, self).__init__(
usage=__doc__,
version=full_version,
config_file=pylintrc or next(config.find_default_config_files(), None),
self._external_opts = options
self.options = options + PyLinter.make_options()
self.option_groups = option_groups + PyLinter.option_groups
self._options_methods = {"enable": self.enable, "disable": self.disable}
self._bw_options_methods = {
"disable-msg": self.disable,
"enable-msg": self.enable,
}
full_version = "pylint %s\nastroid %s\nPython %s" % (
version,
astroid_version,
sys.version,
)
MessagesHandlerMixIn.__init__(self)
reporters.ReportsHandlerMixIn.__init__(self)
super(PyLinter, self).__init__(
usage=__doc__,
version=full_version,
config_file=pylintrc or next(config.find_default_config_files(), None),
)
checkers.BaseTokenChecker.__init__(self)
# provided reports
self.reports = (
("RP0001", "Messages by category", report_total_messages_stats),
(
"RP0002",
"% errors / warnings by module",
report_messages_by_module_stats,
),
("RP0003", "Messages", report_messages_stats),
)
self.register_checker(self)
'code' : msg_id,
'data' : {
'description' : msg,
},
'location' : (((line_number,offset),(line_number,None)),),
}
issues.append(issue)
return issues
def _display(self,layout):
pass
class Linter(PyLinter):
"""
Modified version of PyLinter, which accepts a string and a filename as input and
analyzes it using the base class.
"""
def check(self,content,filename):
self._content = content
f = tempfile.NamedTemporaryFile(delete = False)
try:
with f:
f.write(content)
return super(Linter,self).check([f.name])
finally:
os.unlink(f.name)
def execute(self, finder):
packages = list(finder.packages(filters=self.config['filters']))
modules = [
mod
for mod in finder.modules(filters=self.config['filters'])
if os.path.dirname(mod) not in packages
]
targets = modules + finder.topmost_directories(packages)
if not targets:
return []
pylint = PyLinter()
pylint.load_default_plugins()
pylint.load_plugin_modules(self.config['options']['plugins'])
astroid.MANAGER.extension_package_whitelist.update(
self.config['options']['extension-pkg-whitelist'],
)
reporter = TidyPyReporter(
pylint.msgs_store,
filters=self.config['filters'],
finder=finder,
targets=targets,
)
pylint.set_reporter(reporter)
for checker in pylint.get_checkers():
self.msgs_store = MessageDefinitionStore()
self.reporter = None
self._reporter_name = None
self._reporters = {}
self._checkers = collections.defaultdict(list)
self._pragma_lineno = {}
self._ignore_file = False
# visit variables
self.file_state = FileState()
self.current_name = None
self.current_file = None
self.stats = None
# init options
self._external_opts = options
self.options = options + PyLinter.make_options()
self.option_groups = option_groups + PyLinter.option_groups
self._options_methods = {"enable": self.enable, "disable": self.disable}
self._bw_options_methods = {
"disable-msg": self.disable,
"enable-msg": self.enable,
}
full_version = "pylint %s\nastroid %s\nPython %s" % (
version,
astroid_version,
sys.version,
)
MessagesHandlerMixIn.__init__(self)
reporters.ReportsHandlerMixIn.__init__(self)
super(PyLinter, self).__init__(
usage=__doc__,
version=full_version,
config_file=pylintrc or next(config.find_default_config_files(), None),
'python_ta/checkers/invalid_range_index_checker',
# 'python_ta/checkers/always_returning_checker',
'python_ta/checkers/one_iteration_checker',
'python_ta/checkers/constant_test_checker',
'python_ta/checkers/structure_test_checker',
'python_ta/checkers/type_annotation_checker',
'python_ta/checkers/unnecessary_indexing_checker',
'python_ta/checkers/shadowing_in_comp_checker',
'python_ta/checkers/redundant_assignment_checker'
# 'python_ta/checkers/simplified_if_checker'
]
# Register new options to a checker here to allow references to
# options in `.pylintrc` config file.
# Options stored in linter: `linter._all_options`, `linter._external_opts`
linter = pylint.lint.PyLinter(options=new_checker_options)
linter.load_default_plugins() # Load checkers, reporters
linter.load_plugin_modules(custom_checkers)
if isinstance(config, str) and config != '':
# Use config file at the specified path instead of the default.
_load_config(linter, config)
else:
# If available, use config file at directory of the file being linted.
pylintrc_location = None
if file_linted:
pylintrc_location = _find_local_config(file_linted)
# Otherwise, use default config file shipped with python_ta package.
if not pylintrc_location:
pylintrc_location = _find_local_config(os.path.dirname(__file__))