-
-
Notifications
You must be signed in to change notification settings - Fork 928
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
feature: Adding type annotations to Connections #1612
base: main
Are you sure you want to change the base?
Changes from all commits
f763078
58804fc
716d9c0
c6d006b
d2edc8d
d1fdaee
29404ee
ec39644
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import os | ||
from itertools import chain | ||
|
||
from .connection import Resource | ||
from .connection import Connection, ConnectionPool, Resource | ||
from .messaging import Producer | ||
from .utils.collections import EqualityDict | ||
from .utils.compat import register_after_fork | ||
|
@@ -78,13 +78,13 @@ def release(self, resource): | |
class PoolGroup(EqualityDict): | ||
"""Collection of resource pools.""" | ||
|
||
def __init__(self, limit=None, close_after_fork=True): | ||
def __init__(self, limit: int | None = None, close_after_fork: bool = True): | ||
self.limit = limit | ||
self.close_after_fork = close_after_fork | ||
if self.close_after_fork and register_after_fork is not None: | ||
register_after_fork(self, _after_fork_cleanup_group) | ||
|
||
def create(self, resource, limit): | ||
def create(self, resource, limit: int | None): | ||
raise NotImplementedError('PoolGroups must define ``create``') | ||
|
||
def __missing__(self, resource): | ||
|
@@ -104,9 +104,18 @@ def register_group(group): | |
class Connections(PoolGroup): | ||
"""Collection of connection pools.""" | ||
|
||
def create(self, connection, limit): | ||
def create(self, connection: Connection, limit: int | None): | ||
return connection.Pool(limit=limit) | ||
|
||
def __getitem__(self, connection: Connection) -> ConnectionPool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to know, what this dunder methods are going to achieve? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made it very clear Code editors such as vscode, pycharm, etc. cannot provide intelligent type inference for this connections[connection].acquire(block=True) code, so I would like to add typing hint to achieve this (intelligent type inference) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When your mouse hovers over a conn, vscode will tell you that it is the type of this conn is Any, which is very stupid. When hovering over a conn, I wish vscode would tell me what the explicit type is, instead of a stupid and damned Any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I added these typing hint which will solve this problem very well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return super().__getitem__(connection) | ||
|
||
def __setitem__(self, connection: Connection, pool: ConnectionPool): | ||
return super().__setitem__(connection, pool) | ||
|
||
def __delitem__(self, connection: Connection): | ||
return super().__delitem__(connection) | ||
|
||
|
||
connections = register_group(Connections(limit=use_global_limit)) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this method newly introduced?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not new, ConnectionPool inherits from Resource
Resource has an acquire method, but since Resource is an abstract class (abstracting the connection as a resource), this
-> Connection
type annotation should not be added to the acquire method of Resource, so I added an acquire method to ConnectionPool and added the I added an acquire method to the ConnectionPool and added the-> Connection
type annotation to the ConnectionPool to make it semantickombu/connection.py
kombu/resource.py