From a7bd1227635581395f14f528a1c896ee59e009ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Wed, 9 Jun 2021 20:07:15 +0200 Subject: [PATCH] warnings for some use cases of Closeable --- CHANGELOG.md | 2 ++ satella/__init__.py | 2 +- satella/coding/misc.py | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd202b13..a73bc421 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 588c3879..1b1cd032 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 ed970434..7377f33c 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 -- GitLab