1

For a new application, I want to set up logging. The app is going to contain several modules, that will require the same kind of configuration. So I create a module my_company/logging.py:

import logging  # the standard Python logging

def configure():
  logging.getLogger('module1').setLevel(logging.INFO)
  logging.getLogger('module2').setLevel(logging.WARNING)

mypy does not really like this:

my_company/logging.py:6: error: Module has no attribute "getLogger"
Found 1 error in 1 file (checked 3 source files)

How can I make mypy see that it should use the logging module python distribution, not from the my_company folder?

xtofl
  • 40,723
  • 12
  • 105
  • 192
  • have you tried something like `import logging as lg`? – Orestis Zekai Feb 16 '21 at 09:36
  • Thansl - I did, to no avail :( – xtofl Feb 16 '21 at 09:43
  • 1
    How do you use ``my_company/logging.py``? As the name implies, it should be ``my_company.logger`` which would not clash with ``logging``. Be aware that you cannot generally have two modules of the *exact* same name, since modules are application-global. – MisterMiyagi Feb 17 '21 at 09:58
  • @MisterMiyagi indeed. My only problem is with `mypy` trying to use _my_ logging module. I was hoping something was wrong with my code, but maybe it's my mypy. – xtofl Feb 18 '21 at 08:45
  • 1
    Again, how do you use `my_company/logging.py`? A naive reconstruction does *not* reproduce the error for me. Please provide a [mcve] – the minimum number of files, their content, how they are arranged and how mypy is run. – MisterMiyagi Feb 18 '21 at 08:50
  • :( problem disappeared for a while. I'll be back when it reappears. – xtofl Feb 18 '21 at 14:52
  • Hey @xtofl did you try the solution by Cesar below? https://stackoverflow.com/a/69852452/110395 It really seems to be the missing __init__.py as I also had the same problem and it's easily reproducible if you omit the __init__.py from the directory where the module which shadows the built-in module resides. – Lefteris Mar 24 '22 at 21:52
  • @Lefteris I must have done 'something' to make it go away, and I don't remember what. So I didn't consciously try it. I did take a habit, however, to avoid choosing module names that are in the standard library already. – xtofl Mar 28 '22 at 05:42

1 Answers1

3

It could be a namespaced package problem

I had exactly the same problem and it was caused by a missing __init__.py file.

Adding it fixed the problem.

Cesar Canassa
  • 18,659
  • 11
  • 66
  • 69