mozlint package



mozlint.cli module

class mozlint.cli.MozlintParser(**kwargs)

Bases: ArgumentParser

arguments = [[['paths'], {'default': None, 'help': "Paths to file or directories to lint, like 'browser/components/loop' or 'mobile/android'. If not provided, defaults to the files changed according to --outgoing and --workdir.", 'nargs': '*'}], [['-l', '--linter'], {'action': 'append', 'default': [], 'dest': 'linters', 'help': "Linters to run, e.g 'eslint'. By default all linters are run for all the appropriate files."}], [['--list'], {'action': 'store_true', 'default': False, 'dest': 'list_linters', 'help': 'List all available linters and exit.'}], [['-W', '--warnings'], {'choices': ['soft'], 'const': True, 'dest': 'show_warnings', 'help': 'Display and fail on warnings in addition to errors. --warnings=soft can be used to report warnings but only fail on errors.', 'nargs': '?'}], [['-v', '--verbose'], {'action': 'store_true', 'default': False, 'dest': 'show_verbose', 'help': 'Enable verbose logging.'}], [['-f', '--format'], {'action': 'append', 'dest': 'formats', 'help': "Formatter to use. Defaults to 'stylish' on stdout. You can specify an optional path as --format formatter:path that will be used instead of stdout. You can also use multiple formatters at the same time. Formatters available: compact, json, stylish, summary, treeherder, unix."}], [['-n', '--no-filter'], {'action': 'store_false', 'default': True, 'dest': 'use_filters', 'help': "Ignore all filtering. This is useful for quickly testing a directory that otherwise wouldn't be run, without needing to modify the config file."}], [['--include-third-party'], {'action': 'store_true', 'default': False, 'dest': 'include_third-party', 'help': 'Also run the linter(s) on third-party code'}], [['-o', '--outgoing'], {'const': True, 'help': 'Lint files touched by commits that are not on the remote repository. Without arguments, finds the default remote that would be pushed to. The remote branch can also be specified manually. Works with mercurial or git.', 'nargs': '?'}], [['-w', '--workdir'], {'choices': ['staged', 'all'], 'const': 'all', 'help': "Lint files touched by changes in the working directory (i.e haven't been committed yet). On git, --workdir=staged can be used to only consider staged files. Works with mercurial or git.", 'nargs': '?'}], [['-r', '--rev'], {'default': None, 'help': 'Lint files touched by changes in revisions described by REV. For mercurial, it may be any revset. For git, it is a single tree-ish.', 'type': <class 'str'>}], [['--fix'], {'action': 'store_true', 'default': False, 'help': 'Fix lint errors if possible. Any errors that could not be fixed will be printed as normal.'}], [['--edit'], {'action': 'store_true', 'default': False, 'help': 'Each file containing lint errors will be opened in $EDITOR one after the other.'}], [['--setup'], {'action': 'store_true', 'default': False, 'help': 'Bootstrap linter dependencies without running any of the linters.'}], [['-j', '--jobs'], {'default': None, 'dest': 'num_procs', 'help': 'Number of worker processes to spawn when running linters. Defaults to the number of cores in your CPU.', 'type': <class 'int'>}], [['--config-path'], {'action': 'append', 'default': [], 'dest': 'config_paths', 'help': '==SUPPRESS=='}], [['--check-exclude-list'], {'action': 'store_true', 'default': False, 'dest': 'check_exclude_list', 'help': 'Run linters for all the paths in the exclude list.'}], [['extra_args'], {'help': 'Extra arguments that will be forwarded to the underlying linter.', 'nargs': '...'}]]
parse_known_args(*args, **kwargs)
mozlint.cli.find_linters(config_paths, linters=None)
mozlint.cli.get_exclude_list_output(result, paths)
mozlint.cli.main() int, linters, formats, outgoing, workdir, rev, edit, check_exclude_list, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, setupargs=None, **lintargs)

mozlint.editor module


mozlint.errors module

exception mozlint.errors.LintException

Bases: Exception

exception mozlint.errors.LinterNotFound(path)

Bases: LintException

exception mozlint.errors.LinterParseError(path, message)

Bases: LintException

exception mozlint.errors.LintersNotConfigured

Bases: LintException

exception mozlint.errors.NoValidLinter

Bases: LintException

mozlint.parser module

class mozlint.parser.Parser(root)

Bases: object

Reads and validates lint configuration files.


Read a linter and return its LINTER definition.


path – Path to the linter.


List of linter definitions ([dict])


LinterNotFound, LinterParseError

required_attributes = ('name', 'description', 'type', 'payload')

mozlint.pathutils module

class mozlint.pathutils.FilterPath(path)

Bases: object

Helper class to make comparing and matching file paths easier.


Return True if other is a subdirectory of self or equals self.

property exists
property ext
property finder
property isdir
property isfile
mozlint.pathutils.collapse(paths, base=None, dotfiles=False)

Given an iterable of paths, collapse them into the smallest possible set of paths that contain the original set (without containing any extra paths).

For example, if directory ‘a’ contains two files b.txt and c.txt, calling:

collapse([‘a/b.txt’, ‘a/c.txt’])

returns [‘a’]. But if a third file d.txt also exists, then it will return [‘a/b.txt’, ‘a/c.txt’] since [‘a’] would also include that extra file.


paths – An iterable of paths (files and directories) to collapse.


The smallest set of paths (files and directories) that contain the original set of paths and only the original set.

mozlint.pathutils.expand_exclusions(paths, config, root)

Returns all files that match patterns and aren’t excluded.

