Skip to content

Commit

Permalink
Remove the parsable decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
tmke8 committed Oct 10, 2023
1 parent fc00e1c commit ef068ba
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 41 deletions.
16 changes: 2 additions & 14 deletions ranzen/decorators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Decorator functions."""
from __future__ import annotations
from enum import Enum
from typing import Any, Callable, Protocol, TypeVar, get_type_hints
from typing import Any, Callable, Protocol, TypeVar
from typing_extensions import deprecated

__all__ = ["enum_name_str", "implements", "parsable"]
__all__ = ["enum_name_str", "implements"]


_T = TypeVar("_T")
Expand Down Expand Up @@ -38,18 +38,6 @@ def __call__(self, func: _F) -> _F:
return func


def parsable(func: _F) -> _F:
"""Mark an object's __init__ as parsable by Configen, so only pre-3.9 type annotations should be used."""
assert func.__name__ == "__init__", "@parsable can only be used to decorate __init__."
try:
get_type_hints(func)
except TypeError:
raise ValueError(
"the type annotations of this function are not automatically parseable."
) from None
return func


E = TypeVar("E", bound=Enum)


Expand Down
3 changes: 1 addition & 2 deletions ranzen/torch/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from torch import Tensor, nn
import torch.nn.functional as F

from ranzen import parsable, str_to_enum
from ranzen import str_to_enum

__all__ = [
"CrossEntropyLoss",
Expand Down Expand Up @@ -148,7 +148,6 @@ class indices.
class CrossEntropyLoss(nn.Module):
weight: Tensor | None

@parsable
def __init__(
self,
*,
Expand Down
26 changes: 1 addition & 25 deletions tests/decorator_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Test decorators."""
from __future__ import annotations
from enum import Enum, auto
from typing import List, Union
from typing_extensions import deprecated

import pytest

from ranzen import enum_name_str, parsable # pyright: ignore
from ranzen import enum_name_str # pyright: ignore


@pytest.mark.parametrize("explanation", ["All things that have form eventually decay."])
Expand All @@ -27,29 +26,6 @@ def foo() -> None:
foo() # pyright: ignore


def test_parsable() -> None:
class Foo: # pyright: ignore
@parsable
def __init__(self) -> None:
...


def test_parsable_valid() -> None:
class Foo: # pyright: ignore
@parsable
def __init__(self, a: int, b: Union[int, float], c: List[str]):
...


def test_parsable_invalid_union() -> None:
with pytest.raises(ValueError):

class Foo: # pyright: ignore
@parsable
def __init__(self, a: int, b: int | float, c: List[str]):
...


def test_enum_str() -> None:
with pytest.deprecated_call():

Expand Down

0 comments on commit ef068ba

Please sign in to comment.