python-json-logger icon indicating copy to clipboard operation
python-json-logger copied to clipboard

Type hints & PEP 561 packaging

Open tuukkamustonen opened this issue 4 years ago • 5 comments

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/

tuukkamustonen avatar May 07 '21 09:05 tuukkamustonen

@madzak Would you accept a PR for this?

chdsbd avatar Oct 10 '21 18:10 chdsbd

Sure!

madzak avatar Oct 10 '21 19:10 madzak

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

bringhurst avatar Feb 06 '22 18:02 bringhurst

Any additional progress on this? Do we have a list of what needs to be completed in order to achieve PEP 561 compliance?

alexmirrington avatar Sep 11 '22 23:09 alexmirrington

There's already a py.typed file in the repo, but it doesn't seem to be included in the packages uploaded to PyPI.

mwgamble avatar Sep 17 '22 13:09 mwgamble

@madzak perhaps https://github.com/madzak/python-json-logger/pull/156 would bring this issue closer to done?

akshayphdk avatar Oct 25 '22 20:10 akshayphdk

@madzak any updates on this?

Eyal-Shalev avatar Nov 21 '22 15:11 Eyal-Shalev