Skip to content

Commit fb12560

Browse files
update all path related operations to have more accurate types (#713)
1 parent 799b41f commit fb12560

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

django-stubs/contrib/staticfiles/finders.pyi

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
1-
from typing import Any, Iterable, Iterator, List, Mapping, Optional, Union, overload
1+
import os
2+
from typing import Any, Iterable, Iterator, List, Mapping, Optional, Tuple, Type, Union, overload
23

34
from django.core.checks.messages import CheckMessage
45
from django.core.files.storage import Storage
56
from typing_extensions import Literal
67

8+
_PathType = Union[str, bytes, os.PathLike]
79
searched_locations: Any
810

911
class BaseFinder:
1012
def check(self, **kwargs: Any) -> List[CheckMessage]: ...
11-
def find(self, path: str, all: bool = ...) -> Optional[Any]: ...
13+
@overload
14+
def find(self, path: _PathType, all: Literal[True]) -> List[_PathType]: ...
15+
@overload
16+
def find(self, path: _PathType, all: Literal[False] = ...) -> Optional[_PathType]: ...
1217
def list(self, ignore_patterns: Any) -> Iterable[Any]: ...
1318

1419
class FileSystemFinder(BaseFinder):
15-
locations: List[Any] = ...
20+
locations: List[Tuple[str, _PathType]] = ...
1621
storages: Mapping[str, Any] = ...
1722
def __init__(self, app_names: None = ..., *args: Any, **kwargs: Any) -> None: ...
18-
def find_location(self, root: str, path: str, prefix: str = ...) -> Optional[str]: ...
23+
def find_location(self, root: _PathType, path: _PathType, prefix: str = ...) -> Optional[_PathType]: ...
1924

2025
class AppDirectoriesFinder(BaseFinder):
21-
storage_class: Any = ...
26+
storage_class: Type[Storage] = ...
2227
source_dir: str = ...
2328
apps: List[str] = ...
24-
storages: Mapping[str, Any] = ...
29+
storages: Mapping[str, Storage] = ...
2530
def __init__(self, app_names: None = ..., *args: Any, **kwargs: Any) -> None: ...
26-
def find_in_app(self, app: str, path: str) -> Optional[str]: ...
31+
def find_in_app(self, app: str, path: _PathType) -> Optional[_PathType]: ...
2732

2833
class BaseStorageFinder(BaseFinder):
2934
storage: Storage = ...
3035
def __init__(self, storage: Optional[Storage] = ..., *args: Any, **kwargs: Any) -> None: ...
3136

3237
class DefaultStorageFinder(BaseStorageFinder): ...
3338

34-
def find(path: str, all: bool = ...) -> Optional[Union[List[str], str]]: ...
39+
@overload
40+
def find(path: str, all: Literal[True]) -> List[_PathType]: ...
41+
@overload
42+
def find(path: str, all: Literal[False] = ...) -> Optional[_PathType]: ...
3543
def get_finders() -> Iterator[BaseFinder]: ...
3644
@overload
3745
def get_finder(import_path: Literal["django.contrib.staticfiles.finders.FileSystemFinder"]) -> FileSystemFinder: ...
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
- case: test_find_one
2+
main: |
3+
from django.contrib.staticfiles import finders
4+
5+
reveal_type(finders.find("filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
6+
7+
for finder in finders.get_finders():
8+
reveal_type(finder.find("filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
9+
10+
reveal_type(finders.FileSystemFinder().find("filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
11+
reveal_type(finders.AppDirectoriesFinder().find("filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
12+
reveal_type(finders.DefaultStorageFinder().find("filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
13+
14+
- case: test_find_all
15+
main: |
16+
from django.contrib.staticfiles import finders
17+
18+
reveal_type(finders.find("filepath", all=True)) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.bytes, os.PathLike[Any]]]"
19+
20+
for finder in finders.get_finders():
21+
reveal_type(finder.find("filepath", all=True)) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.bytes, os.PathLike[Any]]]"
22+
23+
reveal_type(finders.FileSystemFinder().find("filepath", all=True)) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.bytes, os.PathLike[Any]]]"
24+
reveal_type(finders.AppDirectoriesFinder().find("filepath", all=True)) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.bytes, os.PathLike[Any]]]"
25+
reveal_type(finders.DefaultStorageFinder().find("filepath", all=True)) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.bytes, os.PathLike[Any]]]"
26+
27+
- case: test_file_system_finder # test methods *only* on FileSystemFinder
28+
main: |
29+
from django.contrib.staticfiles.finders import FileSystemFinder
30+
31+
finder = FileSystemFinder()
32+
reveal_type(finder.find_location(".", "filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"
33+
34+
- case: test_app_directories_finder # test methods *only* on AppDirectoriesFinder
35+
main: |
36+
from django.contrib.staticfiles.finders import AppDirectoriesFinder
37+
38+
finder = AppDirectoriesFinder()
39+
reveal_type(finder.find_in_app("app", "filepath")) # N: Revealed type is "Union[builtins.str, builtins.bytes, os.PathLike[Any], None]"

0 commit comments

Comments
 (0)