airtai / faststream

FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
https://faststream.airt.ai/latest/
Apache License 2.0
2.4k stars 120 forks source link

Bug: Reload option sometimes triggers TypeError: not all arguments converted during string formatting #1364

Closed pySilver closed 5 months ago

pySilver commented 5 months ago

Describe the bug

I am not sure what really causes this error. My wild guess is that it is somehow related to the fact that I'm using structlog that was configured per official instructions described here: https://faststream.airt.ai/latest/getting-started/logging/?h=structlog#structlog-example. Error does not happen all the time but quite frequently during development.

How to reproduce

# logger.py
import sys

import structlog

from faststream import context

def merge_contextvars(
    logger: structlog.types.WrappedLogger,
    method_name: str,
    event_dict: structlog.types.EventDict,
) -> structlog.types.EventDict:
    event_dict["extra"] = event_dict.get(
        "extra",
        context.get("log_context", {}),
    )
    return event_dict

shared_processors = [
    merge_contextvars,
    structlog.processors.add_log_level,
    structlog.processors.StackInfoRenderer(),
    structlog.dev.set_exc_info,
    structlog.processors.TimeStamper(fmt="iso"),
]

if sys.stderr.isatty():
    processors = shared_processors + [structlog.dev.ConsoleRenderer()]
else:
    processors = shared_processors + [
        structlog.processors.dict_tracebacks,
        structlog.processors.JSONRenderer(),
    ]

structlog.configure(
    processors=processors,
    logger_factory=structlog.PrintLoggerFactory(),
    cache_logger_on_first_use=False,
)
logger = structlog.get_logger()

integration with an app:

# serve.py
import logging

from faststream import FastStream
from faststream.nats import NatsBroker

from logger import logger

broker = NatsBroker(
    "nats://localhost:4222",
    logger=logger,
    log_level=logging.DEBUG,
    description="IO-bound service",
)
broker.include_router(router)
app = FastStream(broker, logger=logger)
...

And/Or steps to reproduce the behavior:

  1. Integrate structlog per documentation
  2. Run the app with --reload option faststream run serve:app --reload

Expected behavior No TypeError on reload.

Observed behavior

2024-04-11T16:55:56.073888Z [info     ] FastStream app started successfully! To exit, press CTRL+C extra={}
Traceback (most recent call last):

  File "/Users/Silver/.pyenv/versions/3.12.1/envs/jetstream/bin/faststream", line 8, in <module>
    sys.exit(cli())
             ^^^^^

  File "/Users/Silver/.pyenv/versions/3.12.1/envs/jetstream/lib/python3.12/site-packages/faststream/cli/main.py", line 137, in run
    ).run()
      ^^^^^

  File "/Users/Silver/.pyenv/versions/3.12.1/envs/jetstream/lib/python3.12/site-packages/faststream/cli/supervisors/basereload.py", line 88, in run
    if self.should_restart():  # pragma: no branch
       ^^^^^^^^^^^^^^^^^^^^^

  File "/Users/Silver/.pyenv/versions/3.12.1/envs/jetstream/lib/python3.12/site-packages/faststream/cli/supervisors/watchfiles.py", line 89, in should_restart
    logger.info(message % tuple(unique_paths))
                ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

TypeError: not all arguments converted during string formatting

Environment Running FastStream 0.4.6 with CPython 3.12.1 on Darwin

Lancetnik commented 5 months ago

It doesn't related to structlog.

Seems like Path() object can't be casted to str in your case. I'll try to fix it by manual casting, but not sure. I am just can't reproduce your case, sorry