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.