Skip to content

Commit

Permalink
refactor: update copier for a more generic environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Aug 5, 2023
1 parent 9e05ba6 commit 8001914
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Answer file maintained by Copier for: https://github.com/KyleKing/calcipy_template
# DO NOT MODIFY THIS FILE. Edit by re-running copier and changing responses to the questions
# Check into version control.
_commit: 1.7.4
_commit: 1.7.7
_src_path: gh:KyleKing/calcipy_template
author_email: [email protected]
author_name: Kyle King
Expand Down
37 changes: 15 additions & 22 deletions calcipy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
__pkg_name__ = 'calcipy'


class _BeartypeModes(Enum):
"""Supported global beartype modes."""
class _RuntimeTypeCheckingModes(Enum):
"""Supported global runtime type checking modes."""

ERROR = 'ERROR'
WARNING = 'WARNING'
Expand All @@ -21,36 +21,29 @@ class _BeartypeModes(Enum):
@classmethod
def from_environment(cls) -> Self:
"""Return the configured mode."""
beartype_mode = getenv('BEARTYPE_MODE') or None
rtc_mode = getenv('CALCIPY_RUNTIME_TYPE_CHECKING_MODE') or None
try:
return cls(beartype_mode)
return cls(rtc_mode)
except ValueError:
msg = f"'BEARTYPE_MODE={beartype_mode}' is not an allowed mode from {[_e.value for _e in cls]}"
raise ValueError(
msg,
) from None
modes = [_e.value for _e in cls]
msg = f"'CALCIPY_RUNTIME_TYPE_CHECKING_MODE={rtc_mode}' is not an allowed mode from {modes}"
raise ValueError(msg) from None


def configure_beartype() -> None:
"""Optionally configure beartype globally."""
beartype_mode = _BeartypeModes.from_environment()
def configure_runtime_type_checking_mode() -> None:
"""Optionally configure runtime type checking mode globally."""
rtc_mode = _RuntimeTypeCheckingModes.from_environment()

if beartype_mode != _BeartypeModes.OFF:
# PLANNED: Appease mypy and pyright, but this is a private import
from beartype.roar._roarwarn import _BeartypeConfReduceDecoratorExceptionToWarningDefault
beartype_warning_default = _BeartypeConfReduceDecoratorExceptionToWarningDefault
if rtc_mode != _RuntimeTypeCheckingModes.OFF:
from beartype.roar import BeartypeClawDecorWarning

beartype_this_package(conf=BeartypeConf(
warning_cls_on_decorator_exception=(
None if beartype_mode == _BeartypeModes.ERROR else beartype_warning_default
None if rtc_mode is _RuntimeTypeCheckingModes.ERROR else BeartypeClawDecorWarning
),
is_color=getenv('BEARTYPE_NO_COLOR') is not None),
)
))


configure_beartype()

# ====== Above is the recommended code from calcipy_template and may be updated on new releases ======

configure_runtime_type_checking_mode()

# ====== Above is the recommended code from calcipy_template and may be updated on new releases ======
3 changes: 1 addition & 2 deletions docs/docs/CODE_TAG_SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

| Type | Comment | Last Edit | Source File |
|---------|----------------------------------------------------------------------------------------------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| PLANNED | Appease mypy and pyright, but this is a private import | 2023-07-22 | [calcipy/__init__.py:39](https://github.com/KyleKing/calcipy/blame/9017346addc8c8bafd922d1c48e861f77a6ee0bc/calcipy/__init__.py#L39) |
| TODO | If no stale, write out five oldest? | 2023-05-13 | [calcipy/check_for_stale_packages/_check_for_stale_packages.py:202](https://github.com/KyleKing/calcipy/blame/86c37cc4f24911db5562ef6fc8263102dbbcf5c5/calcipy/check_for_stale_packages/_check_for_stale_packages.py#L198) |
| TODO | Can I type this function with fewer Any's? | 2023-05-17 | [calcipy/cli.py:173](https://github.com/KyleKing/calcipy/blame/efaaa73193296e1fa7e82f1e9a40253c5a26da2d/calcipy/cli.py#L173) |
| TODO | Consider adding a configuration item for ignore_patterns | 2023-02-19 | [calcipy/file_search.py:82](https://github.com/KyleKing/calcipy/blame/e6bc0415e3bf6a6df5a9d808ce0e89d0f2c5df9e/calcipy/file_search.py#L82) |
Expand All @@ -12,6 +11,6 @@
| TODO | Is there an easier way to maintain pytest parameter IDs? | 2023-02-17 | [tests/tasks/test_test.py:14](https://github.com/KyleKing/calcipy/blame/785b9d1c3afda6fc5a2e46f2bc7d41ed2614da09/tests/tasks/test_test.py#L11) |
| PLANNED | Convert to hypothesis test! | 2023-02-19 | [tests/test_dot_dict.py:9](https://github.com/KyleKing/calcipy/blame/3f42ad855eb7024ff48af35d496633a87d4a14ac/tests/test_dot_dict.py#L9) |

Found code tags for TODO (7), PLANNED (2)
Found code tags for TODO (7), PLANNED (1)

<!-- calcipy_skip_tags -->
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from os import environ

environ['BEARTYPE_SHOW_WARNINGS'] = 'yes'
environ['CALCIPY_RUNTIME_TYPE_CHECKING_MODE'] = 'WARNING'

0 comments on commit 8001914

Please sign in to comment.