Skip to content

Commit 2bb784a

Browse files
committed
Update code unlocked by Python 3.10 minimum version
1 parent 79599b5 commit 2bb784a

File tree

8 files changed

+40
-53
lines changed

8 files changed

+40
-53
lines changed

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ maintainers = [
1616

1717
# Dependency Information
1818
requires-python = ">=3.10"
19-
dependencies = [
20-
"typing_extensions;python_version<'3.10'",
21-
]
2219

2320
# Extra information
2421
readme = "README.md"
@@ -55,7 +52,6 @@ dev = [
5552
## Test
5653
"pytest",
5754
"freezegun",
58-
"backports.zoneinfo;python_version<'3.9'",
5955
"tzdata",
6056
## Build
6157
"build",

src/pythonjsonlogger/core.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@
1010
import logging
1111
import re
1212
import sys
13-
from typing import Optional, Union, List, Dict, Container, Any, Sequence
14-
15-
if sys.version_info >= (3, 10):
16-
from typing import TypeAlias
17-
else:
18-
from typing_extensions import TypeAlias
13+
from typing import TypeAlias, Any
14+
from collections.abc import Container, Sequence
1915

2016
## Installed
2117

@@ -24,7 +20,7 @@
2420

2521
### CONSTANTS
2622
### ============================================================================
27-
RESERVED_ATTRS: List[str] = [
23+
RESERVED_ATTRS: list[str] = [
2824
"args",
2925
"asctime",
3026
"created",
@@ -71,7 +67,7 @@
7167

7268
## Type Aliases
7369
## -----------------------------------------------------------------------------
74-
LogData: TypeAlias = Dict[str, Any]
70+
LogData: TypeAlias = dict[str, Any]
7571
"""Type alias
7672
7773
*Changed in 4.0*: renamed from `LogRecord` to `LogData`
@@ -82,10 +78,10 @@
8278
### ============================================================================
8379
def merge_record_extra(
8480
record: logging.LogRecord,
85-
target: Dict,
81+
target: dict[Any, Any],
8682
reserved: Container[str],
87-
rename_fields: Optional[Dict[str, str]] = None,
88-
) -> Dict:
83+
rename_fields: dict[str, str] | None = None,
84+
) -> dict[Any, Any]:
8985
"""
9086
Merges extra attributes from LogRecord object into target dictionary
9187
@@ -121,25 +117,25 @@ class BaseJsonFormatter(logging.Formatter):
121117
*Added in 3.3*: `exc_info_as_array` and `stack_info_as_array` options are added.
122118
"""
123119

124-
_style: Union[logging.PercentStyle, str] # type: ignore[assignment]
120+
_style: logging.PercentStyle | str # type: ignore[assignment]
125121

126122
## Parent Methods
127123
## -------------------------------------------------------------------------
128124
# pylint: disable=too-many-arguments,super-init-not-called
129125
def __init__(
130126
self,
131-
fmt: Optional[Union[str, Sequence[str]]] = None,
132-
datefmt: Optional[str] = None,
127+
fmt: str | Sequence[str] | None = None,
128+
datefmt: str | None = None,
133129
style: str = "%",
134130
validate: bool = True,
135131
*,
136132
prefix: str = "",
137-
rename_fields: Optional[Dict[str, str]] = None,
133+
rename_fields: dict[str, str] | None = None,
138134
rename_fields_keep_missing: bool = False,
139-
static_fields: Optional[Dict[str, Any]] = None,
140-
reserved_attrs: Optional[Sequence[str]] = None,
141-
timestamp: Union[bool, str] = False,
142-
defaults: Optional[Dict[str, Any]] = None,
135+
static_fields: dict[str, Any] | None = None,
136+
reserved_attrs: Sequence[str] | None = None,
137+
timestamp: bool | str = False,
138+
defaults: dict[str, Any] | None = None,
143139
exc_info_as_array: bool = False,
144140
stack_info_as_array: bool = False,
145141
) -> None:
@@ -243,7 +239,7 @@ def format(self, record: logging.LogRecord) -> str:
243239
Args:
244240
record: the record to format
245241
"""
246-
message_dict: Dict[str, Any] = {}
242+
message_dict: dict[str, Any] = {}
247243
# TODO: logging.LogRecord.msg and logging.LogRecord.message in typeshed
248244
# are always type of str. We shouldn't need to override that.
249245
if isinstance(record.msg, dict):
@@ -276,7 +272,7 @@ def format(self, record: logging.LogRecord) -> str:
276272

277273
## JSON Formatter Specific Methods
278274
## -------------------------------------------------------------------------
279-
def parse(self) -> List[str]:
275+
def parse(self) -> list[str]:
280276
"""Parses format string looking for substitutions
281277
282278
This method is responsible for returning a list of fields (as strings)
@@ -327,9 +323,9 @@ def serialize_log_record(self, log_data: LogData) -> str:
327323

328324
def add_fields(
329325
self,
330-
log_data: Dict[str, Any],
326+
log_data: dict[str, Any],
331327
record: logging.LogRecord,
332-
message_dict: Dict[str, Any],
328+
message_dict: dict[str, Any],
333329
) -> None:
334330
"""Extract fields from a LogRecord for logging
335331
@@ -402,15 +398,15 @@ def process_log_record(self, log_data: LogData) -> LogData:
402398
"""
403399
return log_data
404400

405-
def formatException(self, ei) -> Union[str, list[str]]: # type: ignore
401+
def formatException(self, ei) -> str | list[str]: # type: ignore[override]
406402
"""Format and return the specified exception information.
407403
408404
If exc_info_as_array is set to True, This method returns an array of strings.
409405
"""
410406
exception_info_str = super().formatException(ei)
411407
return exception_info_str.splitlines() if self.exc_info_as_array else exception_info_str
412408

413-
def formatStack(self, stack_info) -> Union[str, list[str]]: # type: ignore
409+
def formatStack(self, stack_info) -> str | list[str]: # type: ignore[override]
414410
"""Format and return the specified stack information.
415411
416412
If stack_info_as_array is set to True, This method returns an array of strings.

src/pythonjsonlogger/defaults.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,11 @@
1616
import dataclasses
1717
import datetime
1818
import enum
19-
import sys
2019
from types import TracebackType
21-
from typing import Any
20+
from typing import Any, TypeGuard
2221
import traceback
2322
import uuid
2423

25-
if sys.version_info >= (3, 10):
26-
from typing import TypeGuard
27-
else:
28-
from typing_extensions import TypeGuard
29-
3024
## Installed
3125

3226
## Application

src/pythonjsonlogger/json.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
## Standard Library
1313
import datetime
1414
import json
15-
from typing import Any, Callable, Optional, Union
15+
from typing import Any
16+
from collections.abc import Callable
1617
import warnings
1718

1819
## Application
@@ -67,10 +68,10 @@ class JsonFormatter(core.BaseJsonFormatter):
6768
def __init__(
6869
self,
6970
*args,
70-
json_default: Optional[Callable] = None,
71-
json_encoder: Optional[Callable] = None,
71+
json_default: Callable | None = None,
72+
json_encoder: Callable | None = None,
7273
json_serializer: Callable = json.dumps,
73-
json_indent: Optional[Union[int, str]] = None,
74+
json_indent: int | str | None = None,
7475
json_ensure_ascii: bool = True,
7576
**kwargs,
7677
) -> None:

src/pythonjsonlogger/msgspec.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from __future__ import annotations
77

88
## Standard Library
9-
from typing import Any, Optional, Callable
9+
from typing import Any
10+
from collections.abc import Callable
1011

1112
## Installed
1213

@@ -43,7 +44,7 @@ class MsgspecFormatter(core.BaseJsonFormatter):
4344
def __init__(
4445
self,
4546
*args,
46-
json_default: Optional[Callable] = msgspec_default,
47+
json_default: Callable | None = msgspec_default,
4748
**kwargs,
4849
) -> None:
4950
"""

src/pythonjsonlogger/orjson.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from __future__ import annotations
77

88
## Standard Library
9-
from typing import Any, Optional, Callable
9+
from typing import Any
10+
from collections.abc import Callable
1011

1112
## Installed
1213

@@ -45,7 +46,7 @@ class OrjsonFormatter(core.BaseJsonFormatter):
4546
def __init__(
4647
self,
4748
*args,
48-
json_default: Optional[Callable] = orjson_default,
49+
json_default: Callable | None = orjson_default,
4950
json_indent: bool = False,
5051
**kwargs,
5152
) -> None:

tests/test_dictconfig.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import json
1010
import logging
1111
import logging.config
12-
from typing import Any, Generator
12+
from typing import Any
13+
from collections.abc import Generator
1314

1415
## Installed
1516
import pytest
@@ -64,7 +65,7 @@ def load_json(self) -> Any:
6465

6566

6667
@pytest.fixture
67-
def env() -> Generator[LoggingEnvironment, None, None]:
68+
def env() -> Generator[LoggingEnvironment]:
6869
global _LOGGER_COUNT # pylint: disable=global-statement
6970
_LOGGER_COUNT += 1
7071
logging.config.dictConfig(LOGGING_CONFIG)

tests/test_formatters.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
import sys
1414
import traceback
1515
from types import TracebackType
16-
from typing import Any, Generator
16+
from typing import Any
17+
from collections.abc import Generator
1718
import uuid
18-
19-
if sys.version_info >= (3, 9):
20-
import zoneinfo
21-
else:
22-
from backports import zoneinfo
19+
import zoneinfo
2320

2421
## Installed
2522
import freezegun
@@ -63,7 +60,7 @@ def load_json(self) -> Any:
6360

6461

6562
@pytest.fixture
66-
def env() -> Generator[LoggingEnvironment, None, None]:
63+
def env() -> Generator[LoggingEnvironment]:
6764
global _LOGGER_COUNT # pylint: disable=global-statement
6865
_LOGGER_COUNT += 1
6966
logger = logging.getLogger(f"pythonjsonlogger.tests.{_LOGGER_COUNT}")

0 commit comments

Comments
 (0)