Skip to content

FEAT: type hint #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

iwakitakuma33
Copy link

@iwakitakuma33 iwakitakuma33 commented May 29, 2025

@amirreza8002
Copy link

i'm yet to take a good look at this PR
just like to mention that redis-py has wrong type hints
so it shouldn't be used as guide to type hint this package

@iwakitakuma33
Copy link
Author

iwakitakuma33 commented May 29, 2025

@amirreza8002
thanks for your review. I will do without redis-py!

best

Comment on lines +25 to +26
from redis.asyncio.connection import ConnectionPool
from redis.asyncio.client import Redis
Copy link
Author

@iwakitakuma33 iwakitakuma33 May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amirreza8002
Shouldn't I also use Redis and ConnectionPool?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those are just classes

what i mean is, if you see something like Redis().some_method(), there is a change the type hint on some_method is wrong, and we shouldn't just put the same thing in this codebase

Copy link

@amirreza8002 amirreza8002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a quick look around, not a deep review

if TYPE_CHECKING:
from redis.asyncio.connection import ConnectionPool
from redis.asyncio.client import Redis
from .core import RedisChannelLayer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this imported?
i don't see any use for it

self._lock = asyncio.Lock()
self.channel_layer = channel_layer
self._connections = {}
self._connections: "Dict[int, Redis]" = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would just import typing.Dict and go with that instead of strings
same thing for other places where this happens

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't mean to do self._connections: typing.Dict[int, "Redis"]
but rather

from typing import Dict
self._connections: Dict[int, "Redis"]

@@ -143,33 +155,33 @@ def __init__(
# Number of coroutines trying to receive right now
self.receive_count = 0
# The receive lock
self.receive_lock = None
self.receive_lock: "Optional[asyncio.Lock]" = None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.Optional would be a better
same thing for other places where this happens

)
# Detached channel cleanup tasks
self.receive_cleaners = []
self.receive_cleaners: "List[asyncio.Task]" = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.List

Comment on lines 500 to 501
assert self.require_valid_group_name(group), True
assert self.require_valid_channel_name(channel), True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this name change should be in another commit
same thing for other places where this happens

Comment on lines +591 to +596
_channels_over_capacity = -1
try:
_channels_over_capacity = float(channels_over_capacity)
except Exception:
pass
if _channels_over_capacity > 0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am not sure what's happening here
but i think it should be in another commit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection.eval is inferred to return await[str] | str.
By converting it to float, it can be compared with 0.

@@ -91,11 +95,11 @@ class RedisPubSubLoopLayer:

def __init__(
self,
hosts=None,
hosts: "Union[Iterable, str, bytes, None]" = None,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.Union

raise ValueError(
"symmetric_encryption_keys must be a list of possible keys"
)
keys = cast("Iterable[Union[str, Buffer]]", symmetric_encryption_keys)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a string?

Comment on lines -123 to +147
class MsgPackSerializer(MissingSerializer):
class _MsgPackSerializer(MissingSerializer):
exception = exc

MsgPackSerializer = _MsgPackSerializer
else:

class MsgPackSerializer(BaseMessageSerializer):
class __MsgPackSerializer(BaseMessageSerializer):
as_bytes = staticmethod(msgpack.packb)
from_bytes = staticmethod(msgpack.unpackb)

MsgPackSerializer = __MsgPackSerializer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the two names are different than each other (one has two "__")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally used a different name for avoiding PylancereportRedeclaration.

message = json.dumps(message, *args, **kwargs)
return message.encode("utf-8")

from_bytes = staticmethod(json.loads)


# code ready for a future in which msgpack may become an optional dependency
MsgPackSerializer: "Optional[Type[BaseMessageSerializer]]" = None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this is correct
also why a string?

Copy link
Author

@iwakitakuma33 iwakitakuma33 May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amirreza8002
thanks for your review.

Sorry for one commit.
I've fixed it.

@amirreza8002
Copy link

hi @bigfootjon
sorry for the ping
since django is now python >= 3.10, is there any plans to move this package to >=3.10 as well?
if so we could drop a lot of old syntax and take advantage of the newer typing supports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants