Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Add PEP 561 marker #129

Merged
merged 6 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,52 @@
# 3. If at all possible, it is good practice to do this. If you cannot, you
# will need to generate wheels for each Python version that you support.
python-tag=py3

[mypy]

# For details on each flag, please see the mypy documentation at:
# https://mypy.readthedocs.io/en/stable/config_file.html#config-file

# Import Discovery
mypy_path = src
namespace_packages = true

# Disallow dynamic typing
disallow_any_unimported = true
disallow_any_expr = false
disallow_any_decorated = true
disallow_any_explicit = true
disallow_any_generics = false
disallow_subclassing_any = true

# Untyped definitions and calls
disallow_untyped_calls = false
disallow_untyped_defs = false
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true

# None and Optional handling
no_implicit_optional = true

# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true

# Miscellaneous strictness flags
implicit_reexport = true
strict_equality = true

# Configuring error messages
show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true
show_absolute_path = true

# Miscellaneous
warn_unused_configs = true
verbosity = 0
15 changes: 11 additions & 4 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
import traceback
import importlib

from typing import Dict, Union, List, Tuple

from inspect import istraceback

from collections import OrderedDict

# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
RESERVED_ATTRS = (
RESERVED_ATTRS: Tuple[str, ...] = (
'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename',
'funcName', 'levelname', 'levelno', 'lineno', 'module',
'msecs', 'message', 'msg', 'name', 'pathname', 'process',
'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName')


def merge_record_extra(record, target, reserved):
def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[Dict, List]) -> Dict:
"""
Merges extra attributes from LogRecord object into target dictionary

Expand Down Expand Up @@ -138,7 +140,7 @@ def _str_to_fn(self, fn_as_str):
module = importlib.import_module(path)
return getattr(module, function)

def parse(self):
def parse(self) -> List[str]:
"""
Parses format string looking for substitutions

Expand All @@ -155,7 +157,11 @@ def parse(self):
formatter_style_pattern = re.compile(r'%\((.+?)\)s', re.IGNORECASE)
else:
raise ValueError('Invalid format: %s' % self._fmt)
return formatter_style_pattern.findall(self._fmt)

if self._fmt:
return formatter_style_pattern.findall(self._fmt)
else:
return []

def add_fields(self, log_record, record, message_dict):
"""
Expand Down Expand Up @@ -220,6 +226,7 @@ def format(self, record):
# Python2.7 doesn't have stack_info.
pass

log_record: Dict
try:
log_record = OrderedDict()
except NameError:
Expand Down
1 change: 1 addition & 0 deletions src/pythonjsonlogger/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# PEP-561 marker. https://mypy.readthedocs.io/en/latest/installed_packages.html
6 changes: 5 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ python =
3.10: py310

[testenv]
commands = python -m unittest discover
deps =
mypy
commands =
python -m unittest discover
mypy src