From 25b1baac9748dd32969a47c59c62b0aeaf30a94f Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Fri, 13 Dec 2024 13:19:49 +1100 Subject: [PATCH 1/9] Fix import pthonjsonlogger.jsonlogger 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. --- docs/changelog.md | 6 ++++++ pyproject.toml | 2 +- src/pythonjsonlogger/__init__.py | 20 ++++---------------- src/pythonjsonlogger/jsonlogger.py | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 src/pythonjsonlogger/jsonlogger.py diff --git a/docs/changelog.md b/docs/changelog.md index 2902298..fbb75b6 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) - UNRELEASED + +### 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..0bbeb81 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.rc1" 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..66bc90f --- /dev/null +++ b/src/pythonjsonlogger/jsonlogger.py @@ -0,0 +1,14 @@ +"""Stub module retained for compatibility. + +It retains access to old names whilst sending deprecation 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 From 5e77c0d843baa553e8326f5e28d4b556fcadf388 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Fri, 13 Dec 2024 13:29:32 +1100 Subject: [PATCH 2/9] import warnings --- src/pythonjsonlogger/jsonlogger.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pythonjsonlogger/jsonlogger.py b/src/pythonjsonlogger/jsonlogger.py index 66bc90f..bd8ae95 100644 --- a/src/pythonjsonlogger/jsonlogger.py +++ b/src/pythonjsonlogger/jsonlogger.py @@ -3,6 +3,8 @@ It retains access to old names whilst sending deprecation warnings. """ +import warnings + ## Throw warning warnings.warn( "pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json", From 9681ac0cc2dfe2fb07d6e32c972c79ef2dc05d81 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Fri, 13 Dec 2024 13:32:04 +1100 Subject: [PATCH 3/9] Fix lint errors, fix tests --- src/pythonjsonlogger/jsonlogger.py | 2 ++ tests/test_deprecation.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pythonjsonlogger/jsonlogger.py b/src/pythonjsonlogger/jsonlogger.py index bd8ae95..0b283b2 100644 --- a/src/pythonjsonlogger/jsonlogger.py +++ b/src/pythonjsonlogger/jsonlogger.py @@ -3,6 +3,8 @@ It retains access to old names whilst sending deprecation warnings. """ +# pylint: disable=wrong-import-position,unused-import + import warnings ## Throw warning diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index ad4c988..24e1939 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -16,7 +16,7 @@ ### ============================================================================ def test_jsonlogger_deprecated(): with pytest.deprecated_call(): - pythonjsonlogger.jsonlogger + import pythonjsonlogger.jsonlogger return From b4e4247e9eb077c69b77278bc1a45f0c765d3ead Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Fri, 13 Dec 2024 13:39:16 +1100 Subject: [PATCH 4/9] Use dev instead of rc release --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0bbeb81..b4fbb8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "python-json-logger" -version = "3.2.1.rc1" +version = "3.2.1.dev1" description = "JSON Log Formatter for the Python Logging Package" authors = [ {name = "Zakaria Zajac", email = "zak@madzak.com"}, From 99876a673745f236133a2e745c4508538a023b2d Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Fri, 13 Dec 2024 13:43:27 +1100 Subject: [PATCH 5/9] Update git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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 From 9c3499d1914273bc7db4d26fd85e3e6786d49de1 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Mon, 16 Dec 2024 17:16:40 +1100 Subject: [PATCH 6/9] Add import tests --- tests/test_deprecation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index 24e1939..3326107 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -4,6 +4,7 @@ from __future__ import annotations ## Standard Library +import subprocess ## Installed import pytest @@ -26,3 +27,17 @@ 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", + [ + "import pythonjsonlogger.jsonlogger", + "from pythonjsonlogger.jsonlogger import JsonFormatter", + "from pythonjsonlogger.jsonlogger import RESERVED_ATTRS", + ], +) +def test_import(command: str): + output = subprocess.check_output(["python", "-c", f"{command};print('OK')"]) + assert output.strip() == b"OK" + return From c0feec549d352462b750155df2b8b41991a8f224 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Mon, 16 Dec 2024 17:21:36 +1100 Subject: [PATCH 7/9] switch to sys.executable --- tests/test_deprecation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index 3326107..07b0204 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -5,6 +5,7 @@ ## Standard Library import subprocess +import sys ## Installed import pytest @@ -38,6 +39,6 @@ def test_jsonlogger_reserved_attrs_deprecated(): ], ) def test_import(command: str): - output = subprocess.check_output(["python", "-c", f"{command};print('OK')"]) + output = subprocess.check_output([sys.executable, "-c", f"{command};print('OK')"]) assert output.strip() == b"OK" return From 598407ecc9928e1cdc06cdb135c7102c7efbacd9 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Mon, 16 Dec 2024 17:28:30 +1100 Subject: [PATCH 8/9] More tests --- tests/test_deprecation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_deprecation.py b/tests/test_deprecation.py index 07b0204..a784aec 100644 --- a/tests/test_deprecation.py +++ b/tests/test_deprecation.py @@ -33,6 +33,7 @@ def test_jsonlogger_reserved_attrs_deprecated(): @pytest.mark.parametrize( "command", [ + "from pythonjsonlogger import jsonlogger", "import pythonjsonlogger.jsonlogger", "from pythonjsonlogger.jsonlogger import JsonFormatter", "from pythonjsonlogger.jsonlogger import RESERVED_ATTRS", From 130bf2af578debece23ac9afd4592e7104222be0 Mon Sep 17 00:00:00 2001 From: Nicholas Hairs Date: Mon, 16 Dec 2024 17:35:16 +1100 Subject: [PATCH 9/9] Add release info --- docs/changelog.md | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index fbb75b6..82d1cc7 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -4,7 +4,7 @@ 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) - UNRELEASED +## [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) diff --git a/pyproject.toml b/pyproject.toml index b4fbb8a..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.1.dev1" +version = "3.2.1" description = "JSON Log Formatter for the Python Logging Package" authors = [ {name = "Zakaria Zajac", email = "zak@madzak.com"},