Skip to content

Commit

Permalink
feat!: Python 3.12 Upgrade (#4405)
Browse files Browse the repository at this point in the history
  • Loading branch information
UsamaSadiq authored Sep 9, 2024
1 parent 54216e9 commit 4a07e48
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 176 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['py38', 'py311', 'py312']
python-version: ['py312']
django-version: ['django42']
db-version: ['mysql80']
pytest-split-group: [1, 2, 3, 4, 5, 6]
Expand All @@ -23,8 +23,8 @@ jobs:
shell: bash
run: |
# Remove 'py' and insert a dot to format the version
FORMATTED_VERSION=${{ matrix.python-version }} # e.g., py38
FORMATTED_VERSION=${FORMATTED_VERSION/py3/3.} # becomes 3.8
FORMATTED_VERSION=${{ matrix.python-version }} # e.g., py312
FORMATTED_VERSION=${FORMATTED_VERSION/py3/3.} # becomes 3.12
# Set environment variables
echo "PYTHON_VERSION=$FORMATTED_VERSION" >> $GITHUB_ENV
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
- uses: actions/checkout@v3
- run: make ci_up
env:
PYTHON_VERSION: 3.8
PYTHON_VERSION: 3.12
- name: Download all artifacts
# Downloads coverage1, coverage2, etc.
uses: actions/download-artifact@v2
Expand All @@ -71,14 +71,14 @@ jobs:
- uses: actions/checkout@v3
- run: make ci_up
env:
PYTHON_VERSION: 3.8
PYTHON_VERSION: 3.12
- run: make ci_quality

semgrep:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.11', '3.12']
python-version: ['3.12']
steps:
- uses: actions/checkout@v3
- run: make ci_up
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/migrations-check-mysql8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-20.04 ]
python-version: [ 3.8 ]
python-version: [ 3.12 ]

steps:
- name: Checkout repo
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/requirements-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
python-version: [ '3.8' ]
python-version: [ '3.12' ]

steps:
- name: setup target branch
Expand Down
15 changes: 11 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM ubuntu:focal as app

ARG PYTHON_VERSION=3.8
ARG PYTHON_VERSION=3.12

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

