#๐Ÿ”’ Vscode and Python Intellisense

4 messages ยท Page 1 of 1 (latest)

pastel pilot
#
import json
import logging.config
from typing import cast, TYPE_CHECKING
import atexit
import pathlib

if TYPE_CHECKING:
    from utils.logger import QueueHandlerWithListener

class Logger:
    def __init__(self):
        self.logger = logging.getLogger("test")
        config_file = pathlib.Path("config/logging.json")
        with open(config_file, 'r') as file:
            config = json.load(file)
        logging.config.dictConfig(config)
        queue_handler = logging.getHandlerByName("queue_handler")
        if queue_handler is not None:
            queue_handler = cast("QueueHandlerWithListener", queue_handler)
            queue_handler.listener.start()
            atexit.register(queue_handler.listener.stop)

    def get_logger(self):
        return self.logger

This is the pyi:

import logging
import logging.handlers

class Logger:
    def __init__(self) -> None: ...
    def get_logger(self) -> logging.Logger: ...

class QueueHandlerWithListener(logging.handlers.QueueHandler):
    listener: logging.handlers.QueueListener

so I came together with this because if I just didn't have the cast vscode would highlight listener. Curious if there is a better way.

spark acornBOT
#

@pastel pilot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

spark acornBOT
#

@pastel pilot

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.