-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add stubs for django-environ #14573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmCeeEs
wants to merge
10
commits into
python:main
Choose a base branch
from
EmCeeEs:feature/django-environ-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−0
Open
Add stubs for django-environ #14573
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a7151c6
[django-environ] Run create_baseline_stubs.py script.
40c0da3
[django-environ] Fix incomplete types.
a08916e
[django-environ] Use generic types where applicable.
f090e9f
[django-environ] Fix tests.
11da833
[django-environ] Improve typing for Path.
5b5a7bb
[django-environ] Fix stubtests.
88ae28a
[django-environ] Apply suggestions.
e0977d6
[django-environ] Use import aliases for builtins.
fe8b2f4
[django-environ] Add overloads for various cast arguments types.
73b4012
[django-environ] Add missing type arguments (pyright).
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
version = "0.12.*" | ||
upstream_repository = "https://github.com/joke2k/django-environ" | ||
requires = ["django-stubs"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import Final | ||
|
||
from .compat import DJANGO_POSTGRES as DJANGO_POSTGRES, PYMEMCACHE_DRIVER as PYMEMCACHE_DRIVER, REDIS_DRIVER as REDIS_DRIVER | ||
from .environ import * | ||
|
||
__copyright__: Final[str] | ||
__version__: Final[str] | ||
__license__: Final[str] | ||
__author_email__: Final[str] | ||
__maintainer__: Final[str] | ||
__maintainer_email__: Final[str] | ||
__url__: Final[str] | ||
__description__: Final[str] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import Final | ||
|
||
from django.core.exceptions import ImproperlyConfigured as ImproperlyConfigured | ||
|
||
def choose_rediscache_driver() -> str: ... | ||
def choose_postgres_driver() -> str: ... | ||
def choose_pymemcache_driver() -> str: ... | ||
|
||
REDIS_DRIVER: Final[str] | ||
DJANGO_POSTGRES: Final[str] | ||
PYMEMCACHE_DRIVER: Final[str] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
from _typeshed import StrPath | ||
|
||
# Use aliases to avoid name conflicts with Path methods | ||
from builtins import ( | ||
bool as _bool, | ||
bytes as _bytes, | ||
dict as _dict, | ||
float as _float, | ||
int as _int, | ||
list as _list, | ||
str as _str, | ||
tuple as _tuple, | ||
) | ||
from collections.abc import Callable, Mapping, MutableMapping | ||
from logging import Logger | ||
from typing import IO, Any, ClassVar, SupportsIndex, TypedDict, TypeVar, overload, type_check_only | ||
from typing_extensions import Required, TypeAlias, Unpack | ||
from urllib.parse import ParseResult | ||
|
||
from .fileaware_mapping import FileAwareMapping | ||
|
||
Openable: tuple[type, ...] | ||
logger: Logger | ||
|
||
class NoValue: ... | ||
|
||
_T = TypeVar("_T") | ||
_Cast: TypeAlias = Callable[[str], _T] | ||
_SchemeValue: TypeAlias = _Cast[Any] | tuple[_Cast[Any], Any] | ||
_EmptyDict: TypeAlias = dict[object, object] # stands for {} | ||
|
||
@type_check_only | ||
class CastDict(TypedDict, total=False): | ||
key: _Cast[Any] | ||
value: _Cast[Any] | ||
cast: dict[str, _Cast[Any]] | ||
|
||
@type_check_only | ||
class PathKwargs(TypedDict, total=False): | ||
required: bool | ||
is_file: bool | ||
|
||
# https://docs.djangoproject.com/en/5.2/ref/settings/#std-setting-DATABASES | ||
@type_check_only | ||
class MemoryDbConfig(TypedDict): | ||
ENGINE: str | ||
NAME: str | ||
|
||
@type_check_only | ||
class DbConfig(MemoryDbConfig, total=False): | ||
USER: Required[str] | ||
PASSWORD: Required[str] | ||
HOST: Required[str] | ||
PORT: Required[int | str] | ||
# Additional base options read from queryParams | ||
CONN_MAX_AGE: int | ||
CONN_HEALTH_CHECKS: bool | ||
ATOMIC_REQUESTS: bool | ||
AUTOCOMMIT: bool | ||
DISABLE_SERVER_SIDE_CURSORS: bool | ||
# Remaining options read from queryParams | ||
OPTIONS: dict[str, Any] | ||
|
||
@type_check_only | ||
class EmailConfig(TypedDict, total=False): | ||
EMAIL_FILE_PATH: Required[str] | ||
EMAIL_HOST_USER: Required[str] | ||
EMAIL_HOST_PASSWORD: Required[str] | ||
EMAIL_HOST: Required[str] | ||
EMAIL_PORT: Required[int] | ||
EMAIL_BACKEND: Required[str] | ||
# Additional base options read from queryParams | ||
EMAIL_USE_TLS: bool | ||
EMAIL_USE_SSL: bool | ||
# Remaining options read from queryParams | ||
OPTIONS: dict[str, Any] | ||
|
||
# https://github.com/django/channels | ||
@type_check_only | ||
class ChannelsConfig(TypedDict, total=False): | ||
BACKEND: Required[str] | ||
CONFIG: dict[str, Any] | ||
|
||
# https://github.com/django-haystack/django-haystack | ||
@type_check_only | ||
class SimpleSearchConfig(TypedDict, total=False): | ||
ENGINE: Required[str] | ||
# Common search params | ||
EXCLUDED_INDEXES: list[str] | ||
INCLUDE_SPELLING: bool | ||
BATCH_SIZE: int | ||
|
||
@type_check_only | ||
class SolrSearchConfig(SimpleSearchConfig, total=False): | ||
URL: Required[str] | ||
TIMEOUT: int | ||
KWARGS: dict[str, Any] | ||
|
||
@type_check_only | ||
class ElasticsearchSearchConfig(SimpleSearchConfig, total=False): | ||
URL: Required[str] | ||
TIMEOUT: int | ||
KWARGS: dict[str, Any] | ||
INDEX_NAME: str | ||
|
||
@type_check_only | ||
class WhooshSearchConfig(SimpleSearchConfig, total=False): | ||
PATH: Required[str] | ||
STORAGE: str | ||
POST_LIMIT: int | ||
|
||
@type_check_only | ||
class XapianSearchConfig(SimpleSearchConfig, total=False): | ||
PATH: Required[str] | ||
FLAGS: int | ||
|
||
# https://docs.djangoproject.com/en/5.2/ref/settings/#std-setting-CACHES | ||
@type_check_only | ||
class CacheConfig(TypedDict, total=False): | ||
BACKEND: Required[str] | ||
LOCATION: Required[str] | ||
# Additional base options read from queryParams | ||
KEY_FUNCTION: str | ||
KEY_PREFIX: str | ||
BINARY: str | ||
TIMEOUT: int | ||
VERSION: int | ||
# Remaining options read from queryParams | ||
OPTIONS: dict[str, Any] | ||
|
||
class Env: | ||
ENVIRON: MutableMapping[_str, _str] | ||
NOTSET: ClassVar[NoValue] | ||
BOOLEAN_TRUE_STRINGS: ClassVar[_tuple[str, ...]] | ||
URL_CLASS: ClassVar[type[ParseResult]] | ||
POSTGRES_FAMILY: ClassVar[_list[_str]] | ||
DEFAULT_DATABASE_ENV: ClassVar[_str] = "DATABASE_URL" | ||
DB_SCHEMES: ClassVar[_dict[_str, _str]] | ||
DEFAULT_CACHE_ENV: ClassVar[_str] = "CACHE_URL" | ||
CACHE_SCHEMES: ClassVar[_dict[_str, _str]] | ||
DEFAULT_EMAIL_ENV: ClassVar[_str] = "EMAIL_URL" | ||
EMAIL_SCHEMES: ClassVar[_dict[_str, _str]] | ||
DEFAULT_SEARCH_ENV: ClassVar[_str] = "SEARCH_URL" | ||
SEARCH_SCHEMES: ClassVar[_dict[_str, _str]] | ||
ELASTICSEARCH_FAMILY: ClassVar[_list[_str]] | ||
CLOUDSQL: ClassVar[_str] | ||
DEFAULT_CHANNELS_ENV: ClassVar[_str] = "CHANNELS_URL" | ||
CHANNELS_SCHEMES: ClassVar[_dict[_str, _str]] | ||
smart_cast: _bool | ||
escape_proxy: _bool | ||
prefix: _str | ||
scheme: Mapping[_str, _SchemeValue] | ||
|
||
def __init__(self, **scheme: _SchemeValue) -> None: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: _Cast[_T] | None = None, default: _T | NoValue = ..., parse_default: _bool = False | ||
) -> _T: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: _list[_Cast[_T]], default: _list[_T] | NoValue = ..., parse_default: _bool = False | ||
) -> _list[_T]: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: _tuple[_Cast[_T]], default: _tuple[_T] | NoValue = ..., parse_default: _bool = False | ||
) -> _tuple[_T]: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: CastDict, default: _dict[Any, Any] | NoValue = ..., parse_default: _bool = False | ||
) -> _dict[Any, Any]: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: type[_list[Any]], default: _list[_str] | NoValue = ..., parse_default: _bool = False | ||
) -> _list[_str]: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: type[_tuple[Any, ...]], default: _tuple[_str, ...] | NoValue = ..., parse_default: _bool = False | ||
) -> _tuple[_str, ...]: ... | ||
@overload | ||
def __call__( | ||
self, var: _str, cast: type[_dict[Any, Any]], default: _dict[_str, _str] | NoValue = ..., parse_default: _bool = False | ||
) -> _dict[_str, _str]: ... | ||
def __contains__(self, var: _str) -> _bool: ... | ||
def str(self, var: _str, default: _str | NoValue = ..., multiline: _bool = False) -> _str: ... | ||
def bytes(self, var: _str, default: _bytes | NoValue = ..., encoding: _str = "utf8") -> _bytes: ... | ||
def bool(self, var: _str, default: _bool | NoValue = ...) -> _bool: ... | ||
def int(self, var: _str, default: _int | NoValue = ...) -> _int: ... | ||
def float(self, var: _str, default: _float | NoValue = ...) -> _float: ... | ||
def json(self, var: _str, default: Any | NoValue = ...) -> Any: ... | ||
def list(self, var: _str, cast: _Cast[_T] | None = None, default: _list[_T] | NoValue = ...) -> _list[_T]: ... | ||
def tuple(self, var: _str, cast: _Cast[_T] | None = None, default: _tuple[_T] | NoValue = ...) -> _tuple[_T]: ... | ||
@overload | ||
def dict(self, var: _str, cast: None = ..., default: _dict[_str, Any] | NoValue = ...) -> _dict[_str, Any]: ... | ||
@overload | ||
def dict(self, var: _str, cast: _Cast[_T], default: _dict[Any, _T] | NoValue = ...) -> _dict[Any, _T]: ... | ||
def url(self, var: _str, default: _str | NoValue = ...) -> _str: ... | ||
def db_url( | ||
self, var: _str = ..., default: _str | NoValue = ..., engine: _str | None = None | ||
) -> MemoryDbConfig | DbConfig | _EmptyDict: ... | ||
|
||
db = db_url | ||
|
||
def cache_url( | ||
self, var: _str = ..., default: _str | NoValue = ..., backend: _str | None = None | ||
) -> CacheConfig | _EmptyDict: ... | ||
|
||
cache = cache_url | ||
|
||
def email_url(self, var: _str = ..., default: _str | NoValue = ..., backend: _str | None = None) -> EmailConfig: ... | ||
|
||
email = email_url | ||
|
||
def search_url( | ||
self, var: _str = ..., default: _str | NoValue = ..., engine: _str | None = None | ||
) -> SimpleSearchConfig | SolrSearchConfig | ElasticsearchSearchConfig | WhooshSearchConfig | XapianSearchConfig: ... | ||
def channels_url(self, var: _str = ..., default: _str | NoValue = ..., backend: _str | None = None) -> ChannelsConfig: ... | ||
|
||
channels = channels_url | ||
|
||
def path(self, var: _str, default: _str | NoValue = ..., **kwargs: Unpack[PathKwargs]) -> Path: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: _Cast[_T] | None = None, default: _T | NoValue = ..., parse_default: _bool = False | ||
) -> _T: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: _list[_Cast[_T]], default: _list[_T] | NoValue = ..., parse_default: _bool = False | ||
) -> _list[_T]: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: _tuple[_Cast[_T]], default: _tuple[_T] | NoValue = ..., parse_default: _bool = False | ||
) -> _tuple[_T]: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: CastDict, default: _dict[Any, Any] | NoValue = ..., parse_default: _bool = False | ||
) -> _dict[Any, Any]: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: type[_list[Any]], default: _list[_str] | NoValue = ..., parse_default: _bool = False | ||
) -> _list[_str]: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: type[_tuple[Any, ...]], default: _tuple[_str, ...] | NoValue = ..., parse_default: _bool = False | ||
) -> _tuple[_str, ...]: ... | ||
@overload | ||
def get_value( | ||
self, var: _str, cast: type[_dict[Any, Any]], default: _dict[_str, _str] | NoValue = ..., parse_default: _bool = False | ||
) -> _dict[_str, _str]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: None) -> _str: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: _Cast[_T]) -> _T: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: _list[_Cast[_T]]) -> _list[_T]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: _tuple[_Cast[_T], ...]) -> _tuple[_T]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: CastDict) -> _dict[Any, Any]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: type[_list[Any]]) -> _list[_str]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: type[_tuple[Any, ...]]) -> _tuple[_str]: ... | ||
@classmethod | ||
@overload | ||
def parse_value(cls, value: _str, cast: type[_dict[Any, Any]]) -> _dict[_str, _str]: ... | ||
@classmethod | ||
def db_url_config(cls, url: _str | ParseResult, engine: _str | None = None) -> MemoryDbConfig | DbConfig | _EmptyDict: ... | ||
@classmethod | ||
def cache_url_config(cls, url: _str | ParseResult, backend: _str | None = None) -> CacheConfig | _EmptyDict: ... | ||
@classmethod | ||
def email_url_config(cls, url: _str | ParseResult, backend: _str | None = None) -> EmailConfig: ... | ||
@classmethod | ||
def channels_url_config(cls, url: _str | ParseResult, backend: _str | None = None) -> ChannelsConfig: ... | ||
@classmethod | ||
def search_url_config( | ||
cls, url: _str | ParseResult, engine: _str | None = None | ||
) -> SimpleSearchConfig | SolrSearchConfig | ElasticsearchSearchConfig | WhooshSearchConfig | XapianSearchConfig: ... | ||
@classmethod | ||
def read_env( | ||
cls, | ||
env_file: StrPath | None = None, | ||
overwrite: _bool = False, | ||
parse_comments: _bool = False, | ||
encoding: _str = "utf8", | ||
**overrides: _dict[_str, _str], | ||
) -> None: ... | ||
|
||
class FileAwareEnv(Env): | ||
ENVIRON: FileAwareMapping | ||
|
||
class Path: | ||
def path(self, *paths: str, **kwargs: Unpack[PathKwargs]) -> Path: ... | ||
def file(self, name: str, *args, **kwargs) -> IO[str]: ... | ||
@property | ||
def root(self) -> str: ... | ||
|
||
__root__: str | ||
|
||
def __init__(self, start: str = "", *paths: str, **kwargs: Unpack[PathKwargs]) -> None: ... | ||
def __call__(self, *paths: str, **kwargs: Unpack[PathKwargs]) -> str: ... | ||
def __eq__(self, other: object | Path) -> bool: ... | ||
def __ne__(self, other: object | Path) -> bool: ... | ||
def __add__(self, other: object | Path) -> Path: ... | ||
def __sub__(self, other: int | str) -> Path: ... | ||
def __invert__(self) -> Path: ... | ||
def __contains__(self, item: Path) -> bool: ... | ||
def __unicode__(self) -> str: ... | ||
@overload | ||
def __getitem__(self, key: SupportsIndex, /) -> str: ... | ||
@overload | ||
def __getitem__(self, key: slice, /) -> str: ... | ||
def __fspath__(self) -> str: ... | ||
def rfind(self, s: str, sub: str, start: int = 0, end: int = ...) -> int: ... | ||
def find(self, s: str, sub: str, start: int = 0, end: int = ...) -> int: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from collections.abc import Iterator, MutableMapping | ||
|
||
class FileAwareMapping(MutableMapping[str, str]): | ||
env: MutableMapping[str, str] | ||
cache: bool | ||
files_cache: dict[str, str] | ||
def __init__(self, env: MutableMapping[str, str] | None = None, cache: bool = True) -> None: ... | ||
def __getitem__(self, key: str) -> str: ... | ||
def __iter__(self) -> Iterator[str]: ... | ||
def __len__(self) -> int: ... | ||
def __setitem__(self, key: str, value: str) -> None: ... | ||
def __delitem__(self, key: str) -> None: ... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@donBarbos @brianschubert Any idea on how to fix the
stub_uploader
test which fails after adding thedjango-stubs
requirement? If I am not mistaken it checks whether the original package requiresdjango
, which it doesn't (could be used without).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could pay attention to how this is done in other stubs for "django" packages, like
django-filter
METADATA.toml
ordjango-import-export
METADATA.toml
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah that could be a problem. I think you'll need to rollback the
ImproperlyConfigured
change and avoid any otherdjango
imports in the stubs. You can just add a note thatImproperlyConfigured
re-exports the django exception when available