From 27e80d1fb01a66df468a0ef5f46025c81bbe1002 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Tue, 12 Nov 2024 14:49:15 -0500 Subject: [PATCH] Remove dict parent class from `Context` Use hasattr to look for `_version` variable Co-Authored-By: Taylor Steinberg --- src/posit/connect/context.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/posit/connect/context.py b/src/posit/connect/context.py index 1f312322..1e275838 100644 --- a/src/posit/connect/context.py +++ b/src/posit/connect/context.py @@ -1,10 +1,14 @@ +from __future__ import annotations + import functools -from typing import Optional, Protocol +from typing import TYPE_CHECKING, Protocol -import requests from packaging.version import Version -from .urls import Url +if TYPE_CHECKING: + import requests + + from .urls import Url def requires(version: str): @@ -23,25 +27,24 @@ def wrapper(instance: ContextManager, *args, **kwargs): return decorator -class Context(dict): +class Context: def __init__(self, session: requests.Session, url: Url): self.session = session self.url = url @property - def version(self) -> Optional[str]: - try: - value = self["version"] - except KeyError: + def version(self) -> str | None: + if not hasattr(self, "_version"): endpoint = self.url + "server_settings" response = self.session.get(endpoint) result = response.json() - value = self["version"] = result.get("version") - return value + self._version: str | None = result.get("version") + + return self._version @version.setter - def version(self, value): - self["version"] = value + def version(self, value: str | None): + self._version = value class ContextManager(Protocol):