python / python/mypy

False positive [attr-defined] when using namespace_packages

Offen
#13,663 3 Kommentare 8 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When using namespace_packages = true, if it happens that a directory with the same name as a module exists, mypy will not recognize names from the module.

To Reproduce

This is a minimal example, tested with python 3.8 and mypy 0.971:

.
├── mypkg
│   ├── foo  # empty directory
│   ├── foo.py
│   └── bar.py
└── mypy.ini
# mypy.ini
[mypy]
namespace_packages = true
show_error_codes = true
# mypkg/bar.py
from mypkg.foo import func
# mypkg/foo.py
def func():
    pass

While python correctly ignores the empty directory mypkg/foo, mypy does not:

# All these are successful
$ python -c 'import mypkg.bar'
$ python -c 'import mypkg.foo'
$ python -c 'from mypkg.foo import func'

# These calls to mypy fail
$ mypy -p mypkg
mypkg/bar.py:1: error: Module "mypkg.foo" has no attribute "func"  [attr-defined]
Found 1 error in 1 file (checked 3 source files)
$ mypy mypkg/bar.py 
mypkg/bar.py:1: error: Module "mypkg.foo" has no attribute "func"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

Mypy should ignore the empty directory and run successfully on this package. According to the docs, a package should be found (and have precedence over a module) if a directory has __init__.py or __init__.pyi in it.

In the case of namespace_packages = true, it seems that mypy is satisfied with a directory missing an __init__.py file and does not look for the module of the same name. This differs from the python search order specified in PEP-420, where the following are searched in turn: <directory>/foo/__init__.py, <directory>/foo.{py,pyc,so,pyd}, <directory>/foo, meaning that a namespace package is the last priority.

Actual Behavior

Mypy detects mypkg/foo as a package before module mypkg/foo.py and does not recognize the names in mypkg/foo.py, resulting in false positive [attr-defined] errors.

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: -p mypkg or mypkg/bar.py
  • Mypy configuration options from mypy.ini (and other config files): namespace_packages = true
  • Python version used: 3.8.5
  • Operating system and version: Linux (Ubuntu 18.04)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Erstelle das minimale mypkg/foo-Verzeichnis und das foo.py-Layout aus dem Bericht nach, mit namespace_packages = true in mypy.ini, und führe mypy -p mypkg und mypy mypkg/bar.py aus. Verfolge, wie mypy den Import in mypkg/bar.py auflöst, wenn sowohl mypkg/foo als auch mypkg/foo.py existieren; abgeschlossen ist die Aufgabe, wenn die func des Moduls erkannt wird und beide Prüfungen ohne [attr-defined] erfolgreich sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.