Skip to content

Commit

Permalink
Merge pull request #60 from jacebrowning/format-dataclasses
Browse files Browse the repository at this point in the history
Format dataclasses automatically
  • Loading branch information
jacebrowning committed Dec 9, 2023
2 parents d31b6db + 7b5156e commit b58ea6e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -5,6 +5,7 @@ python:
- "3.8"
- "3.9"
- "3.10"
- "3.11"

cache:
pip: true
Expand Down
98 changes: 85 additions & 13 deletions .vscode/settings.json
@@ -1,15 +1,87 @@
{
"cSpell.words": [
"caplog",
"coveragerc",
"freezegun",
"minilog",
"minimalistic",
"mkdocs",
"mypy",
"sinfo",
"USERPROFILE",
"venv",
"verchew"
]
"files.exclude": {
".cache/": true,
".venv/": true,
"*.egg-info": true,
"pip-wheel-metadata/": true,
"**/__pycache__": true,
"**/*.pyc": true,
"**/.ipynb_checkpoints": true,
"**/tmp/": true,
"dist/": true,
"htmlcov/": true,
"prof/": true,
"site/": true,
"geckodriver.log": true
},
"python.defaultInterpreterPath": ".venv/bin/python",
"editor.formatOnSave": true,
"pylint.args": ["--rcfile=.pylint.ini"],
"cSpell.words": [
"appex",
"autorefs",
"builtins",
"classproperties",
"codehilite",
"completly",
"cookiecutter",
"coveragerc",
"cygstart",
"cygwin",
"dataclass",
"dataclasses",
"datafile",
"ensurepip",
"findstr",
"fontawesome",
"freezegun",
"gethostname",
"getpid",
"getplugin",
"gitman",
"gitsvn",
"Graphviz",
"iglob",
"imac",
"importlib",
"ioreg",
"iphoto",
"ipython",
"levelname",
"logbreak",
"macbook",
"MDEF",
"mkdocs",
"mkdocstrings",
"mrpossoms",
"mylink",
"mypy",
"noclasses",
"nohup",
"pipx",
"pluginmanager",
"preserialization",
"Preserialized",
"Preserializing",
"psutil",
"repr",
"ruamel",
"rustup",
"scalarstring",
"showfspath",
"startfile",
"tomlkit",
"tput",
"tracebackhide",
"Trilean",
"trufflehog",
"udevadm",
"unparseable",
"USERPROFILE",
"venv",
"verchew",
"verchewrc",
"webfonts",
"YORM"
]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Release Notes

## 2.3 (2023-12-09)

- Added automatic formatting for dataclasses.

## 2.2 (2023-06-29)

- Dropped support for Python 3.7.
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
@@ -1,3 +1,3 @@
mkdocs==1.2.3 ; python_version >= "3.8" and python_version < "4.0"
pygments==2.9.0 ; python_version >= "3.8" and python_version < "4.0"
pygments==2.15.0 ; python_version >= "3.8" and python_version < "4.0"
jinja2==3.0.1 ; python_version >= "3.8" and python_version < "4.0"
13 changes: 12 additions & 1 deletion log/tests/test_utils.py
@@ -1,6 +1,7 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,singleton-comparison,disallowed-name

import logging
from dataclasses import dataclass

from log.utils import create_logger_record, format_message, parse_name

Expand Down Expand Up @@ -36,5 +37,15 @@ def it_formats_structures(expect):
data = {x: x * 10 for x in range(20)}
expect(format_message(data).count("\n")) == 19

def it_formats_dataclasses(expect):
@dataclass
class Example:
foo: int = 1
bar: str = "abc"

example = Example()

expect(format_message(example)) == "{'bar': 'abc', 'foo': 1}"

def it_preserves_strings(expect):
expect(format_message("foobar")) == "foobar"
3 changes: 3 additions & 0 deletions log/utils.py
@@ -1,5 +1,6 @@
"""Implements the "magic" to create `logging` records for the caller."""

import dataclasses
import inspect
import logging
from pprint import pformat
Expand Down Expand Up @@ -63,6 +64,8 @@ def parse_filename(frame_info: Dict) -> str:


def format_message(value) -> str:
if dataclasses.is_dataclass(value):
value = dataclasses.asdict(value)
if not isinstance(value, str):
value = pformat(value)
return value
3 changes: 2 additions & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "minilog"
version = "2.2"
version = "2.3"
description = "Minimalistic wrapper for Python logging."

license = "MIT"
Expand All @@ -26,6 +26,7 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python",
"Topic :: Software Development",
"Topic :: System :: Logging",
Expand Down

0 comments on commit b58ea6e

Please sign in to comment.