Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize python default version lookup #1117

Merged
merged 5 commits into from
Aug 15, 2019
Merged

Stabilize python default version lookup #1117

merged 5 commits into from
Aug 15, 2019

Conversation

scop
Copy link
Contributor

@scop scop commented Aug 15, 2019

For example, for sys.executable:

/usr/bin/python3 -> python3.7

...the default lookup may return either python3 or python3.7. Make the order deterministic by iterating over tuple, not set, of candidates.

@scop
Copy link
Contributor Author

scop commented Aug 15, 2019

An effect of the instability is that pre-commit will end up installing dependencies several times in separate redundant virtualenvs over multiple runs, depending on which version happened to be returned.

For example, for sys.executable:
    /usr/bin/python3 -> python3.7
...the default lookup may return either python3 or python3.7. Make the
order deterministic by iterating over tuple, not set, of candidates.
@scop
Copy link
Contributor Author

scop commented Aug 15, 2019

Don't know what's up with the tests, they pass for me in local tox on Linux.

@asottile
Copy link
Member

as written, those tests require everything to be on PATH which won't be true when we test individual pythons

they pass locally for you because you have all of those pythons available

patch looks fine -- even a patch which just changes the brackets would probably be fine to merge if this is difficult to test

with mock.patch.object(sys, 'executable', exe):
with mock.patch('os.path.realpath', return_value=realpath):
assert python._find_by_sys_executable() == expected
with mock.patch('pre_commit.parse_shebang.find_executable',
Copy link
Member

Choose a reason for hiding this comment

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

please use mock.patch.object instead, mock.patch has import side-effects and can lead to some nasty patch leaking issues. mock.patch.object is also more explicit about the target

also, that feel when you don't run the pre-commit hooks while contributing to pre-commit 😆



@pytest.mark.parametrize(
'exe,realpath,expected', (
Copy link
Member

Choose a reason for hiding this comment

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

would prefer a tuple of strings here instead of stringly typed parametrize as well

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

realized I can do a few of the nitpicks as suggestions



def _get_default_version(): # pragma: no cover (platform dependent)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change



@pytest.mark.parametrize(
'exe,realpath,expected', (
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'exe,realpath,expected', (
('exe', 'realpath', 'expected'), (

@scop
Copy link
Contributor Author

scop commented Aug 15, 2019

Thanks, learned something new :) And I hope I caught all of it now.

pre_commit/languages/python.py Show resolved Hide resolved
@@ -7,6 +7,7 @@
import mock
import pytest

import pre_commit.parse_shebang
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import pre_commit.parse_shebang
from pre_commit import parse_shebang

with mock.patch.object(sys, 'executable', exe):
with mock.patch.object(os.path, 'realpath', return_value=realpath):
with mock.patch.object(
pre_commit.parse_shebang, 'find_executable',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pre_commit.parse_shebang, 'find_executable',
parse_shebang, 'find_executable',

@scop
Copy link
Contributor Author

scop commented Aug 15, 2019

Damn, still not quite there it seems :/

Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

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.

None yet

2 participants