Skip to content
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

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

Conversation

ponponon
Copy link

from kombu import Connection
from kombu.pools import connections

connection = Connection('redis://localhost:6379')

with connections[connection].acquire(block=True) as conn:
    print('Got connection: {0!r}'.format(connection.as_uri()))

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)

I would like to add more typing hint to this project, hopefully the author will pass and let me participate in it

@lgtm-com
Copy link

lgtm-com bot commented Oct 16, 2022

This pull request introduces 1 alert when merging 58804fc into 22b5596 - view on LGTM.com

new alerts:

  • 1 for Unused import

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

kombu/pools.py:7:1: F401 'typing.Optional' imported but unused

@auvipy auvipy added this to the 5.3 milestone Oct 17, 2022
return connection.Pool(limit=limit)

def __getitem__(self, connection: Connection) -> ConnectionPool:
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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)

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Author

@ponponon ponponon Oct 17, 2022

Choose a reason for hiding this comment

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

No type hint 👇
图片

With type annotations 👇
图片

Modern program development is inseparable from the intelligent hints provided by the IDE. With these type annotations, the IDE can provide us with type inference, allowing us to program more efficiently and enjoyably

@lgtm-com
Copy link

lgtm-com bot commented Oct 17, 2022

This pull request introduces 2 alerts when merging c6d006b into 22b5596 - view on LGTM.com

new alerts:

  • 2 for Unused import

@lgtm-com
Copy link

lgtm-com bot commented Oct 17, 2022

This pull request introduces 2 alerts when merging d1fdaee into 22b5596 - view on LGTM.com

new alerts:

  • 2 for Unused import

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

kombu/pools.py:7:1: F401 'typing.Optional' imported but unused
kombu/pools.py:82:80: E501 line too long (80 > 79 characters)
kombu/connection.py:11:1: F401 'typing.Optional' imported but unused
kombu/connection.py:11:1: F401 'typing.Union' imported but unused
kombu/connection.py:1042:80: E501 line too long (93 > 79 characters)
ERROR: InvocationError for command /home/runner/work/kombu/kombu/.tox/flake8/bin/flake8 -j2 kombu t (exited with code 1)

kombu/connection.py Outdated Show resolved Hide resolved
kombu/pools.py Outdated Show resolved Hide resolved
@@ -1039,6 +1039,9 @@ def prepare(self, resource):
resource._debug('acquired')
return resource

def acquire(self, block: bool = False, timeout: int | float | None = None) -> Connection:
Copy link
Member

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?

Copy link
Author

@ponponon ponponon Oct 17, 2022

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 semantic

kombu/connection.py

class ConnectionPool(Resource):
    """Pool of connections."""

    def acquire(self, block: bool = False, timeout: Union[int, float, None] = None) -> Connection:
        return super().acquire(block, timeout)

kombu/resource.py

class Resource:
    """Pool of resources."""

    def acquire(self, block=False, timeout=None):
        """Acquire resource.

        Arguments:
            block (bool): If the limit is exceeded,
                then block until there is an available item.
            timeout (float): Timeout to wait
                if ``block`` is true.  Default is :const:`None` (forever).

        Raises:
            LimitExceeded: if block is false and the limit has been exceeded.

Copy link
Contributor

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

It will be cool to have an entry on the pyproject.toml associated with the annotation. 👀

Copy link
Member

@thedrow thedrow left a comment

Choose a reason for hiding this comment

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

Is there a better way than re-defining existing methods just to provide typing information for them?
I'm not satisfied with the current approach.

@auvipy auvipy modified the milestones: 5.3, 5.3.x Jun 1, 2023
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.

None yet

4 participants