Skip to content

Commit 330b272

Browse files
Avasamgaborbernat
andauthored
Ensure PlatformDirs is valid superclass type for mypy AND not an abstract class for other checkers (#295)
Co-authored-by: Bernát Gábor <[email protected]>
1 parent 1ca8592 commit 330b272

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/platformdirs/__init__.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
from pathlib import Path
2020
from typing import Literal
2121

22+
if sys.platform == "win32":
23+
from platformdirs.windows import Windows as _Result
24+
elif sys.platform == "darwin":
25+
from platformdirs.macos import MacOS as _Result
26+
else:
27+
from platformdirs.unix import Unix as _Result
2228

23-
def _set_platform_dir_class() -> type[PlatformDirsABC]:
24-
if sys.platform == "win32":
25-
from platformdirs.windows import Windows as Result # noqa: PLC0415
26-
elif sys.platform == "darwin":
27-
from platformdirs.macos import MacOS as Result # noqa: PLC0415
28-
else:
29-
from platformdirs.unix import Unix as Result # noqa: PLC0415
3029

30+
def _set_platform_dir_class() -> type[PlatformDirsABC]:
3131
if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
3232
if os.getenv("SHELL") or os.getenv("PREFIX"):
33-
return Result
33+
return _Result
3434

3535
from platformdirs.android import _android_folder # noqa: PLC0415
3636

@@ -39,10 +39,14 @@ def _set_platform_dir_class() -> type[PlatformDirsABC]:
3939

4040
return Android # return to avoid redefinition of a result
4141

42-
return Result
42+
return _Result
4343

4444

45-
PlatformDirs = _set_platform_dir_class() #: Currently active platform
45+
if TYPE_CHECKING:
46+
# Work around mypy issue: https://github.com/python/mypy/issues/10962
47+
PlatformDirs = _Result
48+
else:
49+
PlatformDirs = _set_platform_dir_class() #: Currently active platform
4650
AppDirs = PlatformDirs #: Backwards compatibility with appdirs
4751

4852

tests/test_android.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_android_folder_from_jnius(mocker: MockerFixture, monkeypatch: pytest.Mo
7676

7777
_android_folder.cache_clear()
7878

79-
if PlatformDirs is Android:
79+
if PlatformDirs is Android: # type: ignore[comparison-overlap] # See https://github.com/platformdirs/platformdirs/pull/295
8080
import jnius # pragma: no cover # noqa: PLC0415
8181

8282
autoclass = mocker.spy(jnius, "autoclass") # pragma: no cover

tests/test_api.py

+9
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ def test_no_ctypes() -> None:
121121
import platformdirs # noqa: PLC0415
122122

123123
assert platformdirs
124+
125+
126+
def test_mypy_subclassing() -> None:
127+
# Ensure that PlatformDirs / AppDirs is seen as a valid superclass by mypy
128+
# This is a static type-checking test to ensure we work around
129+
# the following mypy issue: https://github.com/python/mypy/issues/10962
130+
class PlatformDirsSubclass(platformdirs.PlatformDirs): ...
131+
132+
class AppDirsSubclass(platformdirs.AppDirs): ...

0 commit comments

Comments
 (0)