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

Commit 838939b

Browse files
authored
Merge pull request #129 from bringhurst/mypy
Add PEP 561 marker
2 parents 672bf6d + 729991f commit 838939b

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

setup.cfg

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,52 @@
33
# 3. If at all possible, it is good practice to do this. If you cannot, you
44
# will need to generate wheels for each Python version that you support.
55
python-tag=py3
6+
7+
[mypy]
8+
9+
# For details on each flag, please see the mypy documentation at:
10+
# https://mypy.readthedocs.io/en/stable/config_file.html#config-file
11+
12+
# Import Discovery
13+
mypy_path = src
14+
namespace_packages = true
15+
16+
# Disallow dynamic typing
17+
disallow_any_unimported = true
18+
disallow_any_expr = false
19+
disallow_any_decorated = true
20+
disallow_any_explicit = true
21+
disallow_any_generics = false
22+
disallow_subclassing_any = true
23+
24+
# Untyped definitions and calls
25+
disallow_untyped_calls = false
26+
disallow_untyped_defs = false
27+
disallow_incomplete_defs = true
28+
check_untyped_defs = true
29+
disallow_untyped_decorators = true
30+
31+
# None and Optional handling
32+
no_implicit_optional = true
33+
34+
# Configuring warnings
35+
warn_redundant_casts = true
36+
warn_unused_ignores = true
37+
warn_no_return = true
38+
warn_return_any = true
39+
warn_unreachable = true
40+
41+
# Miscellaneous strictness flags
42+
implicit_reexport = true
43+
strict_equality = true
44+
45+
# Configuring error messages
46+
show_error_context = true
47+
show_column_numbers = true
48+
show_error_codes = true
49+
pretty = true
50+
show_absolute_path = true
51+
52+
# Miscellaneous
53+
warn_unused_configs = true
54+
verbosity = 0

src/pythonjsonlogger/jsonlogger.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
import traceback
1010
import importlib
1111

12+
from typing import Dict, Union, List, Tuple
13+
1214
from inspect import istraceback
1315

1416
from collections import OrderedDict
1517

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

2426

25-
def merge_record_extra(record, target, reserved):
27+
def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[Dict, List]) -> Dict:
2628
"""
2729
Merges extra attributes from LogRecord object into target dictionary
2830
@@ -138,7 +140,7 @@ def _str_to_fn(self, fn_as_str):
138140
module = importlib.import_module(path)
139141
return getattr(module, function)
140142

141-
def parse(self):
143+
def parse(self) -> List[str]:
142144
"""
143145
Parses format string looking for substitutions
144146
@@ -155,7 +157,11 @@ def parse(self):
155157
formatter_style_pattern = re.compile(r'%\((.+?)\)s', re.IGNORECASE)
156158
else:
157159
raise ValueError('Invalid format: %s' % self._fmt)
158-
return formatter_style_pattern.findall(self._fmt)
160+
161+
if self._fmt:
162+
return formatter_style_pattern.findall(self._fmt)
163+
else:
164+
return []
159165

160166
def add_fields(self, log_record, record, message_dict):
161167
"""
@@ -220,6 +226,7 @@ def format(self, record):
220226
# Python2.7 doesn't have stack_info.
221227
pass
222228

229+
log_record: Dict
223230
try:
224231
log_record = OrderedDict()
225232
except NameError:

src/pythonjsonlogger/py.typed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# PEP-561 marker. https://mypy.readthedocs.io/en/latest/installed_packages.html

tox.ini

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ python =
1111
3.10: py310
1212

1313
[testenv]
14-
commands = python -m unittest discover
14+
deps =
15+
mypy
16+
commands =
17+
python -m unittest discover
18+
mypy src

0 commit comments

Comments
 (0)