python / python/mypy

note: A user-defined top-level module with name "XXX" is not supported

Aperta
#16,783 2 commenti 0 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 get the following unexpected error.

mypy graph.py
Then, I got the following error.

mypy: "abc.py" shadows library module "abc"
note: A user-defined top-level module with name "abc" is not supported

In the same folder there is a file named abc.py. However I did not check the 'abc.py'. Why I got this kind of error ??

To Reproduce

# graph.py
from collections import defaultdict
from typing import Generic, NewType, TypeVar, Union, List

Restaurant = NewType('Restaurant', str)
Recipe = NewType('Recipe', str)


Node = TypeVar("Node")
Edge = TypeVar("Edge")

class Graph(Generic[Node, Edge]):
    def __init__(self):
        
        self.edges: dict[Node, List[Edge]] = defaultdict(list)

    def add_relation(self, node: Node, to: Edge):
        self.edges[node].append(to)

    def get_relations(self, node: Node) -> List[Edge]:
        return self.edges[node]

restaurants: Graph[Restaurant, Restaurant] = Graph()
recipes: Graph[Recipe, Recipe] = Graph()

restaurant_dishes: Graph[Restaurant, Recipe] = Graph()

recipes.add_relation(Recipe('Pasta Bolognese'), 
                     Recipe('Pasta with Sausage and Basil'))

restaurant_dishes.add_relation(Restaurant('Viafores'),
                               Recipe('Pasta Bolognese'))


# abc.py
import collections
import collections.abc
from typing import Set 


def get_nutrition_information(text):
    return "arugula"

def get_aliases(text):
    if text == 'rocket':
        return ['arugula']

class AliasedIngredients(collections.abc.Set):
    def __init__(self, ingredients: set[str]):
        self.ingredients = ingredients
    
    def __contains__(self, value: str):
        return value in self.ingredients or any(alias in self.ingredients for alias in get_aliases(value))

    def __iter__(self):
        return iter(self.ingredients)

    def __len__(self):
        return len(self.ingredients)

ingredients = AliasedIngredients({'arugula', 'eggplant', 'pepper'})
assert ingredients == {'arugula', 'eggplant', 'pepper'}

assert len(ingredients) == 3

assert 'arugula' in ingredients
assert 'rocket' in ingredients

Expected Behavior

Actual Behavior

Your Environment

  • Mypy version used: mypy 1.5.1 (compiled: no)
  • Mypy command-line flags: unknown
  • Mypy configuration options from mypy.ini (and other config files):unknown
  • Python version used:3.8.18

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 report con i file menzionati graph.py e abc.py eseguendo mypy graph.py. Inizia tracciando il modo in cui mypy gestisce il file abc.py definito dall’utente insieme al modulo di libreria abc e alla relativa diagnostica di ombreggiamento del modulo. Il lavoro dovrebbe considerarsi concluso quando sarà stato stabilito il comportamento previsto per questo caso riproducibile e la diagnostica segnalata sarà stata corretta o chiaramente risolta.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.