Skip to content

Commit

Permalink
Use literal types for annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Nov 26, 2020
1 parent 79254ce commit cb2153d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 54 deletions.
3 changes: 1 addition & 2 deletions app/api/images.py
@@ -1,6 +1,5 @@
import asyncio
from contextlib import suppress
from typing import List

from sanic import Blueprint, response
from sanic.log import logger
Expand Down Expand Up @@ -209,7 +208,7 @@ async def text_jpg(request, template_key, text_paths):
return await render_image(request, template_key, slug, watermark, ext="jpg")


async def preview_image(request, key: str, lines: List[str], style: str):
async def preview_image(request, key: str, lines: list[str], style: str):
if "://" in key:
template = await models.Template.create(key)
else:
Expand Down
8 changes: 3 additions & 5 deletions app/helpers.py
@@ -1,5 +1,3 @@
from typing import Dict, List, Tuple

from sanic_cors import CORS
from sanic_openapi import swagger_blueprint

Expand Down Expand Up @@ -34,12 +32,12 @@ def configure(app):
app.error_handler = errors.BugsnagErrorHandler()


def get_valid_templates(request) -> List[Dict]:
def get_valid_templates(request) -> list[dict]:
templates = Template.objects.filter(valid=True, _exclude="_custom")
return [template.jsonify(request.app) for template in sorted(templates)]


def get_example_images(request) -> List[Tuple[str, str]]:
def get_example_images(request) -> list[tuple[str, str]]:
templates = Template.objects.filter(valid=True, _exclude="_custom")
return [
(
Expand All @@ -50,7 +48,7 @@ def get_example_images(request) -> List[Tuple[str, str]]:
]


def get_test_images(request) -> List[str]:
def get_test_images(request) -> list[str]:
return [
request.app.url_for(
f"images.text_{settings.DEFAULT_EXT}",
Expand Down
12 changes: 6 additions & 6 deletions app/models.py
@@ -1,7 +1,7 @@
import hashlib
import shutil
from pathlib import Path
from typing import Dict, List, Optional
from typing import Optional
from urllib.parse import urlparse

import aiofiles
Expand Down Expand Up @@ -62,11 +62,11 @@ class Template:
key: str
name: str = ""
source: Optional[str] = None
text: List[Text] = field(
text: list[Text] = field(
default_factory=lambda: [Text(), Text(anchor_x=0.0, anchor_y=0.8)]
)
styles: List[str] = field(default_factory=lambda: [])
example: List[str] = field(default_factory=lambda: ["your text", "goes here"])
styles: list[str] = field(default_factory=lambda: [])
example: list[str] = field(default_factory=lambda: ["your text", "goes here"])

def __str__(self):
return str(self.directory)
Expand Down Expand Up @@ -117,7 +117,7 @@ def get_image(self, style: str = "") -> Path:
logger.warning(f"Style {style!r} not available for {self.key}")
return self.get_image()

def jsonify(self, app: Sanic) -> Dict:
def jsonify(self, app: Sanic) -> dict:
return {
"name": self.name,
"key": self.key,
Expand Down Expand Up @@ -163,7 +163,7 @@ def build_example_url(
def build_custom_url(
self,
app: Sanic,
text_lines: List[str],
text_lines: list[str],
*,
extension: str = "",
background: str = "",
Expand Down
8 changes: 3 additions & 5 deletions app/types.py
@@ -1,5 +1,3 @@
from typing import Tuple

Dimensions = Tuple[int, int]
Point = Tuple[int, int]
Offset = Tuple[int, int]
Dimensions = tuple[int, int]
Point = tuple[int, int]
Offset = tuple[int, int]
16 changes: 8 additions & 8 deletions app/utils/images.py
Expand Up @@ -3,7 +3,7 @@
import hashlib
import io
from pathlib import Path
from typing import TYPE_CHECKING, Iterator, List, Optional, Tuple
from typing import TYPE_CHECKING, Iterator, Optional

from PIL import Image, ImageDraw, ImageFilter, ImageFont, ImageOps
from sanic.log import logger
Expand All @@ -18,10 +18,10 @@

def preview(
template: Template,
lines: List[str],
lines: list[str],
*,
style: str = settings.DEFAULT_STYLE,
) -> Tuple[bytes, str]:
) -> tuple[bytes, str]:
image = render_image(template, style, lines, settings.PREVIEW_SIZE, pad=False)
stream = io.BytesIO()
image.save(stream, format="JPEG", quality=50)
Expand All @@ -30,7 +30,7 @@ def preview(

def save(
template: Template,
lines: List[str],
lines: list[str],
watermark: str = "",
*,
ext: str = settings.DEFAULT_EXT,
Expand Down Expand Up @@ -70,7 +70,7 @@ def load(path: Path) -> Image:
def render_image(
template: Template,
style: str,
lines: List[str],
lines: list[str],
size: Dimensions,
*,
pad: Optional[bool] = None,
Expand Down Expand Up @@ -152,7 +152,7 @@ def resize_image(image: Image, width: int, height: int, pad: bool) -> Image:
return image


def fit_image(width: float, height: float) -> Tuple[int, int]:
def fit_image(width: float, height: float) -> tuple[int, int]:
while width * height > settings.MAXIMUM_PIXELS:
width *= 0.75
height *= 0.75
Expand Down Expand Up @@ -206,8 +206,8 @@ def add_watermark(image: Image, text: str) -> Image:


def get_image_elements(
template: Template, lines: List[str], image_size: Dimensions
) -> Iterator[Tuple[Point, Offset, str, Dimensions, str, int, int, str, float]]:
template: Template, lines: list[str], image_size: Dimensions
) -> Iterator[tuple[Point, Offset, str, Dimensions, str, int, int, str, float]]:
for index, text in enumerate(template.text):
point = text.get_anchor(image_size)

Expand Down
3 changes: 1 addition & 2 deletions app/utils/meta.py
@@ -1,4 +1,3 @@
from typing import Tuple
from urllib.parse import unquote

import aiohttp
Expand All @@ -7,7 +6,7 @@
from .. import settings


def get_watermark(request, watermark: str) -> Tuple[str, bool]:
def get_watermark(request, watermark: str) -> tuple[str, bool]:
updated = False

if watermark == "none":
Expand Down
9 changes: 3 additions & 6 deletions app/utils/text.py
@@ -1,7 +1,4 @@
from typing import List, Tuple


def encode(lines: List[str]) -> str:
def encode(lines: list[str]) -> str:
encoded_lines = []

for line in lines:
Expand Down Expand Up @@ -30,7 +27,7 @@ def encode(lines: List[str]) -> str:
return slug or "_"


def decode(slug: str) -> List[str]:
def decode(slug: str) -> list[str]:
has_arrow = "_-->" in slug

slug = slug.replace("_", " ").replace(" ", "_")
Expand All @@ -56,6 +53,6 @@ def decode(slug: str) -> List[str]:
return lines


def normalize(slug: str) -> Tuple[str, bool]:
def normalize(slug: str) -> tuple[str, bool]:
normalized_slug = encode(decode(slug))
return normalized_slug, slug != normalized_slug
28 changes: 10 additions & 18 deletions poetry.lock

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

4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -43,7 +43,7 @@ isort = "=5.5.1"
black = "=20.8b1"

# Type Checking
mypy = "*"
mypy = { git = "https://github.com/python/mypy", rev = "52225ab91703a7283c9e4bd129402a4e43abdb66" }
autoflake = "^1.3.1"

# Testing
Expand All @@ -66,7 +66,7 @@ mkdocs = "^1.1"
honcho = "^1.0"
ipdb = "*"
rope = "^0.14.0"
watchdog = { version = "*", extras = ["watchmedo"] }
watchdog = { version = "=0.10.3", extras = ["watchmedo"] }

[tool.black]

Expand Down

0 comments on commit cb2153d

Please sign in to comment.