Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mypy – Incompatible types in assignment

I am wondering why I am getting Incompatible types in assignment here?

from typing import cast

import pyvisa
from pyvisa.constants import InterfaceType
from pyvisa.resources import GPIBInstrument, TCPIPInstrument


class Instrument:
    resource_manager = pyvisa.ResourceManager()

    def __init__(self, resource: str):
        self.resource = self.resource_manager.open_resource(resource_name=resource)
        if self.resource.interface_type == InterfaceType.tcpip:
            self.instance: TCPIPInstrument = cast(TCPIPInstrument, resource)
        elif self.resource.interface_type == InterfaceType.gpib:
            self.instance: GPIBInstrument = cast(GPIBInstrument, resource)
        else:
            raise TypeError(f"Unsupported resource interface type: {self.resource.interface_type}")

Gives Incompatible types in assignment (expression has type "GPIBInstrument", variable has type "TCPIPInstrument")

self.instance gets correctly the type instance: TCPIPInstrument | GPIBInstrument in vscode.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using python 3.11.3 and mypy 1.2.0.

Link to a gist with the same issue, but with slightly different code since I could not get pyvisa to install in the playground.

Edit 1:
Found some issues in the code from the comments, here is the more correct code, but still with the same issue.

from typing import cast

import pyvisa
from pyvisa.constants import InterfaceType
from pyvisa.resources import GPIBInstrument, TCPIPInstrument


class Instrument:
    resource_manager = pyvisa.ResourceManager()

    def __init__(self, resource_name: str):
        self.resource = self.resource_manager.open_resource(resource_name=resource_name)
        if self.resource.interface_type == InterfaceType.tcpip:
            self.instance = cast(TCPIPInstrument, self.resource)
        elif self.resource.interface_type == InterfaceType.gpib:
            self.instance = cast(GPIBInstrument, self.resource)
        else:
            raise TypeError(f"Unsupported resource interface type: {self.resource.interface_type}")

>Solution :

self.instance can only have one type. If you want it to be a union, you need to specify that when it’s first declared:

        if self.resource.interface_type == InterfaceType.tcpip:
            self.instance: GPIBInstrument | TCPIPInstrument = cast(TCPIPInstrument, resource)
        elif self.resource.interface_type == InterfaceType.gpib:
            self.instance = cast(GPIBInstrument, resource)
        else:
            raise TypeError(f"Unsupported resource interface type: {self.resource.interface_type}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading