py27+ syntax improvements

This commit is contained in:
Anthony Sottile 2017-01-21 11:49:53 -08:00
parent 0a93f3bfdd
commit ba75867c93
9 changed files with 31 additions and 34 deletions

View file

@ -50,8 +50,8 @@ def _update_repository(repo_config, runner):
new_repo = Repository.create(new_config, runner.store) new_repo = Repository.create(new_config, runner.store)
# See if any of our hooks were deleted with the new commits # See if any of our hooks were deleted with the new commits
hooks = set(hook_id for hook_id, _ in repo.hooks) hooks = {hook_id for hook_id, _ in repo.hooks}
hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks.keys())) hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks))
if hooks_missing: if hooks_missing:
raise RepositoryCannotBeUpdatedError( raise RepositoryCannotBeUpdatedError(
'Cannot update because the tip of master is missing these hooks:\n' 'Cannot update because the tip of master is missing these hooks:\n'

View file

@ -19,7 +19,7 @@ logger = logging.getLogger('pre_commit')
def _get_skips(environ): def _get_skips(environ):
skips = environ.get('SKIP', '') skips = environ.get('SKIP', '')
return set(skip.strip() for skip in skips.split(',') if skip.strip()) return {skip.strip() for skip in skips.split(',') if skip.strip()}
def _hook_msg_start(hook, verbose): def _hook_msg_start(hook, verbose):

View file

@ -88,7 +88,7 @@ def get_files_matching(all_file_list_strategy):
def wrapper(include_expr, exclude_expr): def wrapper(include_expr, exclude_expr):
include_regex = re.compile(include_expr) include_regex = re.compile(include_expr)
exclude_regex = re.compile(exclude_expr) exclude_regex = re.compile(exclude_expr)
return set( return {
filename filename
for filename in all_file_list_strategy() for filename in all_file_list_strategy()
if ( if (
@ -96,7 +96,7 @@ def get_files_matching(all_file_list_strategy):
not exclude_regex.search(filename) and not exclude_regex.search(filename) and
os.path.lexists(filename) os.path.lexists(filename)
) )
) }
return wrapper return wrapper

View file

@ -21,4 +21,4 @@ class Manifest(object):
@cached_property @cached_property
def hooks(self): def hooks(self):
return dict((hook['id'], hook) for hook in self.manifest_contents) return {hook['id']: hook for hook in self.manifest_contents}

View file

@ -57,10 +57,10 @@ class Repository(object):
@cached_property @cached_property
def languages(self): def languages(self):
return set( return {
(hook['language'], hook['language_version']) (hook['language'], hook['language_version'])
for _, hook in self.hooks for _, hook in self.hooks
) }
@cached_property @cached_property
def additional_dependencies(self): def additional_dependencies(self):

View file

@ -75,9 +75,9 @@ def no_git_env():
# while running pre-commit hooks in submodules. # while running pre-commit hooks in submodules.
# GIT_DIR: Causes git clone to clone wrong thing # GIT_DIR: Causes git clone to clone wrong thing
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
return dict( return {
(k, v) for k, v in os.environ.items() if not k.startswith('GIT_') k: v for k, v in os.environ.items() if not k.startswith('GIT_')
) }
@contextlib.contextmanager @contextlib.contextmanager
@ -164,10 +164,10 @@ def cmd_output(*cmd, **kwargs):
# py2/py3 on windows are more strict about the types here # py2/py3 on windows are more strict about the types here
cmd = tuple(five.n(arg) for arg in cmd) cmd = tuple(five.n(arg) for arg in cmd)
kwargs['env'] = dict( kwargs['env'] = {
(five.n(key), five.n(value)) five.n(key): five.n(value)
for key, value in kwargs.pop('env', {}).items() for key, value in kwargs.pop('env', {}).items()
) or None } or None
try: try:
cmd = parse_shebang.normalize_cmd(cmd) cmd = parse_shebang.normalize_cmd(cmd)

View file

@ -339,13 +339,13 @@ def test_compute_cols(hooks, verbose, expected):
@pytest.mark.parametrize( @pytest.mark.parametrize(
('environ', 'expected_output'), ('environ', 'expected_output'),
( (
({}, set([])), ({}, set()),
({'SKIP': ''}, set([])), ({'SKIP': ''}, set()),
({'SKIP': ','}, set([])), ({'SKIP': ','}, set()),
({'SKIP': ',foo'}, set(['foo'])), ({'SKIP': ',foo'}, {'foo'}),
({'SKIP': 'foo'}, set(['foo'])), ({'SKIP': 'foo'}, {'foo'}),
({'SKIP': 'foo,bar'}, set(['foo', 'bar'])), ({'SKIP': 'foo,bar'}, {'foo', 'bar'}),
({'SKIP': ' foo , bar'}, set(['foo', 'bar'])), ({'SKIP': ' foo , bar'}, {'foo', 'bar'}),
), ),
) )
def test_get_skips(environ, expected_output): def test_get_skips(environ, expected_output):

View file

@ -80,25 +80,22 @@ def get_files_matching_func():
def test_get_files_matching_base(get_files_matching_func): def test_get_files_matching_base(get_files_matching_func):
ret = get_files_matching_func('', '^$') ret = get_files_matching_func('', '^$')
assert ret == set([ assert ret == {
'pre_commit/main.py', 'pre_commit/main.py',
'pre_commit/git.py', 'pre_commit/git.py',
'hooks.yaml', 'hooks.yaml',
'testing/test_symlink' 'testing/test_symlink'
]) }
def test_get_files_matching_total_match(get_files_matching_func): def test_get_files_matching_total_match(get_files_matching_func):
ret = get_files_matching_func('^.*\\.py$', '^$') ret = get_files_matching_func('^.*\\.py$', '^$')
assert ret == set([ assert ret == {'pre_commit/main.py', 'pre_commit/git.py'}
'pre_commit/main.py',
'pre_commit/git.py',
])
def test_does_search_instead_of_match(get_files_matching_func): def test_does_search_instead_of_match(get_files_matching_func):
ret = get_files_matching_func('\\.yaml$', '^$') ret = get_files_matching_func('\\.yaml$', '^$')
assert ret == set(['hooks.yaml']) assert ret == {'hooks.yaml'}
def test_does_not_include_deleted_fileS(get_files_matching_func): def test_does_not_include_deleted_fileS(get_files_matching_func):
@ -108,7 +105,7 @@ def test_does_not_include_deleted_fileS(get_files_matching_func):
def test_exclude_removes_files(get_files_matching_func): def test_exclude_removes_files(get_files_matching_func):
ret = get_files_matching_func('', '\\.py$') ret = get_files_matching_func('', '\\.py$')
assert ret == set(['hooks.yaml', 'testing/test_symlink']) assert ret == {'hooks.yaml', 'testing/test_symlink'}
def resolve_conflict(): def resolve_conflict():
@ -124,12 +121,12 @@ def test_get_conflicted_files(in_merge_conflict):
cmd_output('git', 'add', 'other_file') cmd_output('git', 'add', 'other_file')
ret = set(git.get_conflicted_files()) ret = set(git.get_conflicted_files())
assert ret == set(('conflict_file', 'other_file')) assert ret == {'conflict_file', 'other_file'}
def test_get_conflicted_files_in_submodule(in_conflicting_submodule): def test_get_conflicted_files_in_submodule(in_conflicting_submodule):
resolve_conflict() resolve_conflict()
assert set(git.get_conflicted_files()) == set(('conflict_file',)) assert set(git.get_conflicted_files()) == {'conflict_file'}
def test_get_conflicted_files_unstaged_files(in_merge_conflict): def test_get_conflicted_files_unstaged_files(in_merge_conflict):
@ -142,7 +139,7 @@ def test_get_conflicted_files_unstaged_files(in_merge_conflict):
bar_only_file.write('new contents!\n') bar_only_file.write('new contents!\n')
ret = set(git.get_conflicted_files()) ret = set(git.get_conflicted_files())
assert ret == set(('conflict_file',)) assert ret == {'conflict_file'}
MERGE_MSG = "Merge branch 'foo' into bar\n\nConflicts:\n\tconflict_file\n" MERGE_MSG = "Merge branch 'foo' into bar\n\nConflicts:\n\tconflict_file\n"

View file

@ -426,7 +426,7 @@ def test_languages(tempdir_factory, store):
path = make_repo(tempdir_factory, 'python_hooks_repo') path = make_repo(tempdir_factory, 'python_hooks_repo')
config = make_config_from_repo(path) config = make_config_from_repo(path)
repo = Repository.create(config, store) repo = Repository.create(config, store)
assert repo.languages == set([('python', 'default')]) assert repo.languages == {('python', 'default')}
@pytest.mark.integration @pytest.mark.integration
@ -435,7 +435,7 @@ def test_additional_dependencies(tempdir_factory, store):
config = make_config_from_repo(path) config = make_config_from_repo(path)
config['hooks'][0]['additional_dependencies'] = ['pep8'] config['hooks'][0]['additional_dependencies'] = ['pep8']
repo = Repository.create(config, store) repo = Repository.create(config, store)
assert repo.additional_dependencies['python']['default'] == set(('pep8',)) assert repo.additional_dependencies['python']['default'] == {'pep8'}
@pytest.mark.integration @pytest.mark.integration