diff --git a/.gitignore b/.gitignore index 8e3c556..97ad525 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.swp build dist +dist_uploaded *.egg-info # Tests and validation diff --git a/docs/changelog.md b/docs/changelog.md index 2902298..82d1cc7 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index c534d5f..08adbde 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "zak@madzak.com"}, diff --git a/src/pythonjsonlogger/__init__.py b/src/pythonjsonlogger/__init__.py index 2eee544..298a3fe 100644 --- a/src/pythonjsonlogger/__init__.py +++ b/src/pythonjsonlogger/__init__.py @@ -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") diff --git a/src/pythonjsonlogger/jsonlogger.py b/src/pythonjsonlogger/jsonlogger.py new file mode 100644 index 0000000..0b283b2 --- /dev/null +++ b/src/pythonjsonlogger/jsonlogger.py @@ -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 diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index ad4c988..a784aec 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -4,6 +4,8 @@ from __future__ import annotations ## Standard Library +import subprocess +import sys ## Installed import pytest @@ -16,7 +18,7 @@ ### ============================================================================ def test_jsonlogger_deprecated(): with pytest.deprecated_call(): - pythonjsonlogger.jsonlogger + import pythonjsonlogger.jsonlogger return @@ -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