Keep dependency in order by tdeo · Pull Request #482 · pre-commit/pre-commit · GitHub
Skip to content

Keep dependency in order - #482

Merged
asottile merged 1 commit into
pre-commit:masterfrom
tdeo:keep_dependencies_order
Feb 9, 2017
Merged

asottile merged 1 commit into
pre-commit:masterfrom
tdeo:keep_dependencies_order

Conversation

@tdeo

@tdeo tdeo commented Feb 9, 2017

Copy link
Copy Markdown
Contributor

I was investigating pre-commit/pre-commit-mirror-maker#20 and realized running gem install gem1:version1 gem2:version can give different result from gem install gem2:version2 gem1:version1.

This PR keeps the list of additional dependencies of an hook in the order they appeared in the .pre-commit-config.yaml.

Comment thread pre_commit/repository.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we do lose set semantics here, can you add a test with multiple hooks that have the same additional dependencies and verify that there are not duplicates?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the current set implementation is already accepting the same dependency listed at different versions. We could keep the same behaviour by handling duplicates manually:

for dep in hook.get('additional_dependencies', []):
    if dep not in dep_dict[hook['language']][hook['language_version']]:
        dep_dict[hook['language']][hook['language_version']].append(dep)

An other option would be to extract the name and the version for each dependency (needs to be implemented for each language though) and then either:

  • keep only the first version of each package
  • issue a warning to the user
  • raise an exception

and then implement tests accordingly, what would you prefer?

@asottile asottile Feb 9, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The former is fine, I don't want to add language-specific parsing of this stuff if avoidable.

Heh, if we could only target python3.6 (or modern pypy), the set would have insertion order

Comment thread pre_commit/repository.py Outdated
logger.warning(
'Additional dependency {} is listed twice for hook '
'{}'.format(dep, hook['name'])
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A warning should not be raised here.

This is a perfectly valid configuration:

    hooks:
    -   id: puppet-validate
        language_version: '2.1.5'
        additional_dependencies: ['puppet:4.5.3']
    -   id: epp-validate
        language_version: '2.1.5'
        additional_dependencies: ['puppet:4.5.3']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can keep the code change smaller by making something like this:

class _UniqueList(list):
    def __init__(self):
        self._set = set()

    def update(self, obj):
        for item in obj:
            if item not in self._set:
                self.append(item)

And then the diff is just

-        dep_dict = defaultdict(lambda: defaultdict(set))
+        dep_dict = defaultdict(lambda: defaultdict(_UniqueList))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Let me know if you want me to squash commits

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you!

@tdeo

tdeo commented Feb 9, 2017

Copy link
Copy Markdown
Contributor Author

I squashed the commit. Everything allright?

@asottile
asottile merged commit 63f65a4 into pre-commit:master Feb 9, 2017
@asottile

asottile commented Feb 9, 2017

Copy link
Copy Markdown
Member

Yep! I was waiting for the build and didn't notice it in my 50 tabs 🤣

Thanks for the contribution!

@tdeo
tdeo deleted the keep_dependencies_order branch February 9, 2017 20:52
@tdeo

tdeo commented Feb 9, 2017

Copy link
Copy Markdown
Contributor Author

Thanks for reactivity!

@asottile

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants