Skip to content

Commit ac1eeb0

Browse files
committed
Update ty, more typing changes
1 parent 9c4415e commit ac1eeb0

File tree

87 files changed

+834
-488
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+834
-488
lines changed

plain-admin/plain/admin/cards/charts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def get_description(self) -> str:
4242
end = datetime_range.end.strftime("%b %d, %Y")
4343
return f"{start} to {end}"
4444

45-
def get_current_preset(self) -> DatetimeRangeAliases:
45+
def get_current_preset(self) -> str:
4646
if s := super().get_current_preset():
47-
return DatetimeRangeAliases.from_value(s)
48-
return self.default_preset
47+
return s
48+
return self.default_preset.value
4949

5050
def get_trend_data(self) -> dict[str, int]:
5151
if not self.model or not self.datetime_field:

plain-admin/plain/admin/views/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from plain.htmx.views import HTMXView
55
from plain.http import Response, ResponseRedirect
6-
from plain.models import Model
6+
from plain.models import Model, QuerySet
77
from plain.paginator import Paginator
88
from plain.views import (
99
CreateView,
@@ -104,7 +104,7 @@ def post(self) -> Response:
104104
def perform_action(self, action: str, target_ids: list) -> Response | None:
105105
raise NotImplementedError
106106

107-
def get_objects(self) -> list:
107+
def get_objects(self) -> list[Any] | QuerySet[Any]:
108108
return []
109109

110110
def get_fields(self) -> list:

plain-admin/plain/admin/views/registry.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from collections.abc import Callable
2-
from typing import Any, TypeVar
2+
from typing import TYPE_CHECKING, Any, TypeVar
33

44
from plain.urls import path, reverse_lazy
55

6+
if TYPE_CHECKING:
7+
from .viewsets import AdminViewset
8+
69
T = TypeVar("T")
10+
VS = TypeVar("VS", bound="AdminViewset")
711

812

913
class NavSection:
@@ -43,9 +47,9 @@ def inner(view: type[T]) -> type[T]:
4347
return inner
4448

4549
def register_viewset(
46-
self, viewset: type[T] | None = None
47-
) -> type[T] | Callable[[type[T]], type[T]]:
48-
def inner(viewset: type[T]) -> type[T]:
50+
self, viewset: type[VS] | None = None
51+
) -> type[VS] | Callable[[type[VS]], type[VS]]:
52+
def inner(viewset: type[VS]) -> type[VS]:
4953
for view in viewset.get_views():
5054
self.register_view(view)
5155
return viewset

plain-admin/plain/admin/views/viewsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class AdminViewset:
55
@classmethod
6-
def get_views(cls) -> list[View]:
6+
def get_views(cls) -> list[type[View]]:
77
"""Views are defined as inner classes on the viewset class."""
88

99
# Primary views that we can interlink automatically

plain-dev/plain/dev/mkcert.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class MkcertManager:
1313
def __init__(self) -> None:
14-
self.mkcert_bin = None
14+
self.mkcert_bin: str | Path | None = None
1515

1616
def setup_mkcert(self, install_path: Path) -> None:
1717
"""Set up mkcert by checking if it's installed or downloading the binary and installing the local CA."""
@@ -62,6 +62,8 @@ def setup_mkcert(self, install_path: Path) -> None:
6262

6363
def is_mkcert_ca_installed(self) -> bool:
6464
"""Check if mkcert local CA is already installed using mkcert -check."""
65+
if not self.mkcert_bin:
66+
return False
6567
try:
6668
result = subprocess.run([self.mkcert_bin, "-check"], capture_output=True)
6769
output = result.stdout.decode() + result.stderr.decode()
@@ -86,6 +88,9 @@ def generate_certs(self, domain: str, storage_path: Path) -> tuple[Path, Path]:
8688

8789
storage_path.mkdir(parents=True, exist_ok=True)
8890

