The following code works exactly as expected:
from abc import ABCMeta
from typing import Dict, Any
class Singleton(ABCMeta):
__instances: Dict[Any, Any] = {}
def __call__(cls, *args: Any, **kwargs: Any) -> Any:
if cls not in cls.__instances:
cls.__instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls.__instances[cls]
@classmethod
def delete_all_instances(cls: Any) -> None:
cls.__instances = {}
print("Deleted")
class Class1(metaclass=Singleton):
def __init__(self) -> None:
print("Constructor")
self.__name = 'Class1'
def say_name(self) -> str:
return self.__name
if __name__ == "__main__":
obj1 = Class1()
print(obj1.say_name())
Singleton.delete_all_instances()
obj1 = Class1()
print(obj1.say_name())
Output:
Constructor
Class1
Deleted
Constructor
Class1
But Pylance (v2023.5.10) installed in Visual Studio Code 1.78.0 complains in line 29 Singleton.delete_all_instances()
, that Argument missing for parameter "cls"
. It doesn't matter, if the Any
behind the cls-parameter in the function definition is there, or not.
Is this a bug in Pylance, or am I just too blind to see the issue?
Seems like a real bug in Pyright. See bug report on Github