Skip to content

Add type hinting for open_url - #85845

Open
sivel wants to merge 2 commits into
ansible:develfrom
sivel:open-url-typing
Open

sivel wants to merge 2 commits into
ansible:develfrom
sivel:open-url-typing

Conversation

@sivel

@sivel sivel commented Sep 11, 2025

Copy link
Copy Markdown
Member
SUMMARY

This PR adds type hints for open_url and associated functions.

ISSUE TYPE
  • Test Pull Request

@ansibot ansibot added test This PR relates to tests. needs_triage Needs a first human triage before being processed. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Sep 11, 2025
@sivel
sivel marked this pull request as draft September 11, 2025 18:04
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Sep 11, 2025
@sivel
sivel marked this pull request as ready for review September 11, 2025 20:43
ciphers: list[str] | None = None,
use_netrc: bool | None = True,
context: ssl.SSLContext | None = None,
) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it intentional to allow the params with defaults (use_proxy, force, timeout, etc) to allow None here? That would seem to nullify the effect of calling _fallback() to set the values in open(), no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

None of the default values have changed, they should all be matching the git removal above this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It wasn't so much about the defaults changing, but more about being explicit that None is allowed. I didn't check any usages to see if we actually do that. It may not actually be a concern, just pointing it out.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pretty sure it's wrong for args with boolean defaults to have | None.

Comment thread lib/ansible/module_utils/urls.py Outdated
ssl_handler = urllib.request.HTTPSHandler(context=context)
handlers.append(ssl_handler)
if HAS_SSL:
if unix_socket and UnixHTTPSHandler:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If adding the HAS_SSL check, might as well simplify to:

if HAS_SSL and unix_socket and UnixHTTPSHandler:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've removed that branch. I'm not sure what I was stuck up on before, but I don't seem to be getting a mypy error any longer.

) from e

return signature
return signature.decode('utf-8')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps, add type annotations to this function signature before making runtime changes? It's not being type-checked unless you do so. Let MyPy show where your change may possibly cause problems.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It has type annotations, which is how I knew it caused an issue. It was that open_url didn't so it had no inference about what was being returned.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I have a feeling that sometimes comment-based annotations don't work well. Have you checked it by introducing an intentional error?
This is what I'm worried about:

def get_signature_from_source(source, display=None):  # type: (str, t.Optional[Display]) -> str

Comment on lines +739 to +756
use_proxy: bool | None = True,
force: bool | None = False,
timeout: int | None = 10,
validate_certs: bool | None = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool | None = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool | None = True,
ciphers: list[str] | None = None,
use_netrc: bool | None = True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Usually, bool values mustn't have a union w/ None (except for when it's used as a sentinel or has defined semantics).

Suggested change
use_proxy: bool | None = True,
force: bool | None = False,
timeout: int | None = 10,
validate_certs: bool | None = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool | None = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool | None = True,
ciphers: list[str] | None = None,
use_netrc: bool | None = True,
use_proxy: bool = True,
force: bool = False,
timeout: int | None = 10,
validate_certs: bool = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool = True,
ciphers: list[str] | None = None,
use_netrc: bool = True,

client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
use_gssapi: bool | None = False,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
use_gssapi: bool | None = False,
use_gssapi: bool = False,

Comment on lines +1057 to +1076
use_proxy: bool | None = True,
force: bool | None = False,
last_mod_time: _datetime.datetime | None = None,
timeout: int | None = 10,
validate_certs: bool | None = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool | None = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
use_gssapi: bool | None = False,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool | None = True,
ciphers: list[str] | None = None,
use_netrc: bool | None = True,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
use_proxy: bool | None = True,
force: bool | None = False,
last_mod_time: _datetime.datetime | None = None,
timeout: int | None = 10,
validate_certs: bool | None = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool | None = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
use_gssapi: bool | None = False,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool | None = True,
ciphers: list[str] | None = None,
use_netrc: bool | None = True,
use_proxy: bool = True,
force: bool = False,
last_mod_time: _datetime.datetime | None = None,
timeout: int | None = 10,
validate_certs: bool = True,
url_username: str | None = None,
url_password: str | None = None,
http_agent: str | None = None,
force_basic_auth: bool = False,
follow_redirects: _FollowRedirects | None = 'urllib2',
client_cert: str | None = None,
client_key: str | None = None,
cookies: cookiejar.CookieJar | None = None,
use_gssapi: bool = False,
unix_socket: str | None = None,
ca_path: str | None = None,
unredirected_headers: list[str] | None = None,
decompress: bool = True,
ciphers: list[str] | None = None,
use_netrc: bool = True,

Comment on lines +661 to +663
use_gssapi: bool | None,
force_basic_auth: bool | None,
use_netrc: bool | None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can these really be None in runtime?

@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Sep 23, 2025
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Sep 30, 2025
@ansibot ansibot added the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label May 5, 2026
@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_pr This PR has not been pushed to for more than one year. test This PR relates to tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants