Skip to content

Commit

Permalink
Fix import pthonjsonlogger.jsonlogger (#33)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nhairs authored Dec 16, 2024
1 parent 36f160e commit e7761e5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
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

0 comments on commit e7761e5

Please sign in to comment.