Skip to content

Commit

Permalink
Upgrade to Python 3.12
Browse files Browse the repository at this point in the history
This commit generated by first editing places we hardcode Python:

- `.tool-versions`
- `pyproject.toml`
- `Dockerfile`

Then updating the source code accordingly:

    poetry lock
    ruff check --fix .

Notably we still run Python 3.10 in production, a bit annoying since I
*still* don't deploy using a `Dockerfile`. We can either edit the Python
version that we install in prod (which runs on Ubuntu 22.04 EC2
instances) *or* (better) we can migrate to containerized deployments.

Latest Ubuntu nudges us towards pipx
====================================
A plain `pip install poetry` no longer works:

    This environment is externally managed
    To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
  • Loading branch information
DavidCain committed Jun 23, 2024
1 parent 7dcd036 commit 139f808
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python 3.10.14
python 3.12.4
poetry 1.8.3
nodejs 16.20.2
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# TODO:
# - Consider python:3.10-slim
# - Consider python:3.12-slim
# - Once dropping legacy AngularJS, build FE bundles separately

# Things needed to use this in production:
# - Direct logs to files outside the container
# - Ensure that Celery works as well
# - Run WSGI with an ENTRYPOINT

FROM ubuntu:22.04 AS build
FROM ubuntu:24.04 AS build

WORKDIR /app/

Expand All @@ -22,7 +22,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
locales \
# Postgres client (for accessing RDS in production) \
postgresql-client postgresql-contrib libpq-dev \
python3.10 python3-dev python3-pip \
python3.12 python3-dev python3-pip pipx \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -40,12 +40,13 @@ ENV LANGUAGE=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8

RUN pip3 install --upgrade pip==24.1

COPY .tool-versions .
RUN pip install poetry==$(awk '/poetry/{printf $2}' .tool-versions)
RUN pipx install poetry==$(awk '/poetry/{printf $2}' .tool-versions)
COPY poetry.lock .

# pipx puts the poetry binary here
ENV PATH="/root/.local/bin:$PATH"

# Instruct poetry to create a venv in `.venv`, then auto-add it to path
RUN poetry config virtualenvs.in-project true
ENV PATH="/app/.venv/bin:$PATH"
Expand Down
55 changes: 5 additions & 50 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ ignore-names = [
# When poetry full supports PEP 621, should migrate more information here!
# See: https://github.com/python-poetry/roadmap/issues/3
name = "ws"
requires-python = "~=3.10" # Keep in sync with Poetry
requires-python = "~=3.12" # Keep in sync with Poetry

[tool.poetry]
name = "ws"
Expand All @@ -147,7 +147,7 @@ repository = "https://github.com/DavidCain/mitoc-trips/"
authors = ["David Cain <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.10.0"
python = "^3.12.0"

[tool.poetry.group.prod.dependencies]
Django = "^4.2"
Expand All @@ -171,7 +171,6 @@ pwned-passwords-django = "*"
requests = "*"
sentry-sdk = "^1.30.0"
setuptools = "^69.5.1" # Required by django-pipeline in prod!
typing-extensions = "^4.11.0" # Can remove after Python 3.11


[tool.poetry.group.test.dependencies]
Expand Down Expand Up @@ -209,7 +208,7 @@ ipdb = { version = "*" }


[tool.mypy]
python_version = "3.10"
python_version = "3.12"
plugins = ["mypy_django_plugin.main"]

# Better errors
Expand Down
3 changes: 1 addition & 2 deletions ws/api_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from collections.abc import Collection, Iterable, Iterator
from datetime import date
from typing import Any, TypedDict, cast
from typing import Any, NotRequired, TypedDict, cast
from zoneinfo import ZoneInfo

import jwt
Expand All @@ -18,7 +18,6 @@
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DetailView, ListView, View
from django.views.generic.detail import SingleObjectMixin
from typing_extensions import NotRequired

import ws.utils.geardb as geardb_utils
import ws.utils.membership as membership_utils
Expand Down
3 changes: 1 addition & 2 deletions ws/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from collections.abc import Collection, Iterable, Iterator
from datetime import date, datetime, timedelta
from typing import Any, Optional, cast
from typing import Any, Optional, Self, cast
from urllib.parse import urlencode, urljoin
from zoneinfo import ZoneInfo

Expand All @@ -24,7 +24,6 @@
from mitoc_const import affiliations
from mitoc_const.membership import RENEWAL_ALLOWED_WITH_DAYS_LEFT
from phonenumber_field.modelfields import PhoneNumberField
from typing_extensions import Self

import ws.utils.dates as date_utils
from ws import enums
Expand Down
4 changes: 2 additions & 2 deletions ws/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextlib
import logging
from collections.abc import Iterator
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from time import monotonic

import requests
Expand Down Expand Up @@ -150,7 +150,7 @@ def update_member_stats(
) -> models.MembershipStats:
cached = models.MembershipStats.load()
acceptable_staleness = timedelta(seconds=acceptable_staleness_seconds)
now = datetime.now(timezone.utc)
now = datetime.now(UTC)
if (now - cached.retrieved_at) > acceptable_staleness or not cached.response:
response = geardb.query_api("/api-auth/v1/stats")

Expand Down
3 changes: 1 addition & 2 deletions ws/utils/member_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import enum
import logging
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, NamedTuple
from typing import TYPE_CHECKING, Any, NamedTuple, assert_never

from allauth.account.models import EmailAddress
from django.db.models import Count, Exists, OuterRef
from django.db.models.functions import Lower
from mitoc_const import affiliations
from typing_extensions import assert_never

from ws import models, tasks

Expand Down

0 comments on commit 139f808

Please sign in to comment.