Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyrightconfig.stricter.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"stubs/cffi",
"stubs/dateparser",
"stubs/defusedxml",
"stubs/django-environ",
"stubs/docker",
"stubs/docutils",
"stubs/Flask-SocketIO",
Expand Down
64 changes: 64 additions & 0 deletions stubs/django-environ/@tests/test_cases/check_parse_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import annotations

from typing import Union
from typing_extensions import TypedDict, assert_type

import environ

env = environ.Env()

assert_type(env.parse_value("just-a-value123", None), str)

# builtin types
assert_type(env.parse_value("string", str), str)
assert_type(env.parse_value("TRUE", bool), bool)
assert_type(env.parse_value("2000", int), int)
assert_type(env.parse_value("-500.01", float), float)
assert_type(env.parse_value("first,second,", list), list[str])
assert_type(env.parse_value("(first,second)", tuple), tuple[str, ...])
assert_type(env.parse_value("a=first,b=second", dict), dict[str, str])


# cast list values (first list element is used)
assert_type(env.parse_value("20.5,-0.2", [str]), list[str])
assert_type(env.parse_value("20.5,-0.2", [bool]), list[bool])
assert_type(env.parse_value("20.5,-0.2", [int]), list[int])
assert_type(env.parse_value("20.5,-0.2", [float]), list[float])

# cast tuple values (first tuple element is used)
assert_type(env.parse_value("(20.5,-0.2)", (str,)), tuple[str, ...])
assert_type(env.parse_value("(20.5,-0.2)", (bool,)), tuple[bool, ...])
assert_type(env.parse_value("(20.5,-0.2)", (int,)), tuple[int, ...])
assert_type(env.parse_value("(20.5,-0.2)", (float,)), tuple[float, ...])

# cast dict values
assert_type(env.parse_value("0=TRUE,99=FALSE", {}), dict[str, str])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"cast": {}}), dict[str, Union[str, object]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the inferred type for this case should ideally be dict[str, str]. Inferring dict[str, object] makes this awkward to use for end users, since you can't use the values in the dictionary without casting them or using a type narrowing construct

assert_type(env.parse_value("0=TRUE,99=FALSE", {"value": bool}), dict[str, bool])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"value": bool, "cast": {}}), dict[str, Union[bool, object]])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"key": int}), dict[int, str])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"key": int, "cast": {}}), dict[int, Union[str, object]])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"key": int, "value": bool}), dict[int, bool])
assert_type(env.parse_value("0=TRUE,99=FALSE", {"key": int, "value": bool, "cast": {}}), dict[int, Union[bool, object]])


# custom cast functions
def cast_float(x: str) -> float:
return float(x)


assert_type(env.parse_value("20.5", cast_float), float)


class Person(TypedDict):
first_name: str
last_name: str
age: int


def cast_person(v: str) -> Person:
parts = v.split(",")
return {"first_name": parts[0], "last_name": parts[1], "age": int(parts[2])}
Comment on lines +59 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to include a runtime implementation since these tests aren't executed (we only check how they type check). I think this case is also redundant with custom cast function case above



assert_type(env.parse_value("Bob,Riveira,30", cast_person), Person)
2 changes: 2 additions & 0 deletions stubs/django-environ/METADATA.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = "0.12.*"
upstream_repository = "https://github.com/joke2k/django-environ"
13 changes: 13 additions & 0 deletions stubs/django-environ/environ/__init__.pyi
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are indirectly re-exported from .environ, so let's move the import there

Suggested change
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]
12 changes: 12 additions & 0 deletions stubs/django-environ/environ/compat.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Final

# reexports ImproperlyConfigured from django.core.exceptions if available
class ImproperlyConfigured(Exception): ...

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]
Loading
Loading