From a9cbabe6bb173bde1f83e1a949da2f347ac3b297 Mon Sep 17 00:00:00 2001 From: Michael H Date: Fri, 5 Apr 2024 19:11:59 -0400 Subject: [PATCH] version --- async_utils/__init__.py | 2 +- async_utils/keyed_locks.py | 82 +++++++++++++++++++------------------- pyproject.toml | 4 ++ 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/async_utils/__init__.py b/async_utils/__init__.py index 8b034be..6ea9926 100644 --- a/async_utils/__init__.py +++ b/async_utils/__init__.py @@ -13,4 +13,4 @@ # limitations under the License. -__version__ = "4.0.1" +__version__ = "4.0.2" diff --git a/async_utils/keyed_locks.py b/async_utils/keyed_locks.py index 470c555..810f172 100644 --- a/async_utils/keyed_locks.py +++ b/async_utils/keyed_locks.py @@ -1,41 +1,41 @@ -# Copyright 2020-present Michael Hall -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from __future__ import annotations - -import asyncio -from collections.abc import Hashable -from typing import Generic, TypeVar -from weakref import WeakValueDictionary - -__all__ = ["KeyedLocks"] - -KT = TypeVar("KT", bound=Hashable) - - -class KeyedLocks(Generic[KT]): - """Locks per hashable resource type - Currently implemented with a weakvalue dictionary + asyncio.Locks - implementation could be improved to not rely on weakreference in the future - for performance reasons, but this would likely involve also re-implementing - some of the functionality of asyncio locks. May revisit later, intent here - is that if I do, everything I use like this improves at once. - """ - - def __init__(self) -> None: - self._locks: WeakValueDictionary[KT, asyncio.Lock] = WeakValueDictionary() - - def __getitem__(self, item: KT) -> asyncio.Lock: - return self._locks.get(item, self._locks.setdefault(item, asyncio.Lock())) +# Copyright 2020-present Michael Hall +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from __future__ import annotations + +import asyncio +from collections.abc import Hashable +from typing import Generic, TypeVar +from weakref import WeakValueDictionary + +__all__ = ["KeyedLocks"] + +KT = TypeVar("KT", bound=Hashable) + + +class KeyedLocks(Generic[KT]): + """Locks per hashable resource type + Currently implemented with a weakvalue dictionary + asyncio.Locks + implementation could be improved to not rely on weakreference in the future + for performance reasons, but this would likely involve also re-implementing + some of the functionality of asyncio locks. May revisit later, intent here + is that if I do, everything I use like this improves at once. + """ + + def __init__(self) -> None: + self._locks: WeakValueDictionary[KT, asyncio.Lock] = WeakValueDictionary() + + def __getitem__(self, item: KT) -> asyncio.Lock: + return self._locks.get(item, self._locks.setdefault(item, asyncio.Lock())) diff --git a/pyproject.toml b/pyproject.toml index fe6a9d3..fa3b7f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,6 +24,10 @@ reportUnnecessaryTypeIgnoreComment = true line-length = 130 target-version = "py310" +[tool.ruff.format] +line-ending = "lf" + + [tool.ruff.lint] select = [ "F", "E", "I", "UP", "YTT", "ANN", "S", "BLE", "B", "A", "COM", "C4", "DTZ",