Skip to content

Commit bd32054

Browse files
committed
fix: remove unnecessary dependency on Pydantic
1 parent 25a0079 commit bd32054

File tree

6 files changed

+54
-36
lines changed

6 files changed

+54
-36
lines changed

.pylintrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ enable =
5656
file-ignored,
5757
use-symbolic-message-instead,
5858
useless-suppression
59-
# Fixes 'no-name-in-module'
60-
extension-pkg-whitelist=pydantic
6159
# Defer to other tools and reduce false positives
6260
disable=
6361
broad-except,

pyproject.toml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ enable_error_code = ["ignore-without-code", "possibly-undefined", "redundant-exp
1313
extra_checks = true
1414
files = ["tail_jsonl", "tests"]
1515
no_implicit_reexport = true
16-
plugins = [
17-
"pydantic.mypy", # Most settings are from: https://pydantic-docs.helpmanual.io/mypy_plugin
18-
]
1916
python_version = "3.9"
2017
show_column_numbers = true
2118
show_error_codes = true
@@ -65,11 +62,6 @@ tail-jsonl = "tail_jsonl.scripts:start"
6562
"Bug Tracker" = "https://github.com/kyleking/tail-jsonl/issues"
6663
"Changelog" = "https://github.com/kyleking/tail-jsonl/blob/main/docs/docs/CHANGELOG.md"
6764

68-
[tool.pydantic-mypy]
69-
init_forbid_extra = true
70-
init_typed = true
71-
warn_required_dynamic_aliases = true
72-
7365
[tool.pyright]
7466
include = ["tail_jsonl", "tests"]
7567
pythonVersion = "3.9"

tail_jsonl/_private/core.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
"""Core print logic."""
22

3+
from __future__ import annotations
4+
35
import json
46
import logging
57
from copy import copy
8+
from dataclasses import dataclass
9+
from typing import Any
610

711
import dotted # type: ignore[import-untyped]
812
from beartype import beartype
9-
from beartype.typing import Any, Dict, List, Optional
1013
from corallium.loggers.rich_printer import rich_printer
1114
from corallium.loggers.styles import get_level
12-
from pydantic import BaseModel
1315
from rich.console import Console
1416

1517
from tail_jsonl.config import Config
1618

1719

18-
@beartype
19-
def _dot_pop(data: Dict, key: str) -> Optional[str]: # type: ignore[type-arg]
20+
21+
def _dot_pop(data: dict, key: str) -> str | None: # type: ignore[type-arg]
2022
value = dotted.get(data, key)
2123
if isinstance(value, str):
2224
dotted.remove(data, key)
2325
return value or None
2426
return None
2527

2628

27-
@beartype
28-
def _pop_key(data: Dict, keys: List[str], fallback: str) -> Any: # type: ignore[type-arg]
29+
30+
def _pop_key(data: dict, keys: list[str], fallback: str) -> Any: # type: ignore[type-arg]
2931
"""Return result of recursively popping each key while searching for a match."""
3032
try:
3133
key = keys.pop(0)
@@ -34,23 +36,24 @@ def _pop_key(data: Dict, keys: List[str], fallback: str) -> Any: # type: ignore
3436
return _dot_pop(data, key) or _pop_key(data, keys, fallback)
3537

3638

37-
@beartype
38-
def pop_key(data: Dict, keys: List[str], fallback: str) -> Any: # type: ignore[type-arg]
39+
40+
def pop_key(data: dict, keys: list[str], fallback: str) -> Any: # type: ignore[type-arg]
3941
"""Return the first key in the data or default to the fallback."""
4042
return _pop_key(data, copy(keys), fallback)
4143

4244

43-
class Record(BaseModel):
45+
@dataclass
46+
class Record:
4447
"""Record Model."""
4548

4649
timestamp: str
4750
level: str
4851
message: str
49-
data: Dict # type: ignore[type-arg]
52+
data: dict # type: ignore[type-arg]
5053

5154
@classmethod
52-
@beartype
53-
def from_line(cls, data: Dict, config: Config) -> 'Record': # type: ignore[type-arg]
55+
56+
def from_line(cls, data: dict, config: Config) -> Record: # type: ignore[type-arg]
5457
"""Return Record from jsonl."""
5558
return cls(
5659
timestamp=pop_key(data, config.keys.timestamp, '<no timestamp>'),
@@ -60,7 +63,7 @@ def from_line(cls, data: Dict, config: Config) -> 'Record': # type: ignore[type
6063
)
6164

6265

63-
@beartype
66+
6467
def print_record(line: str, console: Console, config: Config) -> None:
6568
"""Format and print the record."""
6669
try:

tail_jsonl/config.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,47 @@
11
"""Configuration."""
22

3+
from __future__ import annotations
34

4-
from beartype.typing import List
5-
from corallium.loggers.styles import Styles
6-
from pydantic import BaseModel, Field
5+
from dataclasses import dataclass, field
76

7+
from corallium.loggers.styles import Colors, Styles
88

9-
class Keys(BaseModel):
9+
10+
# PLANNED: temporary backward compatibility until part of Corallium
11+
def styles_from_dict(data: dict) -> Styles:
12+
"""Return Self instance."""
13+
if colors := (data.pop('colors', None) or None):
14+
colors = Colors(**colors)
15+
return Styles(**data, colors=colors)
16+
17+
18+
@dataclass
19+
class Keys:
1020
"""Special Keys."""
1121

12-
timestamp: List[str] = Field(default_factory=lambda: ['timestamp', 'time', 'record.time.repr'])
13-
level: List[str] = Field(default_factory=lambda: ['level', 'levelname', 'record.level.name'])
14-
message: List[str] = Field(default_factory=lambda: ['event', 'message', 'record.message'])
22+
timestamp: list[str] = field(default_factory=lambda: ['timestamp', 'time', 'record.time.repr'])
23+
level: list[str] = field(default_factory=lambda: ['level', 'levelname', 'record.level.name'])
24+
message: list[str] = field(default_factory=lambda: ['event', 'message', 'record.message'])
1525

16-
on_own_line: List[str] = Field(default_factory=lambda: ['text', 'exception'])
26+
on_own_line: list[str] = field(default_factory=lambda: ['text', 'exception'])
1727

28+
@classmethod
29+
def from_dict(cls, data: dict) -> Keys:
30+
"""Return Self instance."""
31+
return cls(**data)
1832

19-
class Config(BaseModel):
33+
34+
@dataclass
35+
class Config:
2036
"""`tail-jsonl` config."""
2137

22-
styles: Styles = Field(default_factory=Styles)
23-
keys: Keys = Field(default_factory=Keys)
38+
styles: Styles = field(default_factory=Styles)
39+
keys: Keys = field(default_factory=Keys)
40+
41+
@classmethod
42+
def from_dict(cls, data: dict) -> Config:
43+
"""Return Self instance."""
44+
return cls(
45+
styles=styles_from_dict(data.get('styles', {})),
46+
keys=Keys.from_dict(data.get('keys', {})),
47+
)

tail_jsonl/scripts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _load_config(config_path: Optional[str]) -> Config:
2222
if config_path:
2323
pth = Path(config_path).expanduser()
2424
user_config = tomllib.loads(pth.read_text(encoding='utf-8'))
25-
return Config(**user_config)
25+
return Config.from_dict(user_config)
2626

2727

2828
@beartype

tests/test_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from dataclasses import asdict
12
from pathlib import Path
23

34
from corallium.tomllib import tomllib
@@ -11,4 +12,4 @@ def test_create_default_config():
1112

1213
config = _load_config(config_path=str(example_config))
1314

14-
assert tomllib.loads(example_config.read_text(encoding='utf-8')) == config.model_dump()
15+
assert tomllib.loads(example_config.read_text(encoding='utf-8')) == asdict(config)

0 commit comments

Comments
 (0)