91+
if not self.mkcert_bin:
92+
raise RuntimeError("mkcert is not set up. Call setup_mkcert first.")
93+
8994
click.secho(f"Generating SSL certificates for {domain}...", bold=True)
9095
subprocess.run(
9196
[

plain-dev/plain/dev/pdb.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def cry(message: str, stderr: Any = sys.__stderr__) -> None:
1818

1919

2020
class LF2CRLF_FileWrapper:
21+
_send: Any # Can be lambda or socket.sendall
22+
2123
def __init__(self, connection: socket.socket) -> None:
2224
self.connection = connection
2325
self.stream = fh = connection.makefile("rw")

plain-email/plain/email/backends/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BaseEmailBackend(ABC):
2828
def __init__(self, fail_silently: bool = False, **kwargs: Any) -> None:
2929
self.fail_silently = fail_silently
3030

31-
def open(self) -> None:
31+
def open(self) -> bool | None:
3232
"""
3333
Open a network connection.
3434

plain-email/plain/email/backends/console.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
2323
def write_message(self, message: EmailMessage) -> None:
2424
msg = message.message()
2525
msg_data = msg.as_bytes()
26-
charset = (
27-
msg.get_charset().get_output_charset() if msg.get_charset() else "utf-8"
28-
)
26+
msg_charset = msg.get_charset()
27+
if msg_charset is None:
28+
charset = "utf-8"
29+
elif isinstance(msg_charset, str):
30+
charset = msg_charset
31+
else:
32+
charset = msg_charset.get_output_charset() or "utf-8"
2933
msg_data = msg_data.decode(charset)
3034
self.stream.write(f"{msg_data}\n")
3135
self.stream.write("-" * 79)

plain-email/plain/email/backends/filebased.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717

1818
class EmailBackend(ConsoleEmailBackend):
19+
file_path: str # Set during __init__, validated to be non-None
20+
1921
def __init__(self, *args: Any, file_path: str | None = None, **kwargs: Any) -> None:
20-
self._fname = None
21-
if file_path is not None:
22-
self.file_path = file_path
23-
else:
24-
self.file_path = getattr(settings, "EMAIL_FILE_PATH", None)
25-
if not self.file_path:
22+
self._fname: str | None = None
23+
_file_path: str | None = file_path or getattr(settings, "EMAIL_FILE_PATH", None)
24+
if not _file_path:
2625
raise ImproperlyConfigured(
2726
"EMAIL_FILE_PATH must be set for the filebased email backend"
2827
)
29-
self.file_path = os.path.abspath(self.file_path)
28+
self.file_path = os.path.abspath(_file_path)
3029
try:
3130
os.makedirs(self.file_path, exist_ok=True)
3231
except FileExistsError:

plain-email/plain/email/message.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __setitem__(self, name: str, val: str) -> None:
181181
name, val = forbid_multi_line_headers(name, val, self.encoding)
182182
MIMEText.__setitem__(self, name, val)
183183

184-
def set_payload(
184+
def set_payload( # type: ignore[override]
185185
self, payload: str, charset: str | Charset.Charset | None = None
186186
) -> None:
187187
if charset == "utf-8" and not isinstance(charset, Charset.Charset):
@@ -349,11 +349,10 @@ def attach(
349349
elif content is None:
350350
raise ValueError("content must be provided.")
351351
else:
352-
mimetype = (
353-
mimetype
354-
or mimetypes.guess_type(filename)[0]
355-
or DEFAULT_ATTACHMENT_MIME_TYPE
356-
)
352+
if filename is not None and mimetype is None:
353+
mimetype = mimetypes.guess_type(filename)[0]
354+
if mimetype is None:
355+
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
357356
basetype, subtype = mimetype.split("/", 1)
358357

359358
if basetype == "text":
@@ -434,6 +433,7 @@ def _create_mime_attachment(
434433
else:
435434
# Encode non-text attachments with base64.
436435
attachment = MIMEBase(basetype, subtype)
436+
assert isinstance(content, bytes), "Non-text attachments must be bytes"
437437
attachment.set_payload(content)
438438
Encoders.encode_base64(attachment)
439439
return attachment

0 commit comments

Comments
 (0)