Lab 5.2 – Fixing faulty implementation with GitHub Copilot
In this lab, we will address a faulty Singleton implementation by adding a unit test to verify its Singleton behavior, then use Copilot to analyze the error and suggest a fix. This lab is inspired by a bug fix the author encountered earlier in her career, where an Environment class, intended to function as a Singleton, failed to preserve attribute changes across instances.
The initial faulty implementation of the Singleton metaclass was as follows:
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls.__name__] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls.__name__]
This implementation is close to the correct implementation of a Singleton __call__ function:
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances...