Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DeprecationWarning with patched python-json-logger #103

Merged
merged 13 commits into from
Dec 17, 2024
10 changes: 6 additions & 4 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.8", "3.12"]
python-version: ["3.9", "3.12"]
include:
- os: windows-latest
python-version: "3.9"
- os: ubuntu-latest
python-version: "pypy-3.8"
python-version: "pypy-3.9"
- os: ubuntu-latest
python-version: "3.10"
- os: macos-latest
Expand All @@ -44,10 +44,12 @@ jobs:
- uses: jupyterlab/maintainer-tools/.github/actions/report-coverage@v1

docs:
runs-on: windows-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
with:
python_version: 3.12
- run: hatch run docs:build

test_lint:
Expand Down Expand Up @@ -82,7 +84,7 @@ jobs:
- name: Base Setup
uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
with:
dependency_type: minimum
python_version: 3.9
- name: Install with minimum versions and optional deps
run: |
pip install -e .[test]
Expand Down
12 changes: 10 additions & 2 deletions jupyter_events/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import typing as t
import warnings
from datetime import datetime, timezone
from importlib.metadata import version

from jsonschema import ValidationError
from pythonjsonlogger import jsonlogger
from packaging.version import parse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a novel (though common) dependency: we'll probably ship it on conda-forge with it added, but will PR up as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. Thanks for catching that.

from traitlets import Dict, Instance, Set, default
from traitlets.config import Config, LoggingConfigurable

Expand All @@ -21,6 +22,13 @@
from .traits import Handlers
from .validators import JUPYTER_EVENTS_CORE_VALIDATOR

# Check if the version is greater than 3.1.0
version_info = version("python-json-logger")
if parse(version_info) >= parse("3.1.0"):
from pythonjsonlogger.json import JsonFormatter
else:
from pythonjsonlogger.jsonlogger import JsonFormatter # type: ignore[attr-defined]

# Increment this version when the metadata included with each event
# changes.
EVENTS_METADATA_VERSION = 1
Expand Down Expand Up @@ -171,7 +179,7 @@ def _handle_message_field(record: t.Any, **kwargs: t.Any) -> str:
del record["message"]
return json.dumps(record, **kwargs)

formatter = jsonlogger.JsonFormatter( # type:ignore [no-untyped-call]
formatter = JsonFormatter(
json_serializer=_handle_message_field,
)
handler.setFormatter(formatter)
Expand Down
4 changes: 2 additions & 2 deletions jupyter_events/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import json
from pathlib import Path, PurePath
from typing import Any, Dict, Union
from typing import Any, Union

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


SchemaType = Union[Dict[str, Any], str, PurePath]
SchemaType = Union[dict[str, Any], str, PurePath]


class EventSchema:
Expand Down
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "hatchling.build"
name = "jupyter-events"
description = "Jupyter Event System library"
readme = "README.md"
requires-python = ">=3.8"
requires-python = ">=3.9"
authors = [
{ name = "Jupyter Development Team", email = "jupyter@googlegroups.com" },
]
Expand Down Expand Up @@ -52,9 +52,10 @@ jupyter-events = "jupyter_events.cli:main"

[project.optional-dependencies]
docs = [
"sphinx>=8",
"jupyterlite-sphinx",
"myst_parser",
"pydata_sphinx_theme",
"pydata_sphinx_theme>=0.16",
"sphinxcontrib-spelling",
]
test = [
Expand Down Expand Up @@ -150,7 +151,7 @@ source = ["jupyter_events"]

[tool.mypy]
files = "jupyter_events"
python_version = "3.8"
python_version = "3.9"
strict = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
warn_unreachable = true
Expand Down Expand Up @@ -181,6 +182,7 @@ extend-select = [
"EXE", # flake8-executable
"PYI", # flake8-pyi
"S", # flake8-bandit
"G001", # .format and co in logging methods
]
ignore = [
"E501", # E501 Line too long (158 > 100 characters)
Expand Down
11 changes: 0 additions & 11 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io
import json
import logging
import sys
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock

Expand Down Expand Up @@ -166,8 +165,6 @@ def test_emit():
"__metadata_version__": 1,
"something": "blah",
}
if sys.version_info >= (3, 12):
expected["taskName"] = None
assert event_capsule == expected


Expand Down Expand Up @@ -214,8 +211,6 @@ def test_message_field():
"something": "blah",
"message": "a message was seen",
}
if sys.version_info >= (3, 12):
expected["taskName"] = None
assert event_capsule == expected


Expand Down Expand Up @@ -263,8 +258,6 @@ def test_nested_message_field():
"__metadata_version__": 1,
"thing": {"message": "a nested message was seen"},
}
if sys.version_info >= (3, 12):
expected["taskName"] = None
assert event_capsule == expected


Expand Down Expand Up @@ -428,8 +421,6 @@ def test_unique_logger_instances():
"__metadata_version__": 1,
"something": "blah",
}
if sys.version_info >= (3, 12):
expected["taskName"] = None
assert event_capsule0 == expected

event_capsule1 = json.loads(output1.getvalue())
Expand All @@ -443,8 +434,6 @@ def test_unique_logger_instances():
"__metadata_version__": 1,
"something": "blah",
}
if sys.version_info >= (3, 12):
expected["taskName"] = None
assert event_capsule1 == expected


Expand Down
Loading