Skip to content

Commit

Permalink
Merge 57cff53 into a64e6c8
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Dec 8, 2022
2 parents a64e6c8 + 57cff53 commit 5addb76
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Human(ZnInit):
language: str = Input("DE")
date: str = Metric() # will not appear in the __init__

def post_init(self):
def _post_init_(self):
self.date = "2022-09-16"


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "zninit"
version = "0.1.5"
version = "0.1.6"
description = "Descriptor based dataclass implementation"
authors = ["zincwarecode <zincwarecode@gmail.com>"]
license = "Apache-2.0"
Expand Down
40 changes: 40 additions & 0 deletions tests/test_AutomaticInit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test for the automatic __init__."""
import pytest

import zninit
from zninit import Descriptor, ZnInit

# autocomplete issue https://youtrack.jetbrains.com/issue/PY-38072
Expand Down Expand Up @@ -199,3 +200,42 @@ def test_OnlyParamsInInit(cls):

with pytest.raises(TypeError):
cls(parameter=10, output=25)


class PostInitA(zninit.ZnInit):
"""Class with 'post_init'."""

parameter = zninit.Descriptor()
called = False

def post_init(self):
"""Post init call."""
self.called = True


class PostInitB(zninit.ZnInit):
"""Class with '_post_init_'."""

parameter = zninit.Descriptor()
called = False

def _post_init_(self):
"""Post init call."""
self.called = True


class PostInitC(zninit.ZnInit):
"""Class with '__post_init__'."""

parameter = zninit.Descriptor()
called = False

def __post_init__(self):
"""Post init call."""
self.called = True


@pytest.mark.parametrize("cls", (PostInitA, PostInitB, PostInitC))
def test_post_init_called(cls):
"""Test different versions of '_post_init_'."""
assert cls(parameter="Test").called
2 changes: 1 addition & 1 deletion tests/test_zninit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

def test_version():
"""Test the installed version."""
assert zninit.__version__ == "0.1.5"
assert zninit.__version__ == "0.1.6"
11 changes: 5 additions & 6 deletions zninit/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Functionality to generate the automatic __init__."""
from __future__ import annotations

import contextlib
import logging
import typing
from copy import deepcopy
Expand Down Expand Up @@ -105,16 +104,17 @@ def auto_init(self, *args, **kwargs):
for key, value in init_kwargs.items():
setattr(self, key, value)

with contextlib.suppress(AttributeError):
self.post_init()
for post_init in ["__post_init__", "_post_init_", "post_init"]:
if hasattr(self, post_init):
getattr(self, post_init)()

# we add this attribute to the __init__ to make it identifiable
auto_init.uses_auto_init = True

return auto_init


class ZnInit:
class ZnInit: # pylint: disable=R0903
"""Parent class for automatic __init__ generation based on descriptors.
Attributes
Expand Down Expand Up @@ -276,10 +276,9 @@ def __repr__(self):
repr_str += ")"
return repr_str

def post_init(self):
def _post_init_(self):
"""Implement if cmds after the automatically generated __init__ should be run.
This only works if no __init__ is defined and the automatically generated
__init__ is used.
"""
raise AttributeError(f"'{self}' object has no attribute 'post_init'")

0 comments on commit 5addb76

Please sign in to comment.