# System requirements.
RUN apt-get update && \
apt-get install -y software-properties-common && \
Expand All @@ -14,8 +16,6 @@ RUN apt-get update && \
git \
language-pack-en \
build-essential \
python${PYTHON_VERSION}-dev \
python${PYTHON_VERSION}-distutils \
libmysqlclient-dev \
libssl-dev \
# TODO: Current version of Pillow (9.5.0) doesn't provide pre-built wheel for python 3.12,
Expand All @@ -24,7 +24,11 @@ RUN apt-get update && \
libjpeg-dev \
# mysqlclient >= 2.2.0 requires pkg-config.
pkg-config \
libcairo2-dev && \
libcairo2-dev \
python3-pip \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev \
python${PYTHON_VERSION}-distutils && \
rm -rf /var/lib/apt/lists/*

# Use UTF-8.
Expand All @@ -47,6 +51,9 @@ ENV DISCOVERY_CODE_DIR "${DISCOVERY_CODE_DIR}"
ENV DISCOVERY_APP_DIR "${DISCOVERY_APP_DIR}"
ENV PYTHON_VERSION "${PYTHON_VERSION}"

# Setup zoneinfo for Python 3.12
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION}
RUN pip install virtualenv

Expand Down
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ $(COMMON_CONSTRAINTS_TXT):
upgrade: $(COMMON_CONSTRAINTS_TXT)
sed 's/django-simple-history==3.0.0//g' requirements/common_constraints.txt > requirements/common_constraints.tmp
mv requirements/common_constraints.tmp requirements/common_constraints.txt
sed 's/Django<4.0//g' requirements/common_constraints.txt > requirements/common_constraints.tmp
mv requirements/common_constraints.tmp requirements/common_constraints.txt
pip install -q -r requirements/pip_tools.txt
pip-compile --allow-unsafe --upgrade -o requirements/pip.txt requirements/pip.in
pip-compile --upgrade -o requirements/pip_tools.txt requirements/pip_tools.in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def test_courses_with_subjects_and_negative_query(self):
start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
status=CourseRunStatus.Published,
type__is_marketable=True,
key=f'{name}/{factory.Faker("word").evaluate(None, None, {"locale":"en"})}/test',
key=f'{name}/{factory.Faker("word").evaluate(None, None, {"locale": "en"})}/test',
)
SeatFactory.create(course_run=course_run)

Expand All @@ -279,7 +279,7 @@ def test_courses_with_subjects_and_negative_query(self):
start=datetime.datetime(2015, 9, 1, tzinfo=pytz.UTC),
status=CourseRunStatus.Published,
type__is_marketable=True,
key=f'{name}/{factory.Faker("word").evaluate(None, None, {"locale":"en"})}/test',
key=f'{name}/{factory.Faker("word").evaluate(None, None, {"locale": "en"})}/test',
)
SeatFactory.create(course_run=course_run)
desired_courses.append(course_run.course)
Expand Down
29 changes: 4 additions & 25 deletions course_discovery/apps/api/v1/tests/test_views/test_search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import json
import sys
import urllib.parse
import uuid

Expand Down Expand Up @@ -69,12 +68,7 @@ def assert_successful_search(self, path=None, serializer=None):
'next': None,
}
actual = response_data['objects'] if path == self.faceted_path else response_data
if sys.version_info > (3, 9):
# Remove this pylint disable once discovery reaches python 3.11+
# pylint: disable=unsupported-binary-operation
self.assertEqual(actual, actual | expected) # pragma: no cover
else:
self.assertDictContainsSubset(expected, actual)
self.assertEqual(actual, actual | expected) # pragma: no cover

return course_run, response_data

Expand All @@ -99,12 +93,7 @@ def assert_response_includes_availability_facets(self, response_data):
},
}

if sys.version_info > (3, 9):
# Remove this pylint disable once discovery reaches python 3.11+
# pylint: disable=unsupported-binary-operation
self.assertEqual(response_data['queries'], response_data['queries'] | expected) # pragma: no cover
else:
self.assertDictContainsSubset(expected, response_data['queries'])
self.assertEqual(response_data['queries'], response_data['queries'] | expected) # pragma: no cover

@ddt.data(faceted_path, list_path, detailed_path)
def test_authentication(self, path):
Expand Down Expand Up @@ -160,12 +149,7 @@ def test_faceted_search(self):
'count': 1,
}
actual = response_data['fields']['pacing_type'][0]
if sys.version_info > (3, 9):
# Remove this pylint disable once discovery reaches python 3.11+
# pylint: disable=unsupported-binary-operation
self.assertEqual(actual, actual | expected) # pragma: no cover
else:
self.assertDictContainsSubset(expected, actual)
self.assertEqual(actual, actual | expected) # pragma: no cover

def test_invalid_query_facet(self):
""" Verify the endpoint returns HTTP 400 if an invalid facet is requested. """
Expand Down Expand Up @@ -250,12 +234,7 @@ def test_exclude_unavailable_program_types(self, path, serializer, result_locati
self.serialize_course_run_search(course_run, serializer=serializer)
]
}
if sys.version_info > (3, 9):
# Remove this pylint disable once discovery reaches python 3.11+
# pylint: disable=unsupported-binary-operation
self.assertEqual(response_data, response_data | expected) # pragma: no cover
else:
self.assertDictContainsSubset(expected, response_data)
self.assertEqual(response_data, response_data | expected) # pragma: no cover

# Check that the program is indeed the active one.
for key in result_location_keys:
Expand Down
4 changes: 2 additions & 2 deletions course_discovery/apps/course_metadata/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ def display_degrees_on_org_page(self, request, queryset):
updated = queryset.update(display_on_org_page=True)
self.message_user(
request,
f"{updated} {'degrees were' if updated>1 else 'degree was'} successfully set to display on org page.",
f"{updated} {'degrees were' if updated > 1 else 'degree was'} successfully set to display on org page.",
messages.SUCCESS,
)

Expand All @@ -886,7 +886,7 @@ def hide_degrees_on_org_page(self, request, queryset):
updated = queryset.update(display_on_org_page=False)
self.message_user(
request,
f"{updated} {'degrees were' if updated>1 else 'degree was'} successfully set to be hidden on org page.",
f"{updated} {'degrees were' if updated > 1 else 'degree was'} successfully set to be hidden on org page.",
messages.SUCCESS,
)

Expand Down
4 changes: 3 additions & 1 deletion course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2384,7 +2384,9 @@ def is_current(self):
now = datetime.datetime.now(pytz.UTC)
two_weeks = datetime.timedelta(days=14)
after_start = (not self.start) or self.start < now
ends_in_more_than_two_weeks = (not self.end) or (now.date() <= self.end.date() - two_weeks)
ends_in_more_than_two_weeks = (not self.end) or (
now.date() <= self.end.date() - two_weeks # pylint: disable=no-member
)
return after_start and ends_in_more_than_two_weeks

def is_current_and_still_upgradeable(self):
Expand Down
2 changes: 1 addition & 1 deletion course_discovery/apps/course_metadata/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ def test_get_slug_for_course__with_existing_url_slug(self):
slug, error = utils.get_slug_for_course(course)

assert error is None
slug_end_prefix = f"-{course_count+1}" if course_count else ""
slug_end_prefix = f"-{course_count + 1}" if course_count else ""
assert slug == f"learn/{subject.slug}/{organization.name}-{course.title}{slug_end_prefix}"
course.set_active_url_slug(slug)

Expand Down
1 change: 0 additions & 1 deletion course_discovery/apps/course_metadata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,6 @@ def get_salesforce_util(partner):


class HTML2TextWithLangSpans(html2text.HTML2Text):
# pylint: disable=abstract-method

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion requirements/django.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
django==4.2.15
django==4.2.16
26 changes: 10 additions & 16 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#
# This file is autogenerated by pip-compile with Python 3.8
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile --output-file=requirements/docs.txt requirements/docs.in
#
accessible-pygments==0.0.4
accessible-pygments==0.0.5
# via pydata-sphinx-theme
alabaster==0.7.13
alabaster==0.7.16
# via sphinx
babel==2.16.0
# via
Expand Down Expand Up @@ -39,8 +39,6 @@ idna==3.8
# via requests
imagesize==1.4.1
# via sphinx
importlib-metadata==8.4.0
# via sphinx
jinja2==3.1.4
# via sphinx
markupsafe==2.1.5
Expand All @@ -49,7 +47,7 @@ packaging==24.1
# via
# pydata-sphinx-theme
# sphinx
pydata-sphinx-theme==0.14.4
pydata-sphinx-theme==0.15.4
# via sphinx-book-theme
pygments==2.18.0
# via
Expand All @@ -58,8 +56,6 @@ pygments==2.18.0
# sphinx
python-dateutil==2.9.0.post0
# via elasticsearch-dsl
pytz==2024.1
# via babel
requests==2.32.3
# via sphinx
six==1.16.0
Expand All @@ -77,25 +73,23 @@ sphinx==5.3.0
# -r requirements/docs.in
# pydata-sphinx-theme
# sphinx-book-theme
sphinx-book-theme==1.0.1
sphinx-book-theme==1.1.3
# via -r requirements/docs.in
sphinxcontrib-applehelp==1.0.4
sphinxcontrib-applehelp==2.0.0
# via sphinx
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-devhelp==2.0.0
# via sphinx
sphinxcontrib-htmlhelp==2.0.1
sphinxcontrib-htmlhelp==2.1.0
# via sphinx
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-qthelp==2.0.0
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
sphinxcontrib-serializinghtml==2.0.0
# via sphinx
typing-extensions==4.12.2
# via pydata-sphinx-theme
urllib3==1.26.20
# via
# elasticsearch
# requests
zipp==3.20.1
# via importlib-metadata
Loading

0 comments on commit 4a07e48

Please sign in to comment.