This is used by some external linters who receive ‘batch’ files (e.g dirs) but aren’t capable of applying their own exclusions. There is an argument to be made that this step should just apply to all linters no matter what.

  • paths (list) – List of candidate paths to lint.

  • config (dict) – Linter’s config object.

  • root (str) – Root of the repository.


Generator which generates list of paths that weren’t excluded.

mozlint.pathutils.filterpaths(root, paths, include, exclude=None, extensions=None, exclude_extensions=None)

Filters a list of paths.

Given a list of paths and some filtering rules, return the set of paths that should be linted. Note that at most one of extensions or exclude_extensions should be provided (ie not both).

  • paths – A starting list of paths to possibly lint.

  • include – A list of paths that should be included (required).

  • exclude – A list of paths that should be excluded (optional).

  • extensions – A list of file extensions which should be considered (optional).

  • exclude_extensions – A list of file extensions which should not be considered (optional).


A tuple containing a list of file paths to lint and a list of paths to exclude.


Find a Python object given a path of the form <modulepath>:<objectpath>. Conceptually equivalent to

def find_object(modulepath, objectpath):

import <modulepath> as mod return mod.<objectpath>

mozlint.pathutils.get_ancestors_by_name(name, path, root)

Returns a list of files called name in path’s ancestors, sorted from closest->furthest. This can be useful for finding relevant configuration files.

mozlint.result module

class mozlint.result.Issue(*, linter, path, lineno=None, column=None, message, hint=None, source=None, level=None, rule=None, lineoffset=None, diff=None)

Bases: object

Represents a single lint issue and its related metadata.

  • linter – name of the linter that flagged this error

  • path – path to the file containing the error

  • message – text describing the error

  • lineno – line number that contains the error

  • column – column containing the error

  • level – severity of the error, either ‘warning’ or ‘error’ (default ‘error’)

  • hint – suggestion for fixing the error (optional)

  • source – source code context of the error (optional)

  • rule – name of the rule that was violated (optional)

  • lineoffset – denotes an error spans multiple lines, of the form (<lineno offset>, <num lines>) (optional)

  • diff – a diff describing the changes that need to be made to the code

class mozlint.result.IssueEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: JSONEncoder

Class for encoding Issue to json.


json.dumps(results, cls=IssueEncoder)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
        iterable = iter(o)
    except TypeError:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class mozlint.result.ResultSummary(root, fail_on_warnings=True)

Bases: object

Represents overall result state from an entire lint run.

property failed

Returns true in case issues were detected during the lint run. Do not consider warning issues in case self.fail_on_warnings is set to False.

property returncode
root = None
property total_fixed
property total_issues
property total_suppressed_warnings

Merge results from another ResultSummary into this one.

mozlint.result.from_config(config, **kwargs)

Create a Issue from a linter config.

Convenience method that pulls defaults from a linter config and forwards them.

  • config – linter config as defined in a .yml file

  • kwargs – same as Issue


Issue object

mozlint.roller module

class mozlint.roller.InterruptableQueue(*args, **kwargs)

Bases: Queue

A multiprocessing.Queue that catches KeyboardInterrupt when a worker is blocking on it and returns None.

This is needed to gracefully handle KeyboardInterrupts when a worker is blocking on ProcessPoolExecutor’s call queue.

get(*args, **kwargs)
class mozlint.roller.LintRoller(root, exclude=None, setupargs=None, **lintargs)

Bases: object

Registers and runs linters.

  • root – Path to which relative paths will be joined. If unspecified, root will either be determined from version control or cwd.

  • lintargs – Arguments to pass to the underlying linter(s).


Parse one or more linters and add them to the registry.


paths – A path or iterable of paths to linter definitions.

roll(paths=None, outgoing=None, workdir=None, rev=None, num_procs=None)

Run all of the registered linters against the specified file paths.

  • paths – An iterable of files and/or directories to lint.

  • outgoing – Lint files touched by commits that are not on the remote repository.

  • workdir – Lint all files touched in the working directory.

  • num_procs – The number of processes to use. Default: cpu count


A ResultSummary instance.


Run setup for applicable linters

should_lint_entire_tree(vcs_paths: Set[str], linter: Dict) bool

Return True if the linter should be run on the entire tree.


Sometimes futures’ atexit handler can spew tracebacks. This wrapper suppresses them.

mozlint.types module

class mozlint.types.BaseType

Bases: object

Abstract base class for all types of linters.

batch = False
class mozlint.types.ExternalFileType

Bases: ExternalType

batch = False
class mozlint.types.ExternalType

Bases: BaseType

Linter type that runs an external function.

The function is responsible for properly formatting the results into a list of Issue objects.

batch = True
class mozlint.types.GlobalType

Bases: ExternalType

Linter type that runs an external global linting function just once.

The function is responsible for properly formatting the results into a list of Issue objects.

batch = True
class mozlint.types.LineType

Bases: BaseType

Abstract base class for linter types that check each line individually.

Subclasses of this linter type will read each file and check the provided payload against each line one by one.

abstract condition(line, config)
class mozlint.types.LintHandler(config)

Bases: LogHandler

class mozlint.types.RegexType

Bases: LineType

Linter type that checks whether a regex match is found.

condition(payload, line, config)
class mozlint.types.StringType

Bases: LineType

Linter type that checks whether a substring is found.

condition(payload, line, config)
class mozlint.types.StructuredLogType

Bases: BaseType

batch = True
mozlint.types.supported_types = {'external': <mozlint.types.ExternalType object>, 'external-file': <mozlint.types.ExternalFileType object>, 'global': <mozlint.types.GlobalType object>, 'regex': <mozlint.types.RegexType object>, 'string': <mozlint.types.StringType object>, 'structured_log': <mozlint.types.StructuredLogType object>}

Mapping of type string to an associated instance.

Module contents