Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Annotation For a Python Logger

I'm quite new to incorporating type hints in my Python code and often feel that I am on shaky ground while doing so. I have a function that creates and returns a custom logger and I'm wondering if I have hinted at the return type correctly.

import logging

def get_logger(param1: str, ...) -> logging.getLogger:
    logger = logging.getLogger(__name__)
    
    # define format
    # add handlers
   
    return logger 

Is logging.getLogger the correct return type in this case? If not, what is the correct type?

like image 943
navneethc Avatar asked May 05 '26 16:05

navneethc


1 Answers

Type hints are supposed to use types. In the case of getLogger() it's logging.Logger:

import logging

def get_logger(param1: str, ...) -> logging.Logger:
    logger = logging.getLogger(__name__)
    
    # define format
    # add handlers
   
    return logger
like image 168
bereal Avatar answered May 08 '26 07:05

bereal