Inherited class not recognized

Hello, I have an issue where an inherited class is not recognized by PyLance in VSCode or mypy.

  • OS: Windows 11
  • Python: 3.10.15
  • IDE: VSCode

I have a toolkit\virtualbus.py file containing the following classes:

from toolkit.log import Loggable
from abc import ABCMeta
from typing import final, TYPE_CHECKING
if TYPE_CHECKING:
    from typing import Type

__all__ = ["VirtualBus", "ModuleVirtualBus"]

class VirtualBus(Loggable, metaclass=ABCMeta):
    ... # no abstract methods, only inheriting from ABCMeta

@final
class ModuleVirtualBus(VirtualBus):
    def __new__(cls: "Type[ModuleVirtualBus]") -> "ModuleVirtualBus":
        # singleton pattern
        if cls._instance is None:
            cls._instance = object.__new__(cls)
        return cls._instance

Now I use this toolkit in another package, core. Whenever I try to import ModuleVirtualBus though, via the following:

from toolkit.virtualbus import ModuleVirtualBus

I get the following PyLance error:

"ModuleVirtualBus" is unknown import symbol

And the following mypy error:

Module "toolkit.virtualbus" has no attribute "ModuleVirtualBus"; maybe "VirtualBus"?

In a previous thread, it was pointed out to me that I was supposed to notify mypy of type checking by adding the py.typed file in the source folder of my toolkit package - which I did. Now, I don’t really understand what the issue is in this case. Am I doing something wrong in the inheritance or in exposing the classes?

I’m not doing the imports in toolkit\__init__.py because I want users to import these classes via accessing the virtualbus module, so it’s an intended choice.

Thanks in advance for any help.

Apparently, when installing in editable mode, ModuleVirtualBus is correctly recognized.

I should probably mention that I’m installing the toolkit locally, i.e.

pip install path/to/toolkit

But I don’t understand what causes this different behavior wether I’m installing in editable mode or not.

The py.typed file is probably not getting copied over. You need to declare it as a datafile for your build backend.

1 Like

Thanks for the reply.

Currently the pyproject.toml looks like this (I’m using setuptools as build backend):

[tool.setuptools]
package-dir = {"" = "src"}

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"*" = ["*.typed"]

py.typed is declared in src\toolkit\py.typed, so this should work but still doesn’t. I guess I have to investigate a little more.