Skip to content

Commit

Permalink
Merge pull request #27 from kapicorp/pydantic-upgrade
Browse files Browse the repository at this point in the history
Upgrade pydantic to 2.6
  • Loading branch information
ademariag authored Mar 30, 2024
2 parents b34e780 + 2310189 commit 40c6e94
Show file tree
Hide file tree
Showing 8 changed files with 656 additions and 161 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length=120
10 changes: 5 additions & 5 deletions .github/workflows/python-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: ["3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand All @@ -19,13 +19,13 @@ jobs:

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install --upgrade pip poetry
poetry config virtualenvs.create false --local
poetry install --with=dev --no-root
- name: Lint with flake8
run: |
flake8 --max-line-length=120 --statistics --show-source .
flake8 --statistics --show-source .
- name: Lint with black
run: |
Expand Down
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: check-docstring-first
- id: debug-statements
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: check-merge-conflict
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile, black]
- repo: meta
hooks:
- id: check-useless-excludes
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
all: clean package

.PHONY: test
test:
@echo ----- Running python tests -----
python3 -m unittest discover

.PHONY: test_coverage
test_coverage:
@echo ----- Testing code coverage -----
coverage run --source=kadet -m unittest discover
coverage report --fail-under=65 -m

.PHONY: test_formatting
test_formatting:
@echo ----- Testing code formatting -----
black --check .
@echo

.PHONY: format_codestyle
format_codestyle:
which black || echo "Install black with pip3 install --user black"
# ignores line length and reclass
black .
@echo
24 changes: 11 additions & 13 deletions kadet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import hashlib
import json
from typing import ClassVar
from typing import Annotated

import yaml
from box import Box, BoxList
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Extra
from pydantic import Field
from typeguard import check_type

ABORT_EXCEPTION_TYPE = ValueError
Expand Down Expand Up @@ -110,7 +110,7 @@ def need(self, key, msg="key and value needed", istype=None):
if key not in self.kwargs:
raise ABORT_EXCEPTION_TYPE(err_msg) # XXX in Kapitan this is CompileError
elif istype is not None:
check_type(key, self.kwargs[key], istype)
check_type(self.kwargs[key], istype)

def optional(self, key, default=None, istype=None):
"""Set self.kwargs key as optional.
Expand All @@ -119,13 +119,13 @@ def optional(self, key, default=None, istype=None):
match type passed in istype.
"""
if key in self.kwargs and istype is not None:
check_type(key, self.kwargs[key], istype)
check_type(self.kwargs[key], istype)

if key not in self.kwargs:
if default is None:
self.kwargs[key] = default
elif istype is not None:
check_type(key, default, istype)
check_type(default, istype)
self.kwargs[key] = default

def new(self):
Expand Down Expand Up @@ -195,11 +195,15 @@ def sha256(self):


class BaseModel(PydanticBaseModel):
root: ClassVar # hide root from repr
root: Annotated[Dict, Field(repr=False)] = Dict()
model_config: Dict = {
# https://docs.pydantic.dev/latest/migration/#changes-to-config
"arbitrary_types_allowed": True,
"extra": "allow",
}

def __init__(self, **data):
super().__init__(**data)
self.root = Dict() # initialise empty root

if hasattr(self, "new"):
assert callable(self.new)
Expand Down Expand Up @@ -253,9 +257,3 @@ def _dump(self, obj):
def dump(self):
"""Return object dict/list."""
return self._dump(self)

class Config:
arbitrary_types_allowed = True # allow all types e.g. BaseObj
copy_on_model_validation = False # performance?
underscore_attrs_are_private = True
extra = Extra.allow
Loading

0 comments on commit 40c6e94

Please sign in to comment.