I'm working on a Python project that uses the Azure SDK, and I'm encountering type checking issues with VS Code/Pylance due to the multi-API versioning mechanism of the Azure SDK.
Here's the error message I'm receiving from Pylance:
Expression of type "azure.mgmt.compute.v2021_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_04_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_11_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_08_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_11_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_09_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2015_06_15.models._models_py3.VirtualMachine | azure.mgmt.compute.v2016_03_30.models._models_py3.VirtualMachine | azure.mgmt.compute.v2016_04_30_preview.models._models_py3.VirtualMachine | azure.mgmt.compute.v2017_03_30.models._models_py3.VirtualMachine | azure.mgmt.compute.v2017_12_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_04_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_06_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_10_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_12_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2020_06_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2020_12_01.models._models_py3.VirtualMachine" cannot be assigned to declared type "VirtualMachine"
Type "azure.mgmt.compute.v2021_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_04_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2021_11_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_08_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2022_11_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2023_09_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2015_06_15.models._models_py3.VirtualMachine | azure.mgmt.compute.v2016_03_30.models._models_py3.VirtualMachine | azure.mgmt.compute.v2016_04_30_preview.models._models_py3.VirtualMachine | azure.mgmt.compute.v2017_03_30.models._models_py3.VirtualMachine | azure.mgmt.compute.v2017_12_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_04_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_06_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2018_10_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_03_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2019_12_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2020_06_01.models._models_py3.VirtualMachine | azure.mgmt.compute.v2020_12_01.models._models_py3.VirtualMachine" cannot be assigned to type "VirtualMachine"
"azure.mgmt.compute.v2021_03_01.models._models_py3.VirtualMachine" is incompatible with "azure.mgmt.compute.v2023_09_01.models._models_py3.VirtualMachine"
I'm looking for a way to suppress these errors without losing the benefits of type checking.
Here's a simplified example of how I'm using the Azure SDK in my code:
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import VirtualMachine
[...]
def get_virtual_machine_details(
self, resource_group_name: str, vm_name: str
) -> VirtualMachine:
vm_details = self._compute_client.virtual_machines.get(
resource_group_name, vm_name, expand="instanceView"
)
return vm_details
This is an example with the VirtualMachine type but it happens with ResourceGroup and others... It does not happen when I use Pycharm instead of VS Code.
I used a lot of # type: ignore in my code, it works, but I feel it's not the most elegant solution.