From ef0769a5c5885ddbb9f732651a4b9f39c4bc06ff Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:14:13 +0100 Subject: [PATCH 1/5] Update type hints of `core.signing` --- django-stubs/core/signing.pyi | 22 +++++++++++++++------- scripts/stubtest/allowlist_todo.txt | 6 ------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/django-stubs/core/signing.pyi b/django-stubs/core/signing.pyi index dba7aa20d..6757737a5 100644 --- a/django-stubs/core/signing.pyi +++ b/django-stubs/core/signing.pyi @@ -1,12 +1,16 @@ from datetime import timedelta from typing import Any, Protocol, type_check_only +BASE62_ALPHABET: str + class BadSignature(Exception): ... class SignatureExpired(BadSignature): ... +def b62_encode(s: int) -> str: ... +def b62_decode(s: str) -> int: ... def b64_encode(s: bytes) -> bytes: ... def b64_decode(s: bytes) -> bytes: ... -def base64_hmac(salt: str, value: bytes | str, key: bytes | str, algorithm: str = ...) -> str: ... +def base64_hmac(salt: bytes | str, value: bytes | str, key: bytes | str, algorithm: str = ...) -> str: ... def get_cookie_signer(salt: str = ...) -> TimestampSigner: ... @type_check_only class Serializer(Protocol): @@ -20,31 +24,35 @@ class JSONSerializer: def dumps( obj: Any, key: bytes | str | None = ..., - salt: str = ..., + salt: bytes | str = ..., serializer: type[Serializer] = ..., compress: bool = ..., ) -> str: ... def loads( s: str, key: bytes | str | None = ..., - salt: str = ..., + salt: bytes | str = ..., serializer: type[Serializer] = ..., max_age: int | timedelta | None = ..., + fallback_keys: list[str | bytes] | None = ..., ) -> Any: ... class Signer: - key: str + key: bytes | str + fallback_keys: list[bytes | str] sep: str - salt: str + salt: bytes | str algorithm: str def __init__( self, + *, key: bytes | str | None = ..., sep: str = ..., - salt: str | None = ..., + salt: bytes | str | None = ..., algorithm: str | None = ..., + fallback_keys: list[bytes | str] | None = ..., ) -> None: ... - def signature(self, value: bytes | str) -> str: ... + def signature(self, value: bytes | str, key: bytes | str | None = ...) -> str: ... def sign(self, value: str) -> str: ... def unsign(self, signed_value: str) -> str: ... def sign_object( diff --git a/scripts/stubtest/allowlist_todo.txt b/scripts/stubtest/allowlist_todo.txt index 72a8467b5..18ee516c0 100644 --- a/scripts/stubtest/allowlist_todo.txt +++ b/scripts/stubtest/allowlist_todo.txt @@ -754,12 +754,6 @@ django.core.management.utils.run_formatters django.core.management.utils.sentinel django.core.servers.basehttp.ServerHandler.__init__ django.core.servers.basehttp.ThreadedWSGIServer.__init__ -django.core.signing.BASE62_ALPHABET -django.core.signing.Signer.__init__ -django.core.signing.Signer.signature -django.core.signing.b62_decode -django.core.signing.b62_encode -django.core.signing.loads django.db.backends.ddl_references.Expressions django.db.backends.mysql.base django.db.backends.mysql.features.DatabaseFeatures.can_rename_index From dd57c67a6d65a4c00c41851df2522a55452e74f4 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 14 Feb 2024 12:46:45 +0100 Subject: [PATCH 2/5] Fix type hint of args --- django-stubs/core/signing.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/django-stubs/core/signing.pyi b/django-stubs/core/signing.pyi index 6757737a5..5b5cf0819 100644 --- a/django-stubs/core/signing.pyi +++ b/django-stubs/core/signing.pyi @@ -1,5 +1,6 @@ from datetime import timedelta from typing import Any, Protocol, type_check_only +from typing_extensions import Unpack BASE62_ALPHABET: str @@ -45,7 +46,7 @@ class Signer: algorithm: str def __init__( self, - *, + *args: Unpack[tuple[()]], key: bytes | str | None = ..., sep: str = ..., salt: bytes | str | None = ..., From cb119cf901fbc1f574af1083e73f094a9c6edcfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:47:55 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- django-stubs/core/signing.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/django-stubs/core/signing.pyi b/django-stubs/core/signing.pyi index 5b5cf0819..278190670 100644 --- a/django-stubs/core/signing.pyi +++ b/django-stubs/core/signing.pyi @@ -1,5 +1,6 @@ from datetime import timedelta from typing import Any, Protocol, type_check_only + from typing_extensions import Unpack BASE62_ALPHABET: str From 56f66f95b6c22f54c19f4b14207406e5e05d8172 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:30:17 +0100 Subject: [PATCH 4/5] Use Any --- django-stubs/core/signing.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/django-stubs/core/signing.pyi b/django-stubs/core/signing.pyi index 278190670..ec61befbb 100644 --- a/django-stubs/core/signing.pyi +++ b/django-stubs/core/signing.pyi @@ -1,8 +1,6 @@ from datetime import timedelta from typing import Any, Protocol, type_check_only -from typing_extensions import Unpack - BASE62_ALPHABET: str class BadSignature(Exception): ... @@ -47,7 +45,7 @@ class Signer: algorithm: str def __init__( self, - *args: Unpack[tuple[()]], + *args: Any, key: bytes | str | None = ..., sep: str = ..., salt: bytes | str | None = ..., From 1a96af738ca75f3d18d631f67105ac302e13aa2c Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:54:55 +0100 Subject: [PATCH 5/5] Try using deprecated --- django-stubs/core/signing.pyi | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/django-stubs/core/signing.pyi b/django-stubs/core/signing.pyi index ec61befbb..494437d6e 100644 --- a/django-stubs/core/signing.pyi +++ b/django-stubs/core/signing.pyi @@ -1,5 +1,7 @@ from datetime import timedelta -from typing import Any, Protocol, type_check_only +from typing import Any, Protocol, overload, type_check_only + +from typing_extensions import deprecated BASE62_ALPHABET: str @@ -43,6 +45,18 @@ class Signer: sep: str salt: bytes | str algorithm: str + @overload + def __init__( + self, + *, + key: bytes | str | None = ..., + sep: str = ..., + salt: bytes | str | None = ..., + algorithm: str | None = ..., + fallback_keys: list[bytes | str] | None = ..., + ) -> None: ... + @overload + @deprecated("Passing positional arguments to Signer is deprecated and will be removed in Django 5.1") def __init__( self, *args: Any,