From ba75867c93a956293b8c880f998f6f684da5d256 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 21 Jan 2017 11:49:53 -0800 Subject: [PATCH] py27+ syntax improvements --- pre_commit/commands/autoupdate.py | 4 ++-- pre_commit/commands/run.py | 2 +- pre_commit/git.py | 4 ++-- pre_commit/manifest.py | 2 +- pre_commit/repository.py | 4 ++-- pre_commit/util.py | 12 ++++++------ tests/commands/run_test.py | 14 +++++++------- tests/git_test.py | 19 ++++++++----------- tests/repository_test.py | 4 ++-- 9 files changed, 31 insertions(+), 34 deletions(-) diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index 714dfd97..621f7156 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -50,8 +50,8 @@ def _update_repository(repo_config, runner): new_repo = Repository.create(new_config, runner.store) # See if any of our hooks were deleted with the new commits - hooks = set(hook_id for hook_id, _ in repo.hooks) - hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks.keys())) + hooks = {hook_id for hook_id, _ in repo.hooks} + hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks)) if hooks_missing: raise RepositoryCannotBeUpdatedError( 'Cannot update because the tip of master is missing these hooks:\n' diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 67d0b778..6d3851f0 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -19,7 +19,7 @@ logger = logging.getLogger('pre_commit') def _get_skips(environ): 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): diff --git a/pre_commit/git.py b/pre_commit/git.py index ab980181..d3946c5b 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -88,7 +88,7 @@ def get_files_matching(all_file_list_strategy): def wrapper(include_expr, exclude_expr): include_regex = re.compile(include_expr) exclude_regex = re.compile(exclude_expr) - return set( + return { filename for filename in all_file_list_strategy() if ( @@ -96,7 +96,7 @@ def get_files_matching(all_file_list_strategy): not exclude_regex.search(filename) and os.path.lexists(filename) ) - ) + } return wrapper diff --git a/pre_commit/manifest.py b/pre_commit/manifest.py index 0738e5d4..8a1c25f5 100644 --- a/pre_commit/manifest.py +++ b/pre_commit/manifest.py @@ -21,4 +21,4 @@ class Manifest(object): @cached_property def hooks(self): - return dict((hook['id'], hook) for hook in self.manifest_contents) + return {hook['id']: hook for hook in self.manifest_contents} diff --git a/pre_commit/repository.py b/pre_commit/repository.py index f48f431f..129872c0 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -57,10 +57,10 @@ class Repository(object): @cached_property def languages(self): - return set( + return { (hook['language'], hook['language_version']) for _, hook in self.hooks - ) + } @cached_property def additional_dependencies(self): diff --git a/pre_commit/util.py b/pre_commit/util.py index dc8e4781..9cf4c164 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -75,9 +75,9 @@ def no_git_env(): # while running pre-commit hooks in submodules. # GIT_DIR: Causes git clone to clone wrong thing # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit - return dict( - (k, v) for k, v in os.environ.items() if not k.startswith('GIT_') - ) + return { + k: v for k, v in os.environ.items() if not k.startswith('GIT_') + } @contextlib.contextmanager @@ -164,10 +164,10 @@ def cmd_output(*cmd, **kwargs): # py2/py3 on windows are more strict about the types here cmd = tuple(five.n(arg) for arg in cmd) - kwargs['env'] = dict( - (five.n(key), five.n(value)) + kwargs['env'] = { + five.n(key): five.n(value) for key, value in kwargs.pop('env', {}).items() - ) or None + } or None try: cmd = parse_shebang.normalize_cmd(cmd) diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index 86d0ecd4..a24ffbdd 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -339,13 +339,13 @@ def test_compute_cols(hooks, verbose, expected): @pytest.mark.parametrize( ('environ', 'expected_output'), ( - ({}, set([])), - ({'SKIP': ''}, set([])), - ({'SKIP': ','}, set([])), - ({'SKIP': ',foo'}, set(['foo'])), - ({'SKIP': 'foo'}, set(['foo'])), - ({'SKIP': 'foo,bar'}, set(['foo', 'bar'])), - ({'SKIP': ' foo , bar'}, set(['foo', 'bar'])), + ({}, set()), + ({'SKIP': ''}, set()), + ({'SKIP': ','}, set()), + ({'SKIP': ',foo'}, {'foo'}), + ({'SKIP': 'foo'}, {'foo'}), + ({'SKIP': 'foo,bar'}, {'foo', 'bar'}), + ({'SKIP': ' foo , bar'}, {'foo', 'bar'}), ), ) def test_get_skips(environ, expected_output): diff --git a/tests/git_test.py b/tests/git_test.py index 701d36b4..95b9df39 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -80,25 +80,22 @@ def get_files_matching_func(): def test_get_files_matching_base(get_files_matching_func): ret = get_files_matching_func('', '^$') - assert ret == set([ + assert ret == { 'pre_commit/main.py', 'pre_commit/git.py', 'hooks.yaml', 'testing/test_symlink' - ]) + } def test_get_files_matching_total_match(get_files_matching_func): ret = get_files_matching_func('^.*\\.py$', '^$') - assert ret == set([ - 'pre_commit/main.py', - 'pre_commit/git.py', - ]) + assert ret == {'pre_commit/main.py', 'pre_commit/git.py'} def test_does_search_instead_of_match(get_files_matching_func): 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): @@ -108,7 +105,7 @@ def test_does_not_include_deleted_fileS(get_files_matching_func): def test_exclude_removes_files(get_files_matching_func): ret = get_files_matching_func('', '\\.py$') - assert ret == set(['hooks.yaml', 'testing/test_symlink']) + assert ret == {'hooks.yaml', 'testing/test_symlink'} def resolve_conflict(): @@ -124,12 +121,12 @@ def test_get_conflicted_files(in_merge_conflict): cmd_output('git', 'add', 'other_file') 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): 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): @@ -142,7 +139,7 @@ def test_get_conflicted_files_unstaged_files(in_merge_conflict): bar_only_file.write('new contents!\n') 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" diff --git a/tests/repository_test.py b/tests/repository_test.py index e37b304d..a725934f 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -426,7 +426,7 @@ def test_languages(tempdir_factory, store): path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path) repo = Repository.create(config, store) - assert repo.languages == set([('python', 'default')]) + assert repo.languages == {('python', 'default')} @pytest.mark.integration @@ -435,7 +435,7 @@ def test_additional_dependencies(tempdir_factory, store): config = make_config_from_repo(path) config['hooks'][0]['additional_dependencies'] = ['pep8'] repo = Repository.create(config, store) - assert repo.additional_dependencies['python']['default'] == set(('pep8',)) + assert repo.additional_dependencies['python']['default'] == {'pep8'} @pytest.mark.integration