Skip to content

Incorrect type reported by type checker for async properties #63

Closed

Description

@ValdezFOmar

When trying to access an async property of an interface, both mypy and pyright report the wrong type. In this example, I'd expect access_point.ssid to report type bytes, but it doesn't. Also, if I call get_async() directly it reports a different type for pyright.

import sdbus
import sdbus_async.networkmanager as nm
from typing import reveal_type

system_bus = sdbus.sd_bus_open_system()

async def get_wifi_connections(wifi_device: nm.NetworkDeviceWireless):
    for path in await wifi_device.get_all_access_points():
        access_point = nm.AccessPoint(path, system_bus)
        ssid = await access_point.ssid
        reveal_type(ssid)
        ssid = await access_point.ssid.get_async()
  reveal_type(ssid)

mypy output:

$ mypy type_test_async.py
type_test_async.py:10: error: Need type annotation for "ssid"  [var-annotated]
type_test_async.py:11: note: Revealed type is "Any"
type_test_async.py:13: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

pyright output:

$ pyright type_test_async.py
./type_test_async.py
 ./type_test_async.py:11:21 - information: Type of "ssid" is "T@__await__"
 ./type_test_async.py:13:21 - information: Type of "ssid" is "Unknown"
0 errors, 0 warnings, 2 informations 

In both cases, I would expect for the revealed type to be bytes. At runtime, my code works and reveal_type prints Runtime type is 'bytes', so I guess the problem has to do with the type hints. Also, the blocking API doesn't have this problem and correctly report bytes.

import sdbus
import sdbus_block.networkmanager as nm_block
from typing import reveal_type

system_bus = sdbus.sd_bus_open_system()

def get_wifi_block_connections(wifi_device: nm_block.NetworkDeviceWireless):
    for path in wifi_device.access_points:
        access_point = nm_block.AccessPoint(path, system_bus)
        ssid = access_point.ssid
        reveal_type(ssid)  # Both mypy and pyright report `bytes`

I'm using:

  • mypy 1.8.0
  • pyright 1.1.350
  • python 3.11.7

Activity

  1. igo95862 commented on Feb 15, 2024

    @igo95862
    Collaborator

    Hello @ValdezFOmar ,

    What version of python-sdbus are you using?

    Is it only .ssid property that has those issues or do other properties have those issues as well?

  2. igo95862 commented on Feb 15, 2024

    @igo95862
    Collaborator

    I tested the typing of a property using the D-Bus org.freedesktop.DBus and I could not replicate those issues:

    features_list = await dbus_object.features
    reveal_type(features_list)
    
    python-sdbus/test/test_proxies.py:41: note: Revealed type is "builtins.list[builtins.str]"
    Success: no issues found in 44 source files
    
  3. igo95862 commented on Feb 15, 2024

    @igo95862
    Collaborator

    Hmmmm. There might be an issue with the 0.11.1 but the master branch seems fine. Could you give a test to the python-sdbus master branch?

  4. ValdezFOmar commented on Feb 15, 2024

    @ValdezFOmar
    Author

    What version of python-sdbus are you using?

    I'm using python-sdbus 0.11.1

    Is it only .ssid property that has those issues or do other properties have those issues as well?

    Any async property that I tried to access had this issue, I only use ssid as an example.

    Hmmmm. There might be an issue with the 0.11.1 but the master branch seems fine. Could you give a test to the python-sdbus master branch?

    I did and now both mypy and pyright report the correct types for properties. Thank you.

  5. igo95862 commented on Feb 16, 2024

    @igo95862
    Collaborator

    Should I release the version 0.11.2 which will fix the issue? The current master branch has some really big reworks.

  6. ValdezFOmar commented on Feb 16, 2024

    @ValdezFOmar
    Author

    Feel free to do what you see fit, I'm not in a hurry and it's not that big of a problem. For me the issue is fixed either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions