Type hints & PEP 561 packaging
Add type hints to all (public API) code make the distribution PEP 561 compliant. This allows mypy (and other tools?) to find the type hints and use them in linting.
In practice, add py.typed file to src/pythonjsonlogger, and include it in the package:
setup(
package_data={'pythonjsonlogger': ['py.typed']},
zip_safe=False, # not needed with wheels, AFAIK
)
PEP 561: https://www.python.org/dev/peps/pep-0561/
@madzak Would you accept a PR for this?
Sure!
I'm having trouble figuring out what should be done in this case:
def format(self, record):
"""Formats a log record and serializes to json"""
message_dict = {}
if isinstance(record.msg, dict):
message_dict = record.msg
record.message = None
else:
record.message = record.getMessage()
with a related test:
def testJsonCustomDefault(self):
def custom(o):
return "very custom"
fr = jsonlogger.JsonFormatter(json_default=custom)
self.logHandler.setFormatter(fr)
msg = {"adate": datetime.datetime(1999, 12, 31, 23, 59),
"normal": "value"}
self.logger.info(msg)
logJson = json.loads(self.buffer.getvalue())
self.assertEqual(logJson.get("adate"), "very custom")
self.assertEqual(logJson.get("normal"), "value")
Note that record.msg is type of Union[str, Dict] here, but logging.LogRecord.msg in typeshed is type of str (same deal with message).
I'll think about it a bit and see what I can do, but any help would be appreciated.
Possible workaround is over at https://github.com/madzak/python-json-logger/pull/133
Any additional progress on this? Do we have a list of what needs to be completed in order to achieve PEP 561 compliance?
There's already a py.typed file in the repo, but it doesn't seem to be included in the packages uploaded to PyPI.
@madzak perhaps https://github.com/madzak/python-json-logger/pull/156 would bring this issue closer to done?
@madzak any updates on this?