Skip to content

Commit e7761e5

Browse files
authored
Fix import pthonjsonlogger.jsonlogger (#33)
Fixes: #29 Although efforts were made for backwards compatibility using `__getattr__` in `pythonjsonlogger/__init__.py`, this only worked if you were doing `from pythonjsonlogger import jsonlogger`. When importing by the full name `import pythonjsonlogger.jsonlogger` it it would fail to locate the module file and thus not be able to produce a module spec which then of course causes the import to fail. We get around this by actually having a module that imports the names it needs from the new locations. ### Test Plan - Run unit tests
1 parent 36f160e commit e7761e5

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.swp
33
build
44
dist
5+
dist_uploaded
56
*.egg-info
67

78
# Tests and validation

docs/changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [3.2.1](https://github.com/nhairs/python-json-logger/compare/v3.2.0...v3.2.1) - 2024-12-16
8+
9+
### Fixed
10+
- Import error on `import pythonjsonlogger.jsonlogger` [#29](https://github.com/nhairs/python-json-logger/issues/29)
11+
12+
713
## [3.2.0](https://github.com/nhairs/python-json-logger/compare/v3.1.0...v3.2.0) - 2024-12-11
814

915
### Changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "python-json-logger"
7-
version = "3.2.0"
7+
version = "3.2.1"
88
description = "JSON Log Formatter for the Python Logging Package"
99
authors = [
1010
{name = "Zakaria Zajac", email = "[email protected]"},

src/pythonjsonlogger/__init__.py

+4-16
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,10 @@
88
## Installed
99

1010
## Application
11-
import pythonjsonlogger.json
12-
import pythonjsonlogger.utils
11+
from . import json
12+
from . import utils
1313

1414
### CONSTANTS
1515
### ============================================================================
16-
ORJSON_AVAILABLE = pythonjsonlogger.utils.package_is_available("orjson")
17-
MSGSPEC_AVAILABLE = pythonjsonlogger.utils.package_is_available("msgspec")
18-
19-
20-
### DEPRECATED COMPATIBILITY
21-
### ============================================================================
22-
def __getattr__(name: str):
23-
if name == "jsonlogger":
24-
warnings.warn(
25-
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json",
26-
DeprecationWarning,
27-
)
28-
return pythonjsonlogger.json
29-
raise AttributeError(f"module {__name__} has no attribute {name}")
16+
ORJSON_AVAILABLE = utils.package_is_available("orjson")
17+
MSGSPEC_AVAILABLE = utils.package_is_available("msgspec")

src/pythonjsonlogger/jsonlogger.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Stub module retained for compatibility.
2+
3+
It retains access to old names whilst sending deprecation warnings.
4+
"""
5+
6+
# pylint: disable=wrong-import-position,unused-import
7+
8+
import warnings
9+
10+
## Throw warning
11+
warnings.warn(
12+
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json",
13+
DeprecationWarning,
14+
)
15+
16+
## Import names
17+
from .json import JsonFormatter, JsonEncoder
18+
from .core import RESERVED_ATTRS

tests/test_deprecation.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from __future__ import annotations
55

66
## Standard Library
7+
import subprocess
8+
import sys
79

810
## Installed
911
import pytest
@@ -16,7 +18,7 @@
1618
### ============================================================================
1719
def test_jsonlogger_deprecated():
1820
with pytest.deprecated_call():
19-
pythonjsonlogger.jsonlogger
21+
import pythonjsonlogger.jsonlogger
2022
return
2123

2224

@@ -26,3 +28,18 @@ def test_jsonlogger_reserved_attrs_deprecated():
2628
# a DeprecationWarning and we specifically want the one for RESERVED_ATTRS
2729
pythonjsonlogger.json.RESERVED_ATTRS
2830
return
31+
32+
33+
@pytest.mark.parametrize(
34+
"command",
35+
[
36+
"from pythonjsonlogger import jsonlogger",
37+
"import pythonjsonlogger.jsonlogger",
38+
"from pythonjsonlogger.jsonlogger import JsonFormatter",
39+
"from pythonjsonlogger.jsonlogger import RESERVED_ATTRS",
40+
],
41+
)
42+
def test_import(command: str):
43+
output = subprocess.check_output([sys.executable, "-c", f"{command};print('OK')"])
44+
assert output.strip() == b"OK"
45+
return

0 commit comments

Comments
 (0)