Skip to content

Commit 6b10bab

Browse files
Merge pull request #2616 from ElLorans/master
Fix: Correct error introduced in PR #2608
2 parents 8844bea + 060e42d commit 6b10bab

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

flask_admin/_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from flask_admin.contrib.sqla.ajax import (
6262
QueryAjaxModelLoader as T_SQLA_QUERY_AJAX_MODEL_LOADER,
6363
) # noqa
64+
from PIL.Image import Image as T_PIL_IMAGE
6465
else:
6566
T_VIEW = "flask_admin.base.BaseView"
6667
T_INPUT_REQUIRED = "InputRequired"
@@ -97,6 +98,7 @@
9798
T_SQLA_QUERY_AJAX_MODEL_LOADER = (
9899
"flask_admin.contrib.sqla.ajax.QueryAjaxModelLoader"
99100
)
101+
T_PIL_IMAGE = "PIL.Image.Image"
100102

101103
T_COLUMN = t.Union[str, T_SQLALCHEMY_COLUMN]
102104
T_FILTER = tuple[int, T_COLUMN, str]

flask_admin/form/upload.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import os.path as op
33
import typing as t
4-
from typing import TYPE_CHECKING
54
from urllib.parse import urljoin
65

76
from markupsafe import Markup
@@ -17,6 +16,7 @@
1716
from wtforms.widgets import html_params
1817

1918
from flask_admin._compat import string_types
19+
from flask_admin._types import T_PIL_IMAGE
2020
from flask_admin._types import T_VALIDATOR
2121
from flask_admin.babel import gettext
2222
from flask_admin.helpers import get_url
@@ -28,10 +28,6 @@
2828
Image = None # type:ignore[assignment]
2929
ImageOps = None # type:ignore[assignment]
3030

31-
if TYPE_CHECKING:
32-
from PIL import Image
33-
from PIL import ImageOps
34-
3531
__all__ = [
3632
"FileUploadInput",
3733
"FileUploadField",
@@ -458,7 +454,7 @@ class MyForm(BaseForm):
458454
self.thumbnail_fn = thumbgen or thumbgen_filename
459455
self.thumbnail_size = thumbnail_size
460456
self.endpoint = endpoint
461-
self.image: t.Optional[Image.Image] = None
457+
self.image: t.Optional[T_PIL_IMAGE] = None
462458
self.url_relative_path = url_relative_path
463459

464460
if not allowed_extensions:
@@ -532,7 +528,7 @@ def _save_thumbnail(self, data: FileStorage, filename: str, format: str) -> None
532528
self._resize(self.image, self.thumbnail_size), path, format
533529
)
534530

535-
def _resize(self, image: Image.Image, size: tuple[int, int, bool]) -> Image.Image:
531+
def _resize(self, image: T_PIL_IMAGE, size: tuple[int, int, bool]) -> T_PIL_IMAGE:
536532
(width, height, force) = size
537533

538534
if image.size[0] > width or image.size[1] > height:
@@ -549,7 +545,7 @@ def _resize(self, image: Image.Image, size: tuple[int, int, bool]) -> Image.Imag
549545

550546
return image
551547

552-
def _save_image(self, image: Image.Image, path: str, format: str = "JPEG") -> None:
548+
def _save_image(self, image: T_PIL_IMAGE, path: str, format: str = "JPEG") -> None:
553549
# New Pillow versions require RGB format for JPEGs
554550
if format == "JPEG" and image.mode != "RGB":
555551
image = image.convert("RGB")
@@ -559,7 +555,7 @@ def _save_image(self, image: Image.Image, path: str, format: str = "JPEG") -> No
559555
with open(path, "wb") as fp:
560556
image.save(fp, format)
561557

562-
def _get_save_format(self, filename: str, image: Image.Image) -> tuple[str, str]:
558+
def _get_save_format(self, filename: str, image: T_PIL_IMAGE) -> tuple[str, str]:
563559
if image.format not in self.keep_image_formats:
564560
name, ext = op.splitext(filename)
565561
filename = f"{name}.jpg"

0 commit comments

Comments
 (0)