pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

A local variable is being treated as type 'Never' in an except handler

Open brianreinhold opened this issue 5 months ago • 3 comments

Environment data

Version: 1.87.2 (user setup) Commit: 863d2581ecda6849923a2118d93a088b0745d9d6 Date: 2024-03-08T15:20:17.278Z Electron: 27.3.2 ElectronBuildId: 26836302 Chromium: 118.0.5993.159 Node.js: 18.17.1 V8: 11.8.172.18-electron.0 OS: Windows_NT x64 10.0.19045

  • Language Server version: 2024.3.1 (pyright version 1.1.351, commit 87171fbf)
  • Windows 10
  • Python version 3.9

Code Snippet

async def connect(self, device: Device, phd_info: PhdInfo) -> `None:
        deviceConnection: DeviceConnection | None = None
        try:
            # connect to the device (this is an aioble method)
            deviceConnection = await device.connect()
            # if none something went wrong. Back to scanning.
            if (not deviceConnection): return
            # doing stuff
        except Exception as ex:
            logger.error(f'LNI: Unexpected exception outer loop: {ex}') # type: ignore
            if deviceConnection is not None:
                await deviceConnection.disconnect()

Repro Steps

In the code above Pylance gives the following error in await deviceConnection.disconnect() in the exception handler:

"Never" is not awaitablePylancereportGeneralTypeIssues (variable) deviceConnection: Unknown | DeviceConnection

If I change deviceConnection to an instance variable self.deviceConnection the error goes away.

Expect:

No error

Actual behavior:

"Never" is not awaitablePylancereportGeneralTypeIssues (variable) deviceConnection: Unknown | DeviceConnection

on the line in the exception handler

brianreinhold avatar Mar 21 '24 11:03 brianreinhold

Please create a minimal, self-contained code sample that demonstrates the issue as requested in the issue template. The code sample you provided above references a bunch of symbols whose origins are unknown, so it's not possible to repro the problem you're reporting.

erictraut avatar Mar 21 '24 14:03 erictraut

Please provide imports for Device, DeviceConnection and PhdInfo and with packages to pip install thank you

bschnurr avatar Apr 02 '24 20:04 bschnurr

I will need to put this on the backburner. It will take the entire project I have to satisfy those imports

brianreinhold avatar Apr 02 '24 20:04 brianreinhold