Skip to content

enhancement(typing): add type annotations to cloudinit.sources.helpers.netlink (#5445) - #7064

Open
AhmadBilalDSA wants to merge 3 commits into
canonical:mainfrom
AhmadBilalDSA:fix/typing-netlink-helpers
Open

enhancement(typing): add type annotations to cloudinit.sources.helpers.netlink (#5445)#7064
AhmadBilalDSA wants to merge 3 commits into
canonical:mainfrom
AhmadBilalDSA:fix/typing-netlink-helpers

Conversation

@AhmadBilalDSA

Copy link
Copy Markdown

Addresses part of #5445 by adding strict type annotations to cloudinit.sources.helpers.netlink to satisfy --disallow-untyped-defs.

Changes Made

  • Added type hints to all public helper functions, methods, and return signatures in cloudinit/sources/helpers/netlink.py.
  • Fully annotated internal callback signatures (should_continue_cb).
  • Added # type: ignore[attr-defined] guards for platform-specific socket attributes (AF_NETLINK, NETLINK_ROUTE).
  • Verified zero errors with mypy --disallow-untyped-defs.

Type of Change

  • Code quality / Typing improvement

Addresses #5445

@blackboxsw blackboxsw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank your for this submission and helping improve quality in cloud-init.

I think there are a couple of thngs to resolve to move forward with this PR.

  1. Please avoid adding more # type: ignore comments to the code and actually resolve the typing issues in the file to progress toward proper typing coverage.
  2. make tox -e mypy succeed
  3. update docstr :returns: of create_bound_netlink_socket and get_netlink_msg_header to represent that actual instance type returned.

@blackboxsw blackboxsw added the incomplete Action required by submitter label Sep 4, 2026
@AhmadBilalDSA

AhmadBilalDSA commented Sep 5, 2026

Copy link
Copy Markdown
Author

Thank you for the review, @blackboxsw! I have addressed all feedback points in the latest commit:

  1. Avoid # type: ignore: Replaced direct access to socket.AF_NETLINK and socket.NETLINK_ROUTE with getattr(socket, 'AF_NETLINK', 16) and getattr(socket, 'NETLINK_ROUTE', 0), resolving the typing issue cleanly without ignores. 2. Type checker: Verified that mypy cloudinit/sources/helpers/netlink.py passes cleanly (Success: no issues found in 1 source file). 3. Docstring updates: Updated :returns: for both create_bound_netlink_socket and get_netlink_msg_header to document the concrete instance types (socket.socket and NetlinkHeader)

@AhmadBilalDSA

Copy link
Copy Markdown
Author

All four feedback items have been addressed in cloudinit/sources/helpers/netlink.py:

  1. Type safety without # type: ignore or getattr fallbacks:
    Replaced getattr(socket, "AF_NETLINK", 16) and getattr(socket, "NETLINK_ROUTE", 0) with standard socket.AF_NETLINK and socket.NETLINK_ROUTE inside a sys.platform == "linux" guard. It raises NetlinkCreateSocketError on non-Linux platforms rather than relying on fallback magic numbers. MyPy cleanly narrows sys.platform via typeshed without requiring ignores.

  2. Docstrings:
    Verified and retained explicit return types (socket.socket at line 65 and NetlinkHeader namedtuple at line 92).

  3. Elimination of # type: ignore:
    Removed # type: ignore[unreachable] and the dead if data is None branch in read_netlink_socket. netlink_socket.recv() returns bytes per standard library type stubs, so it directly returns netlink_socket.recv(MAX_SIZE). The file now contains zero # type: ignore pragmas.

  4. Verification:

    • Static typing: mypy passes cleanly under both default (Windows) and --platform linux.
    • Formatting/linting: ruff check and black pass with zero violations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

incomplete Action required by submitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants