diff --git a/CHANGELOG.md b/CHANGELOG.md index fd202b13b0e08ebfa4e266b58545705adb63f8fa..a73bc421399e29c08873a9e6902237ae5afde524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.17.4 + +* warnings for some use cases of Closeable diff --git a/satella/__init__.py b/satella/__init__.py index 588c3879b0769584fe15ba92b1099d0c1c53b34d..1b1cd0327bf37dc564b76f4d260019b63a2c2878 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.17.4a1' +__version__ = '2.17.4a2' diff --git a/satella/coding/misc.py b/satella/coding/misc.py index ed970434fc518582ba80c72a61e11dfd7522ea24..7377f33ceedafaae12337d5313cd2b9ef7201431 100644 --- a/satella/coding/misc.py +++ b/satella/coding/misc.py @@ -60,7 +60,11 @@ class Closeable: Can be also used as a context manager, with close() called upon __exit__. .. warning:: You should extend both __init__ and close(). Invoke __init__() at the end of - your class constructor, this will prevent the destructor from closing on half-initialized classes. + your class constructor, this will prevent the destructor from closing on half-initialized + classes. + + Objects before initialization (calling of this constructor) are considered closed. + Checking if they are closed will emit a warning. """ __slots__ = '__finalized', @@ -72,7 +76,12 @@ class Closeable: """ :return: whether this object is closed """ - return self.__finalized + try: + return self.__finalized + except AttributeError: + warnings.warn('You are checking whether a non-initialized object was closed', + UserWarning) + return True def close(self) -> bool: """ @@ -91,7 +100,7 @@ class Closeable: try: return not self.__finalized except AttributeError: - return False # the device does not need clean up + warnings.warn('Attempted to clean up a non-initialized object', UserWarning) finally: self.__finalized = True