surplus: revert Result to typing.NamedTuple

dataclasses cannot be frozen and have slots, a bug that probably will never be fixed
This commit is contained in:
Mark Joshwel 2023-09-01 07:50:17 +00:00
parent 3a67ad8a1d
commit 70b7cb6f8c
2 changed files with 56 additions and 10 deletions

View file

@ -9,7 +9,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -34,13 +34,60 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from surplus import PlusCodeQuery, LocalCodeQuery, LatlongQuery, StringQuery\n", "from surplus import PlusCodeQuery, LocalCodeQuery, LatlongQuery, StringQuery\n",
"from surplus import default_geocoder, default_reverser\n", "from surplus import Latlong, Result\n",
"from surplus import Latlong" "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<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell://wsl%2Balpine/home/m/works/surplus/surplus.future.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a>\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 <a href='vscode-notebook-cell://wsl%2Balpine/home/m/works/surplus/surplus.future.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=3'>4</a>\u001b[0m \u001b[39mprint\u001b[39m(nom_result\u001b[39m.\u001b[39mget())\n\u001b[0;32m----> <a href='vscode-notebook-cell://wsl%2Balpine/home/m/works/surplus/surplus.future.ipynb#X13sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a>\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", "cell_type": "code",
"execution_count": 13, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -74,7 +121,7 @@
"Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)" "Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)"
] ]
}, },
"execution_count": 13, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -168,7 +215,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.12" "version": "3.11.1"
}, },
"orig_nbformat": 4 "orig_nbformat": 4
}, },

View file

@ -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 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/> For more information, please refer to <http:m/unlicense.org/>
""" """
from argparse import ArgumentParser from argparse import ArgumentParser
@ -120,8 +120,7 @@ class NoSuitableLocationError(Exception):
ResultType = TypeVar("ResultType") ResultType = TypeVar("ResultType")
@dataclass class Result(NamedTuple, Generic[ResultType]):
class Result(Generic[ResultType]):
""" """
typing.NamedTuple representing a result for safe value handling typing.NamedTuple representing a result for safe value handling