diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index 3f7d949..afe8351 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -19,18 +19,18 @@ from pathlib import Path from typing import Literal +if sys.platform == "win32": + from platformdirs.windows import Windows as _Result +elif sys.platform == "darwin": + from platformdirs.macos import MacOS as _Result +else: + from platformdirs.unix import Unix as _Result -def _set_platform_dir_class() -> type[PlatformDirsABC]: - if sys.platform == "win32": - from platformdirs.windows import Windows as Result # noqa: PLC0415 - elif sys.platform == "darwin": - from platformdirs.macos import MacOS as Result # noqa: PLC0415 - else: - from platformdirs.unix import Unix as Result # noqa: PLC0415 +def _set_platform_dir_class() -> type[PlatformDirsABC]: if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system": if os.getenv("SHELL") or os.getenv("PREFIX"): - return Result + return _Result from platformdirs.android import _android_folder # noqa: PLC0415 @@ -39,10 +39,14 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]: return Android # return to avoid redefinition of a result - return Result + return _Result -PlatformDirs = _set_platform_dir_class() #: Currently active platform +if TYPE_CHECKING: + # Work around mypy issue: https://github.com/python/mypy/issues/10962 + PlatformDirs = _Result +else: + PlatformDirs = _set_platform_dir_class() #: Currently active platform AppDirs = PlatformDirs #: Backwards compatibility with appdirs diff --git a/tests/test_android.py b/tests/test_android.py index 7166ee4..0c5de90 100644 --- a/tests/test_android.py +++ b/tests/test_android.py @@ -76,7 +76,7 @@ def test_android_folder_from_jnius(mocker: MockerFixture, monkeypatch: pytest.Mo _android_folder.cache_clear() - if PlatformDirs is Android: + if PlatformDirs is Android: # type: ignore[comparison-overlap] # See https://github.com/platformdirs/platformdirs/pull/295 import jnius # pragma: no cover # noqa: PLC0415 autoclass = mocker.spy(jnius, "autoclass") # pragma: no cover diff --git a/tests/test_api.py b/tests/test_api.py index a868e29..22299db 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -121,3 +121,12 @@ def test_no_ctypes() -> None: import platformdirs # noqa: PLC0415 assert platformdirs + + +def test_mypy_subclassing() -> None: + # Ensure that PlatformDirs / AppDirs is seen as a valid superclass by mypy + # This is a static type-checking test to ensure we work around + # the following mypy issue: https://github.com/python/mypy/issues/10962 + class PlatformDirsSubclass(platformdirs.PlatformDirs): ... + + class AppDirsSubclass(platformdirs.AppDirs): ...