From 70b7cb6f8c209c681945dd1070aeaf6489a85e0b Mon Sep 17 00:00:00 2001 From: Mark Joshwel Date: Fri, 1 Sep 2023 07:50:17 +0000 Subject: [PATCH] surplus: revert Result to typing.NamedTuple dataclasses cannot be frozen and have slots, a bug that probably will never be fixed --- surplus.future.ipynb | 61 +++++++++++++++++++++++++++++++++++++++----- surplus.py | 5 ++-- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/surplus.future.ipynb b/surplus.future.ipynb index 2dff9eb..bc8ad90 100644 --- a/surplus.future.ipynb +++ b/surplus.future.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -34,13 +34,60 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from surplus import PlusCodeQuery, LocalCodeQuery, LatlongQuery, StringQuery\n", - "from surplus import default_geocoder, default_reverser\n", - "from surplus import Latlong" + "from surplus import Latlong, Result\n", + "from surplus import default_geocoder, default_reverser" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Generic Result NamedTuple" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + }, + { + "ename": "Exception", + "evalue": "test", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/m/works/surplus/surplus.future.ipynb Cell 5\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m err_result \u001b[39m=\u001b[39m surplus\u001b[39m.\u001b[39mResult[\u001b[39mint\u001b[39m](\u001b[39m3\u001b[39m, error\u001b[39m=\u001b[39m\u001b[39mException\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mtest\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m 4\u001b[0m \u001b[39mprint\u001b[39m(nom_result\u001b[39m.\u001b[39mget())\n\u001b[0;32m----> 5\u001b[0m \u001b[39mprint\u001b[39m(err_result\u001b[39m.\u001b[39;49mget())\n", + "File \u001b[0;32m~/works/surplus/surplus.py:152\u001b[0m, in \u001b[0;36mResult.get\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[39m\"\"\"method that returns self.value if Result is non-erroneous else raises error\"\"\"\u001b[39;00m\n\u001b[1;32m 151\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39merror \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[0;32m--> 152\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39merror\n\u001b[1;32m 154\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mvalue\n", + "\u001b[0;31mException\u001b[0m: test" + ] + } + ], + "source": [ + "nom_result = Result[int](3)\n", + "err_result = Result[int](3, error=Exception(\"test\"))\n", + "\n", + "print(nom_result.get())\n", + "print(err_result.get())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Query Types" ] }, { @@ -65,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -74,7 +121,7 @@ "Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)" ] }, - "execution_count": 13, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -168,7 +215,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.11.1" }, "orig_nbformat": 4 }, diff --git a/surplus.py b/surplus.py index 3c91c30..f6dc749 100644 --- a/surplus.py +++ b/surplus.py @@ -26,7 +26,7 @@ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -For more information, please refer to +For more information, please refer to """ from argparse import ArgumentParser @@ -120,8 +120,7 @@ class NoSuitableLocationError(Exception): ResultType = TypeVar("ResultType") -@dataclass -class Result(Generic[ResultType]): +class Result(NamedTuple, Generic[ResultType]): """ typing.NamedTuple representing a result for safe value handling