cloud-init icon indicating copy to clipboard operation
cloud-init copied to clipboard

[enhancement]: fix typing of untyped-defs

Open aciba90 opened this issue 2 years ago • 60 comments

Enhancement

In https://github.com/canonical/cloud-init/pull/5409 we enabled the check_untyped_defs mypy lint to not include more type issues and disabled modules not passing the check. Fix them all!

If you want to work any of the modules listed below, please add a comment with the name of the module you are working on so that work isn't duplicated.

Disabled modules

  • [x] cloudinit.analyze
  • [x] cloudinit.analyze.dump
  • [x] cloudinit.analyze.show
  • [x] cloudinit.cmd.devel.hotplug_hook
  • [x] cloudinit.cmd.devel.make_mime
  • [x] cloudinit.cmd.devel.net_convert
  • [x] cloudinit.cmd.main
  • [x] cloudinit.config.cc_apt_configure
  • [ ] cloudinit.config.cc_ca_certs
  • [x] cloudinit.config.cc_growpart
  • [x] cloudinit.config.cc_ntp
  • [x] cloudinit.config.cc_power_state_change
  • [x] cloudinit.config.cc_rsyslog
  • [x] cloudinit.config.cc_ubuntu_pro
  • [x] cloudinit.config.modules (priority)
  • [x] cloudinit.distros
  • [ ] cloudinit.distros.alpine
  • [ ] cloudinit.distros.azurelinux
  • [x] cloudinit.distros.bsd
  • [ ] cloudinit.distros.opensuse
  • [ ] cloudinit.distros.parsers.hostname
  • [x] cloudinit.distros.parsers.hosts
  • [ ] cloudinit.distros.parsers.resolv_conf
  • [x] cloudinit.distros.ubuntu
  • [x] cloudinit.distros.ug_util (priority)
  • [ ] cloudinit.helpers (priority)
  • [x] cloudinit.net.cmdline (priority)
  • [x] cloudinit.net.eni
  • [x] cloudinit.net.ephemeral
  • [ ] cloudinit.net.freebsd
  • [ ] cloudinit.net.netbsd
  • [ ] cloudinit.net.network_manager
  • [ ] cloudinit.net.network_state (priority)
  • [ ] cloudinit.net.networkd
  • [x] cloudinit.net.sysconfig
  • [ ] cloudinit.netinfo
  • [x] cloudinit.sources.DataSourceAzure
  • [x] cloudinit.sources.DataSourceBigstep
  • [x] cloudinit.sources.DataSourceCloudSigma
  • [x] cloudinit.sources.DataSourceCloudStack
  • [ ] cloudinit.sources.DataSourceConfigDrive
  • [x] cloudinit.sources.DataSourceDigitalOcean
  • [ ] cloudinit.sources.DataSourceEc2
  • [ ] cloudinit.sources.DataSourceExoscale
  • [ ] cloudinit.sources.DataSourceGCE
  • [ ] cloudinit.sources.DataSourceHetzner
  • [x] cloudinit.sources.DataSourceMAAS
  • [ ] cloudinit.sources.DataSourceNoCloud
  • [ ] cloudinit.sources.DataSourceOVF
  • [x] cloudinit.sources.DataSourceOpenNebula
  • [ ] cloudinit.sources.DataSourceOpenStack
  • [x] cloudinit.sources.DataSourceOracle
  • [ ] cloudinit.sources.DataSourceRbxCloud
  • [ ] cloudinit.sources.DataSourceScaleway
  • [ ] cloudinit.sources.DataSourceSmartOS
  • [x] cloudinit.sources.DataSourceVMware
  • [x] cloudinit.sources
  • [ ] cloudinit.sources.helpers.azure
  • [x] cloudinit.sources.helpers.ec2
  • [x] cloudinit.sources.helpers.netlink
  • [ ] cloudinit.sources.helpers.openstack
  • [ ] cloudinit.sources.helpers.vmware.imc.config_file
  • [ ] cloudinit.sources.helpers.vmware.imc.config_nic
  • [x] cloudinit.sources.helpers.vultr
  • [x] cloudinit.ssh_util (priority)
  • [x] cloudinit.stages
  • [x] cloudinit.temp_utils
  • [x] cloudinit.templater
  • [x] cloudinit.user_data (priority)
  • [x] tests.integration_tests.instances
  • [x] tests.unittests.analyze.test_show
  • [x] tests.unittests.cmd.devel.test_hotplug_hook
  • [x] tests.unittests.cmd.devel.test_render
  • [x] tests.unittests.cmd.test_clean
  • [x] tests.unittests.cmd.test_query
  • [x] tests.unittests.cmd.test_status
  • [x] tests.unittests.config.test_apt_configure_sources_list_v1
  • [ ] tests.unittests.config.test_apt_configure_sources_list_v3
  • [ ] tests.unittests.config.test_apt_source_v1
  • [x] tests.unittests.config.test_apt_source_v3
  • [x] tests.unittests.config.test_cc_ansible
  • [x] tests.unittests.config.test_cc_apk_configure
  • [ ] tests.unittests.config.test_cc_apt_pipelining
  • [x] tests.unittests.config.test_cc_bootcmd
  • [x] tests.unittests.config.test_cc_ca_certs
  • [ ] tests.unittests.config.test_cc_chef
  • [x] tests.unittests.config.test_cc_disable_ec2_metadata
  • [ ] tests.unittests.config.test_cc_final_message
  • [ ] tests.unittests.config.test_cc_growpart
  • [x] tests.unittests.config.test_cc_grub_dpkg
  • [ ] tests.unittests.config.test_cc_install_hotplug
  • [ ] tests.unittests.config.test_cc_keys_to_console
  • [x] tests.unittests.config.test_cc_landscape
  • [x] tests.unittests.config.test_cc_locale
  • [x] tests.unittests.config.test_cc_mcollective
  • [x] tests.unittests.config.test_cc_mounts
  • [x] tests.unittests.config.test_cc_phone_home
  • [x] tests.unittests.config.test_cc_power_state_change
  • [ ] tests.unittests.config.test_cc_puppet
  • [ ] tests.unittests.config.test_cc_resizefs
  • [ ] tests.unittests.config.test_cc_resolv_conf
  • [ ] tests.unittests.config.test_cc_rh_subscription
  • [x] tests.unittests.config.test_cc_rsyslog
  • [x] tests.unittests.config.test_cc_runcmd
  • [x] tests.unittests.config.test_cc_snap
  • [x] tests.unittests.config.test_cc_ssh
  • [x] tests.unittests.config.test_cc_timezone
  • [ ] tests.unittests.config.test_cc_ubuntu_autoinstall
  • [x] tests.unittests.config.test_cc_ubuntu_drivers
  • [x] tests.unittests.config.test_cc_ubuntu_pro
  • [ ] tests.unittests.config.test_cc_update_etc_hosts
  • [ ] tests.unittests.config.test_cc_users_groups
  • [ ] tests.unittests.config.test_cc_wireguard
  • [ ] tests.unittests.config.test_cc_yum_add_repo
  • [ ] tests.unittests.config.test_cc_zypper_add_repo
  • [ ] tests.unittests.config.test_modules
  • [ ] tests.unittests.config.test_schema
  • [x] tests.unittests.conftest (priority)
  • [x] tests.unittests.distros.test_alpine
  • [x] tests.unittests.distros.test_hosts
  • [ ] tests.unittests.distros.test_ifconfig
  • [ ] tests.unittests.distros.test_netbsd
  • [ ] tests.unittests.distros.test_netconfig
  • [ ] tests.unittests.distros.test_opensuse
  • [ ] tests.unittests.distros.test_user_data_normalize
  • [ ] tests.unittests.helpers (priority)
  • [ ] tests.unittests.net.test_dhcp
  • [ ] tests.unittests.net.test_init
  • [x] tests.unittests.net.test_network_state
  • [ ] tests.unittests.net.test_networkd
  • [ ] tests.unittests.runs.test_merge_run
  • [ ] tests.unittests.runs.test_simple_run
  • [ ] tests.unittests.sources.azure.test_errors
  • [ ] tests.unittests.sources.azure.test_imds
  • [ ] tests.unittests.sources.helpers.test_openstack
  • [ ] tests.unittests.sources.test_aliyun
  • [ ] tests.unittests.sources.test_altcloud
  • [ ] tests.unittests.sources.test_azure
  • [ ] tests.unittests.sources.test_azure_helper
  • [ ] tests.unittests.sources.test_cloudsigma
  • [ ] tests.unittests.sources.test_common
  • [ ] tests.unittests.sources.test_configdrive
  • [ ] tests.unittests.sources.test_digitalocean
  • [ ] tests.unittests.sources.test_ec2
  • [ ] tests.unittests.sources.test_exoscale
  • [ ] tests.unittests.sources.test_gce
  • [ ] tests.unittests.sources.test_init
  • [x] tests.unittests.sources.test_lxd
  • [x] tests.unittests.sources.test_maas
  • [ ] tests.unittests.sources.test_nocloud
  • [x] tests.unittests.sources.test_opennebula
  • [ ] tests.unittests.sources.test_openstack
  • [ ] tests.unittests.sources.test_oracle
  • [x] tests.unittests.sources.test_ovf
  • [ ] tests.unittests.sources.test_rbx
  • [ ] tests.unittests.sources.test_scaleway
  • [ ] tests.unittests.sources.test_smartos
  • [ ] tests.unittests.sources.test_upcloud
  • [ ] tests.unittests.sources.test_vultr
  • [x] tests.unittests.sources.test_wsl
  • [ ] tests.unittests.sources.vmware.test_vmware_config_file
  • [ ] tests.unittests.test__init__
  • [ ] tests.unittests.test_apport
  • [ ] tests.unittests.test_builtin_handlers
  • [x] tests.unittests.test_cli
  • [ ] tests.unittests.test_conftest
  • [x] tests.unittests.test_data
  • [ ] tests.unittests.test_ds_identify
  • [ ] tests.unittests.test_helpers
  • [x] tests.unittests.test_log
  • [ ] tests.unittests.test_merging
  • [ ] tests.unittests.test_net
  • [ ] tests.unittests.test_net_activators
  • [ ] tests.unittests.test_ssh_util
  • [ ] tests.unittests.test_stages
  • [ ] tests.unittests.test_subp
  • [ ] tests.unittests.test_templating
  • [ ] tests.unittests.test_upgrade
  • [ ] tests.unittests.test_url_helper
  • [ ] tests.unittests.test_util
  • [x] tests.unittests.util (priority)
  • [ ] netplan_schema_check
  • [x] mock-meta

edit by @holmanb

It's nice to see all of the community contributions working on this issue :-)

Obviously this work has created quite a bit of load on maintainers. We are working to provide feedback, but we also strongly appreciate when community members review each others' PRs before we get to it - this helps to improve consensus and agreement on the best way to tackle these issues.

A few things to keep in mind as you are writing proposals and reviewing others':

Annotating all available functions is preferred. Variable types can be inferred (no annotation needed), except when they can't, and helps us all produce higher quality code.

cast(): best avoided, but at times necessary - please include a comment to explain why a cast is required assert: please don't, this changes runtime behavior in a not-so-productive way: if your assertion just causes code to throw an assertion error before it would have produced some other exception, then we aren't catching and fixing bugs Any / Union - these (and others like them) provide limited utility - more concrete types help humans and analyzers understand what is happening (but these can't always be avoided)

Thanks for all of the contributions, and happy hacking!

aciba90 avatar Jun 25 '24 09:06 aciba90

It is encouraged to split this work on several PRs to ease implementation and review time.

aciba90 avatar Jun 25 '24 09:06 aciba90

Hey @aciba90, this seems like an interesting issue, I am a first time contributor to this repo, can I work on this?

bhngupta avatar Jul 23 '24 05:07 bhngupta

@bhngupta, for sure, contributions are always welcomed. Please, refer to our Contributing Overview for more help.

aciba90 avatar Jul 23 '24 08:07 aciba90

Hi, I will be working on fixing check_untyped_defs in cloudinit.analyze

abdulganiyy avatar Oct 08 '24 06:10 abdulganiyy

https://github.com/canonical/cloud-init/pull/6343

aciba90 avatar Jul 31 '25 14:07 aciba90

I'd like to work on cloudinit.distros.parsers.hostname and cloudinit.distros.parsers.resolv_conf

Ahmedaltu avatar Apr 21 '26 15:04 Ahmedaltu

Hello! As part of my onboarding I will start fixing the problems on cloudinit.cmd.*

jaborvs avatar May 04 '26 10:05 jaborvs

Hi! I would like to work on fixing the typing for cloudinit.distros.ubuntu. Will open a PR soon. Thank you for the great issue!

youneedgreg avatar Jun 16 '26 06:06 youneedgreg

Taking cloudinit.distros.parsers.hosts and tests.unittests.distros.test_hosts. PR incoming.

PhinehasNarh avatar Jun 27 '26 14:06 PhinehasNarh

I'd like to work on cloudinit.netinfo. Wil open a PR shortly.

ashmitjsg avatar Jul 10 '26 21:07 ashmitjsg

@aciba90 I would like to work on cloudinit.temp_utils

Prathamesh0901 avatar Jul 16 '26 13:07 Prathamesh0901

hostname.py 和 temp_utils.py 我去补一下

I'll work on adding type annotations to cloudinit/distros/parsers/hostname.py and cloudinit/temp_utils.py.

XEDAB avatar Jul 30 '26 11:07 XEDAB

hostname.py 和 temp_utils.py 我去补一下

I'll work on adding type annotations to cloudinit/distros/parsers/hostname.py and cloudinit/temp_utils.py.

I am working on cloudinit/temp_utils.py

Prathamesh0901 avatar Jul 30 '26 11:07 Prathamesh0901

抱歉,temp_utils.py 你搞吧,我专注于 hostname.py

Sorry, temp_utils.py is all yours. I'll focus on hostname.py only.

XEDAB avatar Jul 30 '26 11:07 XEDAB

Picked up the Oracle datasource module: #6978 adds the annotations and removes it from the pyproject override list. mypy is clean across the tree with the pinned 1.19.1 and the unit suite passes.

WaleedHU avatar Aug 05 '26 13:08 WaleedHU

Took cloudinit.templater and tests.unittests.test_templating: #6989 removes both from the pyproject override list.

Both came down to a single wrong annotation. JinjaSyntaxParsingException.format_error_message declared line_number: str, but the only production caller, __str__, passes self.lineno, which is an int.

Worth flagging for anyone picking up a nearby module: correcting the annotation also surfaces two call sites in tests/unittests/cmd/test_query.py and tests/unittests/cmd/devel/test_render.py that pass the string "2" to match the old signature. Those modules are already checked by mypy, so they do not show up if you only strip entries from the override list and re-run. The fix is one character each and the output is unchanged, since message_template formats the value through str.format.

tox -e py3 and tox -e check_format are both green on the branch with the pinned mypy 1.19.1.

Mohith1612 avatar Aug 11 '26 08:08 Mohith1612

Took cloudinit.sources.helpers.ec2 in #6995.

Two empty collections in MetadataMaterializer._parse that mypy cannot infer element types for. Annotated them as Dict[str, str] and List[str] based on what the loop below actually stores. No signature changes, so nothing downstream is affected, and tests.unittests.sources.helpers.test_ec2 was already outside the override list so it needed nothing.

tox -e py3 and tox -e check_format are both green on the branch with the pinned mypy 1.19.1.

Mohith1612 avatar Aug 12 '26 07:08 Mohith1612

I'm working on cloudinit.net.freebsd.

skywalkerplayer avatar Aug 13 '26 05:08 skywalkerplayer

I'd like to work on cloudinit.ssh_util and tests.unittests.test_ssh_util. Will open a PR shortly.

prathamesh04 avatar Aug 13 '26 16:08 prathamesh04

Took cloudinit.net.cmdline in #7004.

One empty dict in config_from_klibc_net_cfg that mypy cannot infer element types for. Annotated it as Dict[str, Dict[str, Any]] based on the {"files": [...], "entry": {...}} value the loop stores against each device name. No signature changes, so nothing downstream is affected, and tests.unittests.net.test_cmdline was already outside the override list.

tox -e py3 and tox -e check_format are both green on the branch with the pinned mypy 1.19.1.

Mohith1612 avatar Aug 14 '26 09:08 Mohith1612