Skip to content

Commit 93d37c6

Browse files
cjwatsonpre-commit-ci[bot]Carreau
authored
Fix DeprecationWarning with patched python-json-logger (#103)
* Fix DeprecationWarning with patched python-json-logger Running tests with https://github.com/nhairs/python-json-logger 3.1.0 produces: DeprecationWarning: pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json It's easy enough to be compatible with both. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * be explict about versions for later cleanup * fix * remove weird taskName * pre-commit * min version 3.9 * min py version * fix lints * bump versions * min pyversion 3.9 * min pyversion 3.9 * dont build docs on window --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: M Bussonnier <[email protected]>
1 parent bf1dc11 commit 93d37c6

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

.github/workflows/python-tests.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818
fail-fast: false
1919
matrix:
2020
os: [ubuntu-latest, windows-latest, macos-latest]
21-
python-version: ["3.8", "3.12"]
21+
python-version: ["3.9", "3.12"]
2222
include:
2323
- os: windows-latest
2424
python-version: "3.9"
2525
- os: ubuntu-latest
26-
python-version: "pypy-3.8"
26+
python-version: "pypy-3.9"
2727
- os: ubuntu-latest
2828
python-version: "3.10"
2929
- os: macos-latest
@@ -44,10 +44,12 @@ jobs:
4444
- uses: jupyterlab/maintainer-tools/.github/actions/report-coverage@v1
4545

4646
docs:
47-
runs-on: windows-latest
47+
runs-on: ubuntu-latest
4848
steps:
4949
- uses: actions/checkout@v4
5050
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
51+
with:
52+
python_version: 3.12
5153
- run: hatch run docs:build
5254

5355
test_lint:
@@ -82,7 +84,7 @@ jobs:
8284
- name: Base Setup
8385
uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
8486
with:
85-
dependency_type: minimum
87+
python_version: 3.9
8688
- name: Install with minimum versions and optional deps
8789
run: |
8890
pip install -e .[test]

jupyter_events/logger.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import typing as t
1111
import warnings
1212
from datetime import datetime, timezone
13+
from importlib.metadata import version
1314

1415
from jsonschema import ValidationError
15-
from pythonjsonlogger import jsonlogger
16+
from packaging.version import parse
1617
from traitlets import Dict, Instance, Set, default
1718
from traitlets.config import Config, LoggingConfigurable
1819

@@ -21,6 +22,13 @@
2122
from .traits import Handlers
2223
from .validators import JUPYTER_EVENTS_CORE_VALIDATOR
2324

25+
# Check if the version is greater than 3.1.0
26+
version_info = version("python-json-logger")
27+
if parse(version_info) >= parse("3.1.0"):
28+
from pythonjsonlogger.json import JsonFormatter
29+
else:
30+
from pythonjsonlogger.jsonlogger import JsonFormatter # type: ignore[attr-defined]
31+
2432
# Increment this version when the metadata included with each event
2533
# changes.
2634
EVENTS_METADATA_VERSION = 1
@@ -171,7 +179,7 @@ def _handle_message_field(record: t.Any, **kwargs: t.Any) -> str:
171179
del record["message"]
172180
return json.dumps(record, **kwargs)
173181

174-
formatter = jsonlogger.JsonFormatter( # type:ignore [no-untyped-call]
182+
formatter = JsonFormatter(
175183
json_serializer=_handle_message_field,
176184
)
177185
handler.setFormatter(formatter)

jupyter_events/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import json
55
from pathlib import Path, PurePath
6-
from typing import Any, Dict, Union
6+
from typing import Any, Union
77

88
from jsonschema import FormatChecker, validators
99
from referencing import Registry
@@ -30,7 +30,7 @@ class EventSchemaFileAbsent(Exception):
3030
"""An error for an absent event schema file."""
3131

3232

33-
SchemaType = Union[Dict[str, Any], str, PurePath]
33+
SchemaType = Union[dict[str, Any], str, PurePath]
3434

3535

3636
class EventSchema:

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66
name = "jupyter-events"
77
description = "Jupyter Event System library"
88
readme = "README.md"
9-
requires-python = ">=3.8"
9+
requires-python = ">=3.9"
1010
authors = [
1111
{ name = "Jupyter Development Team", email = "[email protected]" },
1212
]
@@ -52,9 +52,10 @@ jupyter-events = "jupyter_events.cli:main"
5252

5353
[project.optional-dependencies]
5454
docs = [
55+
"sphinx>=8",
5556
"jupyterlite-sphinx",
5657
"myst_parser",
57-
"pydata_sphinx_theme",
58+
"pydata_sphinx_theme>=0.16",
5859
"sphinxcontrib-spelling",
5960
]
6061
test = [
@@ -150,7 +151,7 @@ source = ["jupyter_events"]
150151

151152
[tool.mypy]
152153
files = "jupyter_events"
153-
python_version = "3.8"
154+
python_version = "3.9"
154155
strict = true
155156
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
156157
warn_unreachable = true
@@ -181,6 +182,7 @@ extend-select = [
181182
"EXE", # flake8-executable
182183
"PYI", # flake8-pyi
183184
"S", # flake8-bandit
185+
"G001", # .format and co in logging methods
184186
]
185187
ignore = [
186188
"E501", # E501 Line too long (158 > 100 characters)

tests/test_logger.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io
44
import json
55
import logging
6-
import sys
76
from datetime import datetime, timedelta, timezone
87
from unittest.mock import MagicMock
98

@@ -166,8 +165,6 @@ def test_emit():
166165
"__metadata_version__": 1,
167166
"something": "blah",
168167
}
169-
if sys.version_info >= (3, 12):
170-
expected["taskName"] = None
171168
assert event_capsule == expected
172169

173170

@@ -214,8 +211,6 @@ def test_message_field():
214211
"something": "blah",
215212
"message": "a message was seen",
216213
}
217-
if sys.version_info >= (3, 12):
218-
expected["taskName"] = None
219214
assert event_capsule == expected
220215

221216

@@ -263,8 +258,6 @@ def test_nested_message_field():
263258
"__metadata_version__": 1,
264259
"thing": {"message": "a nested message was seen"},
265260
}
266-
if sys.version_info >= (3, 12):
267-
expected["taskName"] = None
268261
assert event_capsule == expected
269262

270263

@@ -428,8 +421,6 @@ def test_unique_logger_instances():
428421
"__metadata_version__": 1,
429422
"something": "blah",
430423
}
431-
if sys.version_info >= (3, 12):
432-
expected["taskName"] = None
433424
assert event_capsule0 == expected
434425

435426
event_capsule1 = json.loads(output1.getvalue())
@@ -443,8 +434,6 @@ def test_unique_logger_instances():
443434
"__metadata_version__": 1,
444435
"something": "blah",
445436
}
446-
if sys.version_info >= (3, 12):
447-
expected["taskName"] = None
448437
assert event_capsule1 == expected
449438

450439

0 commit comments

Comments
 (0)