Skip to content

mypy complains that logging.Formater does not have a converter attr #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jaloren opened this issue Nov 29, 2016 · 1 comment
Closed

Comments

@jaloren
Copy link

jaloren commented Nov 29, 2016

I have python 2.7 code with a function that creates a logger. I put a type annotation in the comment, where the return type is logging.Logger. I use mypy 0.4.6 on python 3.5.2.

When i run the type checker, i get this error.

mypy --py2 --silent-imports --fast-parser -i script.py
script.py: note: In function "get_logger":
script.py:44: error: "Formatter" has no attribute "converter"

However, this is wrong because the log formatter does in fact have a converter attribute.

Code example below.

   
   import logging
   import time

   def get_logger(loglevel=logging.INFO):
    #type: (...) -> logging.Logger
    global_logger = logging.getLogger("script")
    formatter = logging.Formatter('%(asctime)s UTC - %(levelname)s - %(message)s - func: %(funcName)s - lineno: %(lineno)d')
    formatter.converter = time.gmtime
    ch = logging.StreamHandler()
    ch.setLevel(loglevel)
    ch.setFormatter(formatter)
    global_logger.addHandler(ch)
    global_logger.setLevel(loglevel)
    return global_logger
@ambv
Copy link
Contributor

ambv commented Nov 29, 2016

Preparing a pull request.

ambv pushed a commit that referenced this issue Nov 29, 2016
Fixes #720.

Related changes: used a NamedTuple for time.struct_time on Python 3, used
sys.version selectors for proper typing of #716, added missing *Style classes
to logging on Python 3.
ambv pushed a commit that referenced this issue Nov 29, 2016
Fixes #720.

Related changes: used a NamedTuple for time.struct_time on Python 3, used
sys.version selectors for proper typing of #716, added missing *Style classes
to logging on Python 3.
ambv pushed a commit that referenced this issue Nov 30, 2016
Fixes #720.

Related changes: used a NamedTuple for time.struct_time on Python 3, used
sys.version selectors for proper typing of #716, added missing *Style classes
to logging on Python 3.
gvanrossum pushed a commit that referenced this issue Nov 30, 2016
Fixes #720.

Related changes: used a NamedTuple for time.struct_time on Python 3, used
sys.version selectors for proper typing of #716, added missing *Style classes
to logging on Python 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants