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 import pthonjsonlogger.jsonlogger #33

Merged
merged 10 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.swp
build
dist
dist_uploaded
*.egg-info

# Tests and validation
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.2.1](https://github.com/nhairs/python-json-logger/compare/v3.2.0...v3.2.1) - 2024-12-16

### Fixed
- Import error on `import pythonjsonlogger.jsonlogger` [#29](https://github.com/nhairs/python-json-logger/issues/29)


## [3.2.0](https://github.com/nhairs/python-json-logger/compare/v3.1.0...v3.2.0) - 2024-12-11

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "python-json-logger"
version = "3.2.0"
version = "3.2.1"
description = "JSON Log Formatter for the Python Logging Package"
authors = [
{name = "Zakaria Zajac", email = "[email protected]"},
Expand Down
20 changes: 4 additions & 16 deletions src/pythonjsonlogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@
## Installed

## Application
import pythonjsonlogger.json
import pythonjsonlogger.utils
from . import json
from . import utils

### CONSTANTS
### ============================================================================
ORJSON_AVAILABLE = pythonjsonlogger.utils.package_is_available("orjson")
MSGSPEC_AVAILABLE = pythonjsonlogger.utils.package_is_available("msgspec")


### DEPRECATED COMPATIBILITY
### ============================================================================
def __getattr__(name: str):
if name == "jsonlogger":
warnings.warn(
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json",
DeprecationWarning,
)
return pythonjsonlogger.json
raise AttributeError(f"module {__name__} has no attribute {name}")
ORJSON_AVAILABLE = utils.package_is_available("orjson")
MSGSPEC_AVAILABLE = utils.package_is_available("msgspec")
18 changes: 18 additions & 0 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Stub module retained for compatibility.

It retains access to old names whilst sending deprecation warnings.
"""

# pylint: disable=wrong-import-position,unused-import

import warnings

## Throw warning
warnings.warn(
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json",
DeprecationWarning,
)

## Import names
from .json import JsonFormatter, JsonEncoder
from .core import RESERVED_ATTRS
19 changes: 18 additions & 1 deletion tests/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from __future__ import annotations

## Standard Library
import subprocess
import sys

## Installed
import pytest
Expand All @@ -16,7 +18,7 @@
### ============================================================================
def test_jsonlogger_deprecated():
with pytest.deprecated_call():
pythonjsonlogger.jsonlogger
import pythonjsonlogger.jsonlogger
return


Expand All @@ -26,3 +28,18 @@ def test_jsonlogger_reserved_attrs_deprecated():
# a DeprecationWarning and we specifically want the one for RESERVED_ATTRS
pythonjsonlogger.json.RESERVED_ATTRS
return


@pytest.mark.parametrize(
"command",
[
"from pythonjsonlogger import jsonlogger",
"import pythonjsonlogger.jsonlogger",
"from pythonjsonlogger.jsonlogger import JsonFormatter",
"from pythonjsonlogger.jsonlogger import RESERVED_ATTRS",
],
)
def test_import(command: str):
output = subprocess.check_output([sys.executable, "-c", f"{command};print('OK')"])
assert output.strip() == b"OK"
return
Loading