Skip to content
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

Add support in Model for mimicking the setting and deletion of properties #165

Open
github-actions bot opened this issue Jan 5, 2024 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Jan 5, 2024

def test_mimic_property():

class MyMetadata:

def __init__(self) -> None:

self._metadata: str | None = 'metadata'

@Property

def metadata(self):

return self._metadata

@metadata.setter

def metadata(self, value: str | None):

self._metadata = value

if value is None:

raise ValueError('Not allowed to reset to None')

@metadata.deleter

def metadata(self):

self._metadata = None

raise ValueError('Not allowed to delete metadata')

model = Model[MyMetadata]()

assert model.metadata == 'metadata'

model.metadata = 'my metadata'

assert model.metadata == 'my metadata'

with pytest.raises(ValueError):

model.metadata = None

assert model.metadata == 'my metadata'

with pytest.raises(ValueError):

del model.metadata

assert model.metadata == 'my metadata'

# TODO: Add support in Model for mimicking the setting and deletion of properties

    assert float(model) == 10  # float(), int(), etc always converts


# TODO: Add support in Model for mimicking the setting and deletion of properties
# def test_mimic_property():
#     class MyMetadata:
#         def __init__(self) -> None:
#             self._metadata: str | None = 'metadata'
#
#         @property
#         def metadata(self):
#             return self._metadata
#
#         @metadata.setter
#         def metadata(self, value: str | None):
#             self._metadata = value
#
#             if value is None:
#                 raise ValueError('Not allowed to reset to None')
#
#         @metadata.deleter
#         def metadata(self):
#             self._metadata = None
#             raise ValueError('Not allowed to delete metadata')
#
#
#     model = Model[MyMetadata]()
#     assert model.metadata == 'metadata'
#
#     model.metadata = 'my metadata'
#     assert model.metadata == 'my metadata'
#
#     with pytest.raises(ValueError):
#         model.metadata = None
#
#     assert model.metadata == 'my metadata'
#
#     with pytest.raises(ValueError):
#         del model.metadata
#     assert model.metadata == 'my metadata'


def test_mimic_callable_property():
    # Example of previously failing callable property is pandas.DataFrame.loc

    class MyCallable:
        def __init__(self):
            self.called = False

        def __call__(self):
            self.called = True

    class MyCallableHolder:
        def __init__(self) -> None:
            self._func: Callable = MyCallable()

        @property
        def func(self) -> Callable:
            return self._func

    model = Model[MyCallableHolder]()
    assert model.func.called is False


def test_model_copy():
    ...
@github-actions github-actions bot added the todo label Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants