-
-
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
base: main
Are you sure you want to change the base?
Add stubs for django-environ #14573
Conversation
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Thanks! I just want to note that the project doesn’t seem to be very actively maintained (the last commit was 7 months ago), so I wouldn’t expect type hints to be added to the library itself anytime soon. FTR: here’s a recent issue essentially complaining about the lack of type hints: joke2k/django-environ#567, and another one joke2k/django-environ#365 that might give some insight into the maintainer’s stance on adding them. |
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.
Looks good! Just few moments:
__copyright__: str | ||
__version__: str | ||
__license__: str | ||
__author_email__: str | ||
__maintainer__: str | ||
__maintainer_email__: str | ||
__url__: str | ||
__description__: str |
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 these variables can be marked as Final (but need to import Final
from typing
)
__copyright__: str | |
__version__: str | |
__license__: str | |
__author_email__: str | |
__maintainer__: str | |
__maintainer_email__: str | |
__url__: str | |
__description__: str | |
__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] |
REDIS_DRIVER: str | ||
DJANGO_POSTGRES: str | ||
PYMEMCACHE_DRIVER: str |
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.
REDIS_DRIVER: str | |
DJANGO_POSTGRES: str | |
PYMEMCACHE_DRIVER: str | |
REDIS_DRIVER: Final[str] | |
DJANGO_POSTGRES: Final[str] | |
PYMEMCACHE_DRIVER: Final[str] |
def choose_rediscache_driver(): ... | ||
def choose_postgres_driver(): ... | ||
def choose_pymemcache_driver(): ... |
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.
def choose_rediscache_driver(): ... | |
def choose_postgres_driver(): ... | |
def choose_pymemcache_driver(): ... | |
def choose_rediscache_driver() -> str: ... | |
def choose_postgres_driver() -> str: ... | |
def choose_pymemcache_driver() -> str: ... |
_Str = str | ||
_Bytes = bytes | ||
_Bool = bool | ||
_Int = int | ||
_Float = float | ||
_List = list | ||
_Tuple = tuple | ||
_Dict = dict |
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.
Looks like internal types, I think it's okay to miss them
_Str = str | |
_Bytes = bytes | |
_Bool = bool | |
_Int = int | |
_Float = float | |
_List = list | |
_Tuple = tuple | |
_Dict = dict |
ENVIRON: MutableMapping[_Str, _Str] | ||
NOTSET: ClassVar[NoValue] | ||
BOOLEAN_TRUE_STRINGS: ClassVar[_BooleanTrueStrings] | ||
URL_CLASS: ClassVar[type[ParseResult]] | ||
POSTGRES_FAMILY: ClassVar[_List[_Str]] | ||
DEFAULT_DATABASE_ENV: ClassVar[_Str] = "DATABASE_URL" | ||
DB_SCHEMES: ClassVar[_Dict[_Str, _Str]] |
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.
no need to use aliases for builtin types, it's more confusing
ENVIRON: MutableMapping[_Str, _Str] | |
NOTSET: ClassVar[NoValue] | |
BOOLEAN_TRUE_STRINGS: ClassVar[_BooleanTrueStrings] | |
URL_CLASS: ClassVar[type[ParseResult]] | |
POSTGRES_FAMILY: ClassVar[_List[_Str]] | |
DEFAULT_DATABASE_ENV: ClassVar[_Str] = "DATABASE_URL" | |
DB_SCHEMES: ClassVar[_Dict[_Str, _Str]] | |
ENVIRON: MutableMapping[str, str] | |
NOTSET: ClassVar[NoValue] | |
BOOLEAN_TRUE_STRINGS: ClassVar[_BooleanTrueStrings] | |
URL_CLASS: ClassVar[type[ParseResult]] | |
POSTGRES_FAMILY: ClassVar[list[str]] | |
DEFAULT_DATABASE_ENV: ClassVar[str] = "DATABASE_URL" | |
DB_SCHEMES: ClassVar[dict[str, str]] |
I didn't change everything, but you get the idea :)
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.
This won't work because the Env
class defines methods def str(...)
, def list(...)
, etc. overwriting the builtin types. That was the reason for the aliases. ;) Alternatively, I could explicitly use builtins.str
, builtins.list
, etc. everywhere in the Env
class. What do you think?
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, that makes sense now :)
I think the typical approach for dealing with conflicts like that is to import the builtins symbols under an alias, e.g. from builtins import list as _list
. Please also add a comment mentioning the conflict
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.
Thanks! Looks good overall, see remarks below
__copyright__: str | ||
__version__: str | ||
__license__: str | ||
__author_email__: str | ||
__maintainer__: str | ||
__maintainer_email__: str | ||
__url__: str | ||
__description__: str |
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.
These look like they can be marked as Final
@@ -0,0 +1,9 @@ | |||
class ImproperlyConfigured(Exception): ... |
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.
This re-exports ImproperlyConfigured
from django, so I think you could use
class ImproperlyConfigured(Exception): ... | |
from django.core.exceptions import ImproperlyConfigured as ImproperlyConfigured |
You'll need to add a django-stubs
requirement to METADATA.toml
. See some of the other django stubs for examples
def choose_rediscache_driver(): ... | ||
def choose_postgres_driver(): ... | ||
def choose_pymemcache_driver(): ... | ||
|
||
REDIS_DRIVER: str | ||
DJANGO_POSTGRES: str | ||
PYMEMCACHE_DRIVER: str |
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.
def choose_rediscache_driver(): ... | |
def choose_postgres_driver(): ... | |
def choose_pymemcache_driver(): ... | |
REDIS_DRIVER: str | |
DJANGO_POSTGRES: str | |
PYMEMCACHE_DRIVER: str | |
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] |
# Some type aliases to make our life easier | ||
_Str = str | ||
_Bytes = bytes | ||
_Bool = bool | ||
_Int = int | ||
_Float = float | ||
_List = list | ||
_Tuple = tuple | ||
_Dict = dict |
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'm not sure I see the value of these aliases. Can you switch to using the builtin types directly?
_T = TypeVar("_T") | ||
_Cast: TypeAlias = Callable[[_Str], _T] | ||
_SchemeValue: TypeAlias = _Cast[Any] | tuple[_Cast[Any], Any] | ||
_BooleanTrueStrings: TypeAlias = tuple[str, ...] |
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.
It looks like this alias is only used once, so I'm not sure it really pulls its weight. I'd suggest inlining it below
_Dict = dict | ||
|
||
_T = TypeVar("_T") | ||
_Cast: TypeAlias = Callable[[_Str], _T] |
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.
See comment on parse_value
def db_url_config(cls, url: _Str | ParseResult, engine: _Str | None = None) -> _Dict: ... | ||
@classmethod | ||
def cache_url_config(cls, url: _Str | ParseResult, backend: _Str | None = None) -> _Dict: ... | ||
@classmethod | ||
def email_url_config(cls, url: _Str | ParseResult, backend: _Str | None = None) -> _Dict: ... | ||
@classmethod | ||
def channels_url_config(cls, url: _Str | ParseResult, backend: _Str | None = None) -> _Dict: ... | ||
@classmethod | ||
def search_url_config(cls, url: _Str | ParseResult, engine: _Str | None = None) -> _Dict: ... |
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.
These could return TypedDicts since the keys appear to be fixed
@classmethod | ||
def read_env( | ||
cls, | ||
env_file: _Str | os.PathLike[_Str] | None = None, |
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.
This could use StrPath
(import from _typeshed
)
class FileAwareEnv(Env): | ||
ENVIRON: FileAwareMapping | ||
|
||
class _PathKwargs(TypedDict, total=False): |
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.
class _PathKwargs(TypedDict, total=False): | |
@type_check_only | |
class _PathKwargs(TypedDict, total=False): |
env: dict[str, str] | ||
cache: bool | ||
files_cache: dict[str, str] | ||
def __init__(self, env: dict[str, str] | None = None, cache: bool = True) -> None: ... |
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.
Looks like env
could be relaxed to MutableMapping[str, str]
Types for
django-environ
.