Skip to content

Commit

Permalink
Merge pull request #12 from ingvaldlorentzen/django-5
Browse files Browse the repository at this point in the history
Django 5 Support
  • Loading branch information
ingvaldlorentzen committed Jan 3, 2024
2 parents 1aa4488 + df41a11 commit e37e20f
Show file tree
Hide file tree
Showing 13 changed files with 801 additions and 827 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11"]
django-version: [ "3.2", "4.0", "4.2" ] # Django 3.2 and later support only
python-version: [ "3.10", "3.11", "3.12"]
django-version: [ "4.2", "5.0" ]
steps:
- name: Check out repository
uses: actions/checkout@v2
Expand Down
22 changes: 11 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ exclude: |
)$
repos:
- repo: https://github.com/ambv/black
rev: 23.3.0
rev: 23.12.1
hooks:
- id: black
args:
- --quiet
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: end-of-file-fixer
Expand All @@ -23,31 +23,31 @@ repos:
- id: detect-private-key
- id: double-quote-string-fixer
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies:
- flake8-annotations==3.0.0 # Enforces type annotation
- flake8-bugbear==23.1.20 # Looks for likely bugs and design problems
- flake8-comprehensions==3.10.1 # Looks for unnecessary generator functions that can be converted to list comprehensions
- flake8-deprecated==2.0.1 # Looks for method deprecations
- flake8-annotations==3.0.1 # Enforces type annotation
- flake8-bugbear==23.9.16 # Looks for likely bugs and design problems
- flake8-comprehensions==3.14.0 # Looks for unnecessary generator functions that can be converted to list comprehensions
- flake8-deprecated==2.2.1 # Looks for method deprecations
- flake8-docstrings==1.7.0 # Verifies that all functions/methods have docstrings
- flake8-print==5.0.0 # Checks for print statements
- flake8-use-fstring==1.4 # Enforces use of f-strings over .format and %s
args:
- --enable-extensions=G
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
rev: v3.15.0
hooks:
- id: pyupgrade
args:
- --py38-plus
- --py310-plus
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
</p>
<p align="center">
<a href="https://python.org">
<img src="https://img.shields.io/badge/python-v3.9+-blue.svg" alt="Python version">
<img src="https://img.shields.io/badge/python-v3.10+-blue.svg" alt="Python version">
</a>
<a href="https://djangoproject.com">
<img src="https://img.shields.io/badge/django-3.1.1+%20-blue.svg" alt="Django version">
<img src="https://img.shields.io/badge/django-4.2+%20-blue.svg" alt="Django version">
</a>
<a href="https://docs.celeryproject.org/en/stable/">
<img src="https://img.shields.io/badge/celery-5.0.0+%20-blue.svg" alt="Celery version">
<img src="https://img.shields.io/badge/celery-5.3.0+%20-blue.svg" alt="Celery version">
</a>
<a href="https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/servicebus/azure-servicebus">
<img src="https://img.shields.io/badge/azure--servicebus-7.0.1+%20-blue.svg" alt="ServiceBus version">
<img src="https://img.shields.io/badge/azure--servicebus-7.10.0+%20-blue.svg" alt="ServiceBus version">
</a>
<a href="https://github.com/snok/django-guid/">
<img src="https://img.shields.io/badge/django--guid-3.2.0+-blue.svg" alt="Django GUID version">
Expand Down Expand Up @@ -51,12 +51,12 @@ This app is intended to streamline integration with Metro for all Django+Celery
* Retry failed tasks through your admin dashboard when using the `MetroidTask` base

## Overview
* `python` >= 3.8
* `django` >= 3.1.1 - For `asgiref`, settings
* `python` >= 3.10
* `django` >= 4.2 - For `asgiref`, settings
* `django-guid` >= 3.2.0 - Storing correlation IDs for failed tasks in the database, making debugging easy
* Choose one:
* `celery` >= 5.0.0 - Execute tasks based on a subject
* `rq` >= 2.4.1 - Execute tasks based on a subject
* `celery` >= 5.3.0 - Execute tasks based on a subject
* `django-rq` >= 2.4.1 - Execute tasks based on a subject

### Implementation

Expand Down
2 changes: 1 addition & 1 deletion metroid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
from metroid.celery import MetroidTask # noqa F401
from metroid.publish import publish_event # noqa F401

__version__ = '1.3.0'
__version__ = '1.4.0'
default_app_config = 'metroid.apps.MetroidConfig'
4 changes: 2 additions & 2 deletions metroid/celery.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any, Dict
from typing import Any

from billiard.einfo import ExceptionInfo
from django_guid import get_guid
Expand All @@ -11,7 +11,7 @@

class MetroidTask(Task):
def on_failure(
self, exc: Exception, task_id: str, args: tuple, kwargs: Dict[str, Any], einfo: ExceptionInfo
self, exc: Exception, task_id: str, args: tuple, kwargs: dict[str, Any], einfo: ExceptionInfo
) -> None:
"""
Custom error handler for Metro Celery tasks.
Expand Down
8 changes: 4 additions & 4 deletions metroid/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Callable, List, Optional
from collections.abc import Callable

from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -55,14 +55,14 @@ def __init__(self) -> None:
raise ImproperlyConfigured('`METROID` settings must be defined in settings.py')

@property
def subscriptions(self) -> List[Subscription]:
def subscriptions(self) -> list[Subscription]:
"""
Returns all subscriptions
"""
return self.settings.get('subscriptions', [])

@property
def publish_settings(self) -> List[TopicPublishSettings]:
def publish_settings(self) -> list[TopicPublishSettings]:
"""
Returns all publish to metro settings
"""
Expand All @@ -85,7 +85,7 @@ def get_x_metro_key(self, *, topic_name: str) -> str:
logger.critical('Unable to find a x-metro-key for %s', topic_name)
raise ImproperlyConfigured(f'No x-metro-key found for {topic_name}')

def get_handler_function(self, *, topic_name: str, subscription_name: str, subject: str) -> Optional[Callable]:
def get_handler_function(self, *, topic_name: str, subscription_name: str, subject: str) -> Callable | None:
"""
Intended to be used by retry-log.
It finds the handler function based on information we have stored in the database.
Expand Down
3 changes: 1 addition & 2 deletions metroid/management/commands/metroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import time
from asyncio.tasks import Task
from typing import List

from django.core.management.base import BaseCommand

Expand All @@ -30,7 +29,7 @@ async def start_tasks() -> None:
while True:
time.sleep(60 * 10) # Keeps CPU usage to a minimum

tasks: List[Task] = [
tasks: list[Task] = [
asyncio.create_task(
subscribe_to_topic(
connection_string=subscription['connection_string'],
Expand Down
5 changes: 2 additions & 3 deletions metroid/publish.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from typing import Optional, Union

from django.utils import timezone

Expand All @@ -15,10 +14,10 @@ def publish_event(
*,
topic_name: str,
event_type: str,
data: Union[list, dict],
data: list | dict,
subject: str,
data_version: str,
event_time: Optional[str] = None,
event_time: str | None = None,
) -> None:
"""
Sync helper function to publish metro events based on a topic name.
Expand Down
3 changes: 1 addition & 2 deletions metroid/subscribe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from typing import List

from django.utils.module_loading import import_string

Expand All @@ -19,7 +18,7 @@ async def subscribe_to_topic(
connection_string: str,
topic_name: str,
subscription_name: str,
handlers: List[Handler],
handlers: list[Handler],
) -> None:
"""
Subscribe to a topic, with a connection string
Expand Down
9 changes: 5 additions & 4 deletions metroid/typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Callable, List, Literal, TypedDict
from collections.abc import Callable
from typing import Literal, TypedDict


class Handler(TypedDict):
Expand All @@ -11,7 +12,7 @@ class Subscription(TypedDict):
topic_name: str
subscription_name: str
connection_string: str
handlers: List[Handler]
handlers: list[Handler]


class TopicPublishSettings(TypedDict):
Expand All @@ -20,6 +21,6 @@ class TopicPublishSettings(TypedDict):


class MetroidSettings(TypedDict):
subscriptions: List[Subscription]
publish_settings: List[TopicPublishSettings]
subscriptions: list[Subscription]
publish_settings: list[TopicPublishSettings]
worker_type: Literal['rq', 'celery']
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Global options

[mypy]
python_version = 3.9
python_version = 3.10
# flake8-mypy expects the two following for sensible formatting
show_column_numbers = True
show_error_context = False
Expand Down
Loading

0 comments on commit e37e20f

Please sign in to comment.