From 0633c16e46bd2acc569467ac1e8b258e1ebb5eeb Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 23 Aug 2024 22:30:58 -0400 Subject: [PATCH 1/3] Ensure PlatformDirs is valid superclass type for mypy --- src/platformdirs/__init__.py | 8 +++++++- tests/test_api.py | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index 3f7d9490..9b7a1724 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -19,6 +19,8 @@ from pathlib import Path from typing import Literal + from typing_extensions import TypeAlias + def _set_platform_dir_class() -> type[PlatformDirsABC]: if sys.platform == "win32": @@ -42,7 +44,11 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]: 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: TypeAlias = PlatformDirsABC +else: + PlatformDirs = _set_platform_dir_class() #: Currently active platform AppDirs = PlatformDirs #: Backwards compatibility with appdirs diff --git a/tests/test_api.py b/tests/test_api.py index a868e298..22299dbf 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): ... From 8d958873ab807b7beb6d5383974988ff95259b95 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 23 Aug 2024 23:16:09 -0400 Subject: [PATCH 2/3] Make PlatformDirs not seen as an abstract class for type-checkers --- src/platformdirs/__init__.py | 20 ++++++++++---------- tests/test_android.py | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index 9b7a1724..1ed9d29b 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -21,18 +21,18 @@ from typing_extensions import TypeAlias +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 @@ -41,12 +41,12 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]: return Android # return to avoid redefinition of a result - return Result + return _Result if TYPE_CHECKING: # Work around mypy issue: https://github.com/python/mypy/issues/10962 - PlatformDirs: TypeAlias = PlatformDirsABC + PlatformDirs: TypeAlias = _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 7166ee4a..0c5de905 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 From 91925a9753d9997e0c23a996f5d72ec2f6b72a35 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 23 Aug 2024 23:20:14 -0400 Subject: [PATCH 3/3] Omit TypeAlias --- src/platformdirs/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/platformdirs/__init__.py b/src/platformdirs/__init__.py index 1ed9d29b..afe8351d 100644 --- a/src/platformdirs/__init__.py +++ b/src/platformdirs/__init__.py @@ -19,8 +19,6 @@ from pathlib import Path from typing import Literal - from typing_extensions import TypeAlias - if sys.platform == "win32": from platformdirs.windows import Windows as _Result elif sys.platform == "darwin": @@ -46,7 +44,7 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]: if TYPE_CHECKING: # Work around mypy issue: https://github.com/python/mypy/issues/10962 - PlatformDirs: TypeAlias = _Result + PlatformDirs = _Result else: PlatformDirs = _set_platform_dir_class() #: Currently active platform AppDirs = PlatformDirs #: Backwards compatibility with appdirs