From 07d0bb7da2a745cf8fa0d76091d3fd388882d5e2 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 6 Dec 2026 00:46:57 +0100 Subject: [PATCH] fix(registry): clear the get_upath_class cache for every newly registered protocol --- CHANGELOG.md | 3 +++ upath/registry.py | 5 ++--- upath/tests/test_registry.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e546377e..b7682cec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - upath: **BREAKING** drop support for Python 3.9 and 3.10, which reached their end of life, and require Python>=3.11 (#544) - upath: remove backports for Python<3.11, `typing_extensions` is no longer needed for resolving type hints at runtime +### Fixed +- upath.registry: clear the `get_upath_class()` cache whenever a protocol is registered, so a protocol looked up before it was registered no longer keeps returning `None` (#612) + ## [0.3.11] - 2026-11-24 ### Added diff --git a/upath/registry.py b/upath/registry.py index 6b13c9a2..8ef1b3e2 100644 --- a/upath/registry.py +++ b/upath/registry.py @@ -150,10 +150,9 @@ def __setitem__(self, item: str, value: type[upath.UPath] | str) -> None: raise ValueError( f"expected UPath subclass or FQN-string, got: {type(value).__name__!r}" ) - if not item or item in self._m: - get_upath_class.cache_clear() # type: ignore[attr-defined] - _get_implementation_protocols.cache_clear() # type: ignore[attr-defined] self._m[item] = value + get_upath_class.cache_clear() # type: ignore[attr-defined] + _get_implementation_protocols.cache_clear() # type: ignore[attr-defined] def __delitem__(self, __v: str) -> None: raise NotImplementedError("removal is unsupported") diff --git a/upath/tests/test_registry.py b/upath/tests/test_registry.py index dfbcc555..cd05bf40 100644 --- a/upath/tests/test_registry.py +++ b/upath/tests/test_registry.py @@ -120,6 +120,17 @@ class MyProtoPath(UPath): assert get_upath_class("myproto") is MyProtoPath +def test_register_implementation_after_failed_lookup(): + class LateProtoPath(UPath): + pass + + assert get_upath_class("lateproto", fallback=False) is None + + register_implementation("lateproto", LateProtoPath) + + assert get_upath_class("lateproto", fallback=False) is LateProtoPath + + def test_register_implementation_wrong_input(): with pytest.raises(TypeError): register_implementation(None, UPath) # type: ignore