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

Add urllib3.contrib.socks; improve urllib3.connectionpool #8457

Merged
merged 34 commits into from
Aug 4, 2022
Merged

Add urllib3.contrib.socks; improve urllib3.connectionpool #8457

merged 34 commits into from
Aug 4, 2022

Conversation

kkirsche
Copy link
Contributor

@kkirsche kkirsche commented Aug 1, 2022

This is required to address some re-exports in #8439

The source code is available here:
https://github.com/urllib3/urllib3/blob/main/src/urllib3/contrib/socks.py

@github-actions

This comment has been minimized.

srittau
srittau previously requested changes Aug 1, 2022
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

AlexWaygood commented Aug 1, 2022

As well as @srittau's points, note that this module only imports successfully if the optional dependency PySocks is installed (according to https://github.com/urllib3/urllib3/blob/16d2976041c5b8b0fe5f790e8edc9c50f318ee7b/src/urllib3/contrib/socks.py#L50-L52). You'll need to add the optional dependency to a requirements-stubtest.txt file in order for stubtest to be able to import it in CI (see https://github.com/python/typeshed/blob/master/stubs/tqdm/%40tests/requirements-stubtest.txt for an example).

@github-actions

This comment has been minimized.

Kevin Kirsche and others added 4 commits August 2, 2022 09:16
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

def __exit__(self, exc_type, exc_val, exc_tb): ...
def close(self): ...
scheme: ClassVar[str | None]
QueueCls: ClassVar[type[LifoQueue]]
Copy link
Member

Choose a reason for hiding this comment

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

I still think this would be better as the stdlib Queue[Any]. It's true that at runtime, ConnectionPool.QueueCls is urllib3.util.queue.LifoQueue. But ConnectionPool is a base class for other ConnectionPool classes, and I don't think (from my impression, from reading the code) that it's mandatory for all subclasses to have their QueueCls attribute be a subclass of urllib3.util.queue.LifoQueue. It seems to me that anything with an interface similar to the stdlib queue.Queue class will do. There's evidence for this in the fact that they scrapped the urllib3.util.queue submodule in the main branch and switched to using the stdlib queue module.

Copy link
Member

Choose a reason for hiding this comment

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

Anyway, this is minor, other than this it looks good to me now :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I think I got mixed up between the type[_QueueT] change and the Queue[Any] change. I agree with you on that.

If you are comfortable with it, I will update this to: ClassVar[type[Queue[Any]]]

and change the imports to be fully qualified to differentiate between the two different queues.

Sound good?

Copy link
Member

Choose a reason for hiding this comment

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

That sounds good to me! 👍

Copy link
Contributor Author

@kkirsche kkirsche Aug 3, 2022

Choose a reason for hiding this comment

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

That was changed in 3e231f4 (I expect a pre-commit will trigger). The only thing I may have missed was if there was a better way to import the LifoQueue to avoid the overlapping module names. As it stands, I've updated it to import queue for the standard library instance and usage as queue.Queue[Any] and have left the urllib3 LifoQueue as from .util.queue import LifoQueue.

My concern was that from .util import queue as _queue would be even more confusing with both queue.Queue and _queue.LifoQueue given that both have a LifoQueue

Hopefully thats correct

Copy link
Member

Choose a reason for hiding this comment

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

How about from .util import queue as urllib3queue? And then refer to it as urllib3queue.LifoQueue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had thought that was going to trigger a stubtest warning about it not existing at runtime, but I could just be getting confused about what is checked and what isn't.

That's a good approach, thank you for the suggestion, worst case we can do it as from .util import queue as _urllib3queue to avoid the check

Copy link
Member

Choose a reason for hiding this comment

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

All imports in stub files are considered "private to the stub" (not re-exported), unless they fall into one of the two categories:

  • import foo as foo -- the as has to be truly redundant; if you do import foo as bar, it's still considered private, but import foo as foo is considered a public re-export.
  • from foo import *

Copy link
Member

Choose a reason for hiding this comment

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

-- A third exception is if a name is included in __all__, it's always considered re-exported, even if it's imported via a means that would usually make it a "private" import.

@AlexWaygood AlexWaygood requested a review from srittau August 3, 2022 18:35
@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood changed the title fix: Add urllib3.contrib.socks types Add urllib3.contrib.socks; improve urllib3.connectionpool Aug 3, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau dismissed their stale review August 4, 2022 10:24

Changed since my review.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Not an in-depth review, but everything looked fine at first glance. I'll defer to @AlexWaygood's judgement.

@AlexWaygood
Copy link
Member

Let's land it. Thanks for working on this @kkirsche, this was a tricky one in lots of ways!

@AlexWaygood AlexWaygood merged commit 103c2f3 into python:master Aug 4, 2022
@kkirsche kkirsche deleted the refactor/urllib3-socks branch August 4, 2022 14:12
@kkirsche
Copy link
Contributor Author

kkirsche commented Aug 4, 2022

My pleasure, trying to work towards larger and loftier goals, so hopefully experiences like this will help

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.

4 participants