python / python/mypy

mypy doesn't recognize intra-package imports when qualified by `__all__`

Aperta
#10,826 3 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I have a package and am trying to control which classes/functions are available at the 'top level' of the package. For this purpose, I am using the __all__ dunder and star imports in the __init__.py files. I get unexpected behaviour from mypy when doing this: some of my objects are "not defined", when they in fact are.

To Reproduce

This bug has to do with imports between files, so to reproduce this, we need a bit of a package file structure. Consider the package gym:

gym
  sup/
    __init__.py
    bro.py
  __init__.py
  cool.py

File contents would be:

# sup/bro.py - This is the object we want to be available at the top level
__all__ = ["Bro"]

class Bro:
    pass
# sup/__init__.py - This brings the Bro object to the sup module level
from . import bro
from .bro import *

__all__ = bro.__all__
# __init__.py - This brings the sup module, including the Bro object, to the top module level
from .sup import *
# cool.py - This file uses the Bro object in a function
import gym

__all__ = ["story"]

def story() -> gym.Bro:
    return gym.Bro()

Now, when I run mypy gym/ I get the following errors:

gym/cool.py:6: error: Name "gym.Bro" is not defined
gym/cool.py:7: error: Module has no attribute "Bro"
Found 2 errors in 1 file (checked 4 source files)

However, when I remove the __all__ dunder from the sup/__init__.py file, everything works as expected, and I get no errors! Unfortunately this has the unwanted side-effect that the submodule gym.sup.bro becomes available as gym.bro, which I do not want.

Somehow that __all__ dunder stops mypy from recognizing that the Bro object should arrive at the module level above it. I can also work around the error by using the full path to the Bro object, gym.sup.bro.Bro, or even gym.sup.Bro, but not gym.Bro. This unfortunately gets very verbose in larger packages.

What's going on here?

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.5
  • Operating system and version: WSL Ubuntu 20

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il problema con i file del pacchetto gym indicati ed esegui mypy gym/. Inizia tracciando come vengono gestiti all e gli import con asterisco in sup/init.py durante la risoluzione di gym.Bro da cool.py. Il lavoro è completato quando gym.Bro viene riconosciuto senza esporre gym.bro e la riproduzione passa senza errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.