Skip to content

Default python version per Docker image #6653

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

Merged
merged 5 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions readthedocs/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ def python_interpreter(self):
def python_full_version(self):
ver = self.python.version
if ver in [2, 3]:
# Get the highest version of the major series version if user only
# gave us a version of '2', or '3'
ver = max(
v for v in self.get_valid_python_versions()
if not isinstance(v, str) and v < ver + 1
# use default Python version if user only set '2', or '3'
return self.get_default_python_version_for_image(
self.build.image,
ver,
)
return ver

Expand Down Expand Up @@ -296,6 +295,28 @@ def get_valid_python_versions_for_image(self, build_image):
)
return settings.DOCKER_IMAGE_SETTINGS[build_image]['python']['supported_versions']

def get_default_python_version_for_image(self, build_image, python):
"""
Return the default Python version for Docker image and Py2 or Py3.

:param build_image: the Docker image complete name, already validated
(``readthedocs/build:4.0``, not just ``4.0``)
:type build_image: str

:param python: major Python version (``2`` or ``3``) to get its default
full version
:type python: int

:returns: default version for the ``DOCKER_DEFAULT_VERSION`` if not
``build_image`` found.
"""
if build_image not in settings.DOCKER_IMAGE_SETTINGS:
build_image = '{}:{}'.format(
settings.DOCKER_DEFAULT_IMAGE,
self.default_build_image,
)
return settings.DOCKER_IMAGE_SETTINGS[build_image]['python']['default_version'][python]

def as_dict(self):
config = {}
for name in self.PUBLIC_ATTRIBUTES:
Expand Down
22 changes: 22 additions & 0 deletions readthedocs/config/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,28 @@ def test_python_version_default(self):
build.validate()
assert build.python.version == 3

@pytest.mark.parametrize(
'image,default_version',
[
('1.0', 3.4),
('2.0', 3.5),
('3.0', 3.6),
('4.0', 3.7),
('5.0', 3.7),
('latest', 3.7),
('stable', 3.7),
],
)
def test_python_version_default_from_image(self, image, default_version):
build = self.get_build_config({
'build': {
'image': image,
},
})
build.validate()
assert build.python.version == int(default_version) # 2 or 3
assert build.python_full_version == default_version

@pytest.mark.parametrize('value', [2, 3])
def test_python_version_overrides_default(self, value):
build = self.get_build_config(
Expand Down
48 changes: 42 additions & 6 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,58 @@ def USE_PROMOS(self): # noqa
DOCKER_IMAGE = '{}:{}'.format(DOCKER_DEFAULT_IMAGE, DOCKER_DEFAULT_VERSION)
DOCKER_IMAGE_SETTINGS = {
'readthedocs/build:1.0': {
'python': {'supported_versions': [2, 2.7, 3, 3.4]},
'python': {
'supported_versions': [2, 2.7, 3, 3.4],
'default_version': {
2: 2.7,
3: 3.4,
},
},
},
'readthedocs/build:2.0': {
'python': {'supported_versions': [2, 2.7, 3, 3.5]},
'python': {
'supported_versions': [2, 2.7, 3, 3.5],
'default_version': {
2: 2.7,
3: 3.5,
},
},
},
'readthedocs/build:3.0': {
'python': {'supported_versions': [2, 2.7, 3, 3.3, 3.4, 3.5, 3.6]},
'python': {
'supported_versions': [2, 2.7, 3, 3.3, 3.4, 3.5, 3.6],
'default_version': {
2: 2.7,
3: 3.6,
},
},
},
'readthedocs/build:4.0': {
'python': {'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7]},
'python': {
'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7],
'default_version': {
2: 2.7,
3: 3.7,
},
},
},
'readthedocs/build:5.0': {
'python': {'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7, 'pypy3.5']},
'python': {
'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7, 'pypy3.5'],
'default_version': {
2: 2.7,
3: 3.7,
},
},
},
'readthedocs/build:6.0rc1': {
'python': {'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7, 3.8, 'pypy3.5']},
'python': {
'supported_versions': [2, 2.7, 3, 3.5, 3.6, 3.7, 3.8, 'pypy3.5'],
'default_version': {
2: 2.7,
3: 3.7,
},
},
},
}

Expand Down