Skip to content

Commit

Permalink
switch to uv, fix typing issues (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
AiroPi authored Sep 1, 2024
2 parents f0d3233 + 37ef986 commit 2775ced
Show file tree
Hide file tree
Showing 17 changed files with 836 additions and 204 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ jobs:
with:
python-version: 3.11.0
cache: pip
- name: Install tox
run: |
python -m pip install -U pip
pip install tox
- name: Test application with tox
run: |
tox
# - name: Install tox
# run: |
# python -m pip install -U pip
# pip install tox
# - name: Test application with tox
# run: |
# tox
# - name: Upload test artifacts
# uses: actions/upload-artifact@v3
# with:
Expand Down
20 changes: 11 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# syntax=docker/dockerfile-upstream:master-labs

FROM python:3.12.0-alpine as build
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
WORKDIR /app
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# RUN python -m venv /opt/venv
# ENV PATH="/opt/venv/bin:$PATH"
COPY ./resources ./
RUN --mount=type=cache,target=/var/cache/apk/ \
--mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
--mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
--mount=type=bind,source=./bin/msgfmt.py,target=./msgfmt.py \
: \
&& apk add gcc musl-dev linux-headers \
&& pip install -U pip \
&& pip install -U -r requirements.txt \
&& uv sync --no-dev --locked \
&& python ./msgfmt.py ./locale/**/LC_MESSAGES/*.po \
&& :

FROM python:3.12.0-alpine as base
# https://docs.docker.com/reference/dockerfile/#copy---parents
COPY --parents --from=build /opt/venv /app/locale/**/LC_MESSAGES/*.mo /
COPY --parents --from=build /app/.venv /app/locale/**/LC_MESSAGES/*.mo /
WORKDIR /app
COPY ./src ./
COPY --parents ./alembic.ini ./alembic ./
ENV PATH="/opt/venv/bin:$PATH"
ENV PATH="/app/.venv/bin:$PATH"
ENV PYTHONUNBUFFERED=0


Expand All @@ -31,7 +32,8 @@ CMD ["/bin/sh", "-c", "alembic upgrade head && python ./main.py run --sync -c ./


FROM base as debug
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
ENV DEBUG=1
ENV LOG_LEVEL=DEBUG
RUN pip install debugpy
RUN uv pip install debugpy
CMD ["/bin/sh", "-c", "alembic upgrade head && python -Xfrozen_modules=off -m debugpy --wait-for-client --listen 0.0.0.0:5678 ./main.py run -c ./config.toml"]
5 changes: 5 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3'

services:
mybot:
hostname: mybot
init: true
image: airopi/mybot:stable
build:
Expand All @@ -23,6 +24,10 @@ services:
condition: service_healthy
volumes:
- ./config.toml:/app/config.toml
expose:
- 8080
ports:
- 8080:8080

database:
hostname: database
Expand Down
51 changes: 37 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
[tool.tox]
legacy_tox_ini = """
[tox]
skipsdist = true
[project]
name = "MyBot"
version = "2.0b3"
dependencies = [
"wheel",
"asyncpg",
"sqlalchemy[asyncio]",
"typing_extensions",
"discord.py",
"psutil",
"alembic",
"two048",
"topggpy==2.0.0a0",
"lingua-language-detector",
"aiohttp",
"python-dateutil",
"typer",
]
requires-python = ">=3.12"

[tool.uv]
dev-dependencies = ["tox-uv", "pyright", "pip-tools", "debugpy", "ruff"]

# [tool.tox]
# legacy_tox_ini = """
# [tox]
# skipsdist = true

[testenv]
deps =
-r requirements.txt
-r requirements.dev.txt
commands =
# pytest
ruff format --check src
ruff check src
pyright src/
"""
# [testenv]
# deps =
# -r requirements.txt
# -r requirements.dev.txt
# commands =
# # pytest
# ruff format --check src
# ruff check src
# pyright src/
# """

[tool.ruff]
line-length = 120
Expand Down
5 changes: 0 additions & 5 deletions requirements.dev.in

This file was deleted.

59 changes: 0 additions & 59 deletions requirements.dev.txt

This file was deleted.

13 changes: 0 additions & 13 deletions requirements.in

This file was deleted.

83 changes: 0 additions & 83 deletions requirements.txt

This file was deleted.

4 changes: 2 additions & 2 deletions src/cogs/game/minesweeper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def check_playable(self) -> None:
self.play.disabled = False

@ui.select(
cls=ui.Select,
cls=ui.Select[Self],
placeholder="Select the row",
options=[],
)
Expand All @@ -94,7 +94,7 @@ async def row(self, inter: Interaction, value: ui.Select[Self]) -> None:
await inter.response.edit_message(view=self)

@ui.select(
cls=ui.Select,
cls=ui.Select[Self],
placeholder="Select the column",
options=[],
)
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/poll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def callback(self, inter: Interaction, poll_type: db.PollType) -> None:
creation_date=inter.created_at,
)

poll_menu_from_type = {
poll_menu_from_type: dict[db.PollType, type[PollModal]] = {
db.PollType.CHOICE: ChoicesPollModal,
db.PollType.BOOLEAN: PollModal,
db.PollType.OPINION: PollModal,
Expand Down
11 changes: 10 additions & 1 deletion src/cogs/poll/vote_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ async def __init__(self, cog: PollCog, poll: db.Poll | None = None):
# _silent because this view is added to persistent views.
self.vote.label = _("Vote", _silent=True)

if poll is not None:
self.add_item(
ui.Button(
style=discord.ButtonStyle.url,
label=_("Results", _silent=True),
url=f"http://localhost:8000/poll/{poll.message_id}",
)
)

def get_current_votes(self, poll: db.Poll) -> dict[int, tuple[Interaction, ui.View]]:
return self.cog.current_votes.setdefault(poll.id, {})

Expand Down Expand Up @@ -98,7 +107,7 @@ async def vote(self, inter: discord.Interaction, button: ui.Button[Self]):
await current_votes[inter.user.id][0].delete_original_response()
current_votes[inter.user.id] = (inter, self)

vote_menu_types = {
vote_menu_types: dict[db.PollType, type[VoteMenu]] = {
db.PollType.CHOICE: ChoicePollVote,
db.PollType.BOOLEAN: BooleanPollVote,
db.PollType.OPINION: OpinionPollVote,
Expand Down
4 changes: 2 additions & 2 deletions src/core/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def __getattribute__(self, name: str) -> Any:

def define_config(config_path: Path | str | None = None, **kwargs: Any):
if config_path:
with open(config_path, encoding="utf-8") as f:
kwargs |= tomllib.load(f.buffer)
with open(config_path, "rb", encoding="utf-8") as f:
kwargs |= tomllib.load(f)

Config(**kwargs) # it is a singleton, so it will directly affect the instance.
Config.set_as_defined()
Expand Down
2 changes: 1 addition & 1 deletion src/core/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from sqlalchemy import select as select
from sqlalchemy import Integer as Integer, String as String, cast as cast, func as func, select as select
from sqlalchemy.orm import selectinload as selectinload

from .tables import (
Expand Down
2 changes: 1 addition & 1 deletion src/core/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PremiumType(enum.Enum):


class Base(MappedAsDataclass, AsyncAttrs, DeclarativeBase):
type_annotation_map: ClassVar = {
type_annotation_map: ClassVar[dict[Any, Any]] = {
bool: BOOLEAN,
int: INTEGER,
}
Expand Down
Loading

0 comments on commit 2775ced

Please sign in to comment.