Skip to content

Commit

Permalink
typehint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Mar 15, 2024
1 parent 1ddaa2d commit 01e71d7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
26 changes: 13 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
repos:
- repo: local
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
name: black
language: python
entry: black
require_serial: true
types_or: [python, pyi]
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
name: flake8
language: python
entry: flake8
require_serial: true
types_or: [python, pyi]
additional_dependencies:
- flake8-bugbear
- flake8-comprehensions
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort

default_language_version:
python: python3.9

python: python3.11
2 changes: 1 addition & 1 deletion ragdoll/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__version__ = "0.2.0"
__author__ = "Aaron Zhang <[email protected]>"

from ragdoll import env, base, errors, django
from ragdoll import base, django, env, errors

__all__ = ["env", "base", "errors", "django"]
2 changes: 1 addition & 1 deletion ragdoll/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from typing import Any, Type, Union, Optional, Mapping
from typing import Any, Mapping, Optional, Type, Union

from ragdoll import errors, utils

Expand Down
28 changes: 21 additions & 7 deletions ragdoll/env.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import functools
import os
import typing

from ragdoll import utils, errors
from ragdoll import errors, utils
from ragdoll.base import BaseEntry, BaseSetting

__all__ = ["EnvSetting", "BaseEnvEntry", "StrEnv", "BoolEnv", "IntEnv"]
Expand All @@ -22,6 +21,10 @@ class StrEnv(BaseEnvEntry):
def to_python(self, value: str) -> str:
return value

if typing.TYPE_CHECKING:

def __get__(self, *args, **kwargs) -> str: ...


class IntEnv(BaseEnvEntry):
def __init__(self, *args, base: int = 10, **kwargs):
Expand All @@ -36,6 +39,10 @@ def to_python(self, value: str) -> int:
f"Cannot convert {self._name} to integer"
) from value_error

if typing.TYPE_CHECKING:

def __get__(self, *args, **kwargs) -> int: ...


class BoolEnv(BaseEnvEntry):
TRUE_VALUES = {"true", "1", "yes"}
Expand All @@ -55,14 +62,21 @@ def to_python(self, value: str) -> bool:
f"{(self.TRUE_VALUES | self.FALSE_VALUES)!r}"
)

if typing.TYPE_CHECKING:

def __get__(self, *args, **kwargs) -> bool: ...


class EnvSetting(BaseSetting):
case_sensitive = True
_source: typing.Optional[typing.Mapping] = None

@utils.classproperty
@functools.lru_cache(maxsize=None)
def source(cls) -> typing.Mapping:
if cls.case_sensitive:
return os.environ
else:
return {k.lower(): v for k, v in os.environ.items()}
if not cls._source:
if cls.case_sensitive:
cls._source = os.environ
else:
cls._source = {k.lower(): v for k, v in os.environ.items()}

return cls._source

0 comments on commit 01e71d7

Please sign in to comment.