Skip to content

chore: add type annotations to cloudinit.distros.opensuse - #7118

Open
anujsingh-cse wants to merge 1 commit into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-opensuse-5445
Open

anujsingh-cse wants to merge 1 commit into
canonical:mainfrom
anujsingh-cse:fix/untyped-defs-opensuse-5445

Conversation

@anujsingh-cse

Copy link
Copy Markdown

Proposed Commit Message

chore: add type annotations to cloudinit.distros.opensuse

Removing cloudinit.distros.opensuse and
tests.unittests.distros.test_opensuse from the mypy override list in
pyproject.toml surfaces errors in four places, all of them repeated
instances of two problems.

opensuse.preferred_ntp_clients fills the empty _preferred_ntp_clients
attribute, which the base class declares without a type. Annotate it
as Optional[List[str]] in cloudinit/distros/__init__.py, matching what
the base property, ubuntu, and opensuse all store.

test_opensuse.py builds its fixture through distros.fetch, whose
return type is the base Distro; update_method and read_only_root are
opensuse-specific attributes set in the subclass __init__, so the
assignments mypy flags are on a base-class-typed fixture. Annotate the
fixture as Any.

Refs GH-5445

Additional Context

Refs GH-5445. Claimed in #5445 (comment).

Removing both modules from the override list surfaces 23 errors, which
collapse into two problems:

  • opensuse.py:270 and opensuse.py:276 (plus the same shape in
    alpine.py:204, still overridden): the subclass property fills
    _preferred_ntp_clients, which the base class declares as a bare
    _preferred_ntp_clients = None at cloudinit/distros/__init__.py:145.
    mypy therefore types the attribute as None and rejects every
    subclass assignment of a list. The base property
    (__init__.py:654), ubuntu.py:54, and opensuse.py all store
    list[str] into it, so the declared type is Optional[List[str]].
    Annotating the declaration in the base class fixes every subclass at
    once; no other module assigns or reads the private attribute.
  • test_opensuse.py (update_method x18, read_only_root x3): the
    fixture is built through distros.fetch, annotated as returning
    Type[Distro] (the base class), while update_method and
    read_only_root are opensuse-specific attributes set in the subclass
    __init__. The tests reset that state by assignment, so the fixture
    is annotated Any rather than changing what is fetched or patched.

No signatures change, so there is no effect on callers.

Test Steps

$ python -m mypy cloudinit/ tests/ tools/
(0 errors in the target modules with the overrides removed)

$ pytest tests/unittests/distros/test_opensuse.py -q
14 passed

$ ruff check / black --check / isort --check-only / pylint (changed files)
All green

tox -e py3 and tox -e check_format to be confirmed by CI
(developed on Windows where the full tox suite cannot run).

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

Removing cloudinit.distros.opensuse and
tests.unittests.distros.test_opensuse from the mypy override list in
pyproject.toml surfaces errors in four places, all of them repeated
instances of two problems.

opensuse.preferred_ntp_clients fills the empty _preferred_ntp_clients
attribute, which the base class declares without a type. Annotate it
as Optional[List[str]] in cloudinit/distros/__init__.py, matching what
the base property, ubuntu, and opensuse all store.

test_opensuse.py builds its fixture through distros.fetch, whose
return type is the base Distro; update_method and read_only_root are
opensuse-specific attributes set in the subclass __init__, so the
assignments mypy flags are on a base-class-typed fixture. Annotate the
fixture as Any.

Refs canonicalGH-5445
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.

1 participant