Skip to content

Commit

Permalink
Merge 8434da4 into 1ebd121
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Nov 22, 2022
2 parents 1ebd121 + 8434da4 commit a43d217
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 209 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

19 changes: 11 additions & 8 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ jobs:
flake8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install flake8
run: |
pip install flake8==4.0.1
- name: run flake8
python-version: "3.10"
- name: Install dependencies
run: |
flake8 . --count --show-source --statistics
python -m pip install --upgrade pip
pip install ruff
- name: Run Ruff
run: ruff .

pylint:
runs-on: ubuntu-latest
Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ repos:
hooks:
- id: isort

- repo: https://gitlab.com/pycqa/flake8
rev: 5.0.4
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.126
hooks:
- id: flake8
additional_dependencies: [flake8-isort]
- id: ruff


- repo: local
hooks:
Expand Down
239 changes: 89 additions & 150 deletions poetry.lock

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ black = "^22.8.0"
isort = "^5.10.1"
jupyterlab = "^3.4.7"
coverage = "^6.4.4"
flake8 = "^5.0.4"
pylint = "^2.15.2"
perflint = "^0.7.3"
pre-commit = "^2.20.0"
ruff = "^0.0.105"
ruff = "^0.0.126"

[build-system]
requires = ["poetry-core"]
Expand Down Expand Up @@ -49,3 +48,31 @@ disable = [

[tool.ruff]
line-length = 90

select = ["E", "F", "D", "N", "C"] #, "ANN"]
extend-ignore = [
"D213", "D203", "N802"
]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".hg",
".mypy_cache",
".nox",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"tmp",
]
34 changes: 33 additions & 1 deletion tests/test_AutomaticInit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Test for the automatic __init__."""
import pytest

from zninit import Descriptor, ZnInit
Expand All @@ -6,66 +7,90 @@


class Params(Descriptor):
"""Descriptor child."""

pass


class Outs(Descriptor):
"""Descriptor child."""

pass


class SingleClsDefaults(ZnInit):
"""ZnInit child."""

param1 = Descriptor()
param2 = Descriptor("World")


class ParentClsPlain(ZnInit):
"""A parent class without any changes"""
"""A parent class without any changes."""


class ChildPlainCls(ParentClsPlain):
"""ZnInit child."""

parameter: int = Params()


class ChildPlainClsInit(ParentClsPlain):
"""ZnInit child."""

def __init__(self, parameter):
"""Define __init__."""
super().__init__()
self.parameter = parameter


class ParentCls(ZnInit):
"""ZnInit child."""

parameter: int = Params()


class DefaultIsNone(ZnInit):
"""ZnInit child."""

parameter: int = Descriptor(None)


class ChildCls(ParentCls):
"""ZnInit child."""

pass


class OnlyParamsInInit(ZnInit):
"""ZnInit child."""

init_descriptors = [Params]

parameter: int = Params()
output = Outs()


class ChildClsInit(ParentCls):
"""ZnInit child."""

def __init__(self, parameter, value):
"""Define __init__."""
super().__init__(
parameter=parameter
) # super(ChildClsInit, self).__init__ does not work!
self.value = value


def test_ParentClsPlain():
"""ZnInit Test."""
_ = ParentClsPlain()
with pytest.raises(TypeError):
_ = ParentClsPlain(paramter=10)


def test_ChildPlainCls():
"""ZnInit Test."""
with pytest.raises(TypeError) as err:
_ = ChildPlainCls()

Expand Down Expand Up @@ -98,11 +123,13 @@ def test_ChildPlainCls():


def test_ChildPlainClsInit():
"""ZnInit Test."""
x = ChildPlainClsInit(parameter=10)
assert x.parameter == 10


def test_ParentCls():
"""ZnInit Test."""
with pytest.raises(TypeError) as err:
_ = ParentCls()

Expand All @@ -116,6 +143,7 @@ def test_ParentCls():


def test_ChildCls():
"""ZnInit Test."""
with pytest.raises(TypeError) as err:
_ = ChildCls()

Expand All @@ -129,12 +157,14 @@ def test_ChildCls():


def test_ChildClsInit():
"""ZnInit Test."""
child = ChildClsInit(parameter=10, value=5)
assert child.parameter == 10
assert child.value == 5


def test_SingleClsDefaults():
"""ZnInit Test."""
instance = SingleClsDefaults(param1="Hello")
assert instance.param1 + " " + instance.param2 == "Hello World"

Expand All @@ -143,13 +173,15 @@ def test_SingleClsDefaults():


def test_DefaultIsNone():
"""ZnInit Test."""
instance = DefaultIsNone()
assert instance.parameter is None
instance = DefaultIsNone(parameter=42)
assert instance.parameter == 42


def test_OnlyParamsInInit():
"""ZnInit Test."""
instance = OnlyParamsInInit(parameter=10)
assert instance.parameter == 10
with pytest.raises(AttributeError):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_descriptor.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
"""Test ZnInit descriptors."""
import pytest

from zninit import Descriptor, get_descriptors


class ExampleCls:
"""Example class."""

desc = Descriptor()


class ExampleChild(ExampleCls):
"""Example Child."""

pass


def test_get_descriptor():
"""ZnInit test."""
assert isinstance(ExampleCls.desc, Descriptor)


def test_descriptor_name():
"""ZnInit test."""
descriptor = ExampleCls.desc
assert descriptor.name == "desc"


def test_descriptor_owner():
"""ZnInit test."""
descriptor = ExampleCls.desc
assert descriptor.owner == ExampleCls


@pytest.mark.parametrize("access", ("get", "set"))
def test_get_instance(access):
"""ZnInit test."""
desc = ExampleCls.desc

cls = ExampleCls()
Expand All @@ -40,13 +49,15 @@ def test_get_instance(access):


def test_descriptor_set():
"""ZnInit test."""
desc = ExampleCls.desc
cls = ExampleCls()
cls.desc = 42
assert cls.__dict__[desc.name] == 42


def test_descriptor_get():
"""ZnInit test."""
desc = ExampleCls.desc
cls = ExampleCls()
cls.__dict__[desc.name] = 42
Expand All @@ -55,17 +66,20 @@ def test_descriptor_get():

@pytest.mark.parametrize("cls", (ExampleCls, ExampleChild))
def test_get_descriptors_cls(cls):
"""ZnInit test."""
instance = cls
assert get_descriptors(Descriptor, cls=instance) == [cls.desc]


@pytest.mark.parametrize("cls", (ExampleCls, ExampleChild))
def test_get_descriptors_self(cls):
"""ZnInit test."""
self = cls()
assert get_descriptors(Descriptor, self=self) == [cls.desc]


def test_get_descriptors_exceptions():
"""ZnInit test."""
with pytest.raises(ValueError):
get_descriptors(Descriptor)

Expand All @@ -77,14 +91,20 @@ def test_get_descriptors_exceptions():


class CustomDescriptor1(Descriptor):
"""ZnInit descriptor."""

pass


class CustomDescriptor2(Descriptor):
"""ZnInit descriptor."""

pass


class MultipleDescriptors:
"""ZnInit Descriptor container."""

desc1_1 = CustomDescriptor1()
desc1_2 = CustomDescriptor1()

Expand All @@ -93,6 +113,7 @@ class MultipleDescriptors:


def test_get_custom_descriptors():
"""ZnInit test."""
cls = MultipleDescriptors
assert get_descriptors(Descriptor, cls=cls) == [
cls.desc1_1,
Expand All @@ -115,6 +136,7 @@ def test_get_custom_descriptors():


def test_get_desc_values():
"""ZnInit test."""
self = MultipleDescriptors()

self.desc1_1 = "Hello"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_subclass_automatic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
"""Test subclass handling."""
import pytest

from zninit import Descriptor, ZnInit


class Parent(ZnInit):
"""Parent class."""

def __init__(self, **kwargs):
"""Define __init__."""
self.name = kwargs.pop("name", None)
if len(kwargs) > 0:
raise TypeError(f"'{kwargs}' are an invalid keyword argument")


class Child(Parent):
"""Child class."""

init_subclass_basecls = Parent
text = Descriptor()


def test_subclass_init():
"""Test subclass init."""
instance = Child(name="Test", text="Hello World")
assert instance.name == "Test"
assert instance.text == "Hello World"
Expand Down

0 comments on commit a43d217

Please sign in to comment.