python / python/mypy

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

Offen
#16,783 2 Kommentare 0 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
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

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

Reproduziere den Bericht mit den genannten Dateien graph.py und abc.py, indem du mypy graph.py ausführst. Beginne damit nachzuverfolgen, wie mypy die benutzerdefinierte Datei abc.py zusammen mit dem Bibliotheksmodul abc und seiner Diagnose zur Modulüberschattung verarbeitet. Als abgeschlossen sollte gelten, dass das beabsichtigte Verhalten für diesen reproduzierbaren Fall festgestellt wurde und die gemeldete Diagnose korrigiert oder eindeutig behoben ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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