Odd behavior when creating namedtuple
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I am writing a descriptor and attempting to annotate using the typing module. I think I have found a bug, but since I am pretty new to mypy I am posting it here. Below is the behavior I encountered:
# test_file.py
from collections import namedtuple
class Snapshot:
def create_tuple_type(self, name: str):
self.tuple_type = namedtuple(name, []) # line 6
def create_tuple_type_1(name: str):
x = namedtuple(name, []) # line 9
def create_tuple_type_2(name: str):
return namedtuple(name, [])
running: mypy test_file.py prints:
snapshot\tinutest.py:6: error: NamedTuple type as an attribute is not supported
snapshot\tinutest.py:6: error: namedtuple() expects a string literal as the first argument
snapshot\tinutest.py:9: error: namedtuple() expects a string literal as the first argument
Found 3 errors in 1 file (checked 1 source file)
I am confused:
- what does the first error mean, I am not using NamedTuple (also NamedTuple is not a type, but a function)?
- I explicitly set
nameto be a string, why the second and third error (it says 'literal', but there is no reason this would not work, it actually does in practice ;-)? - why does
create_tuple_type_1give an error, whilecreate_tuple_type_2doesn't?
This might more issues, but since they occur so close together, they might be correlated?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Der Reproducer ist das Inline-Beispiel test_file.py; beginne damit, mypy dagegen auszuführen und die Diagnosen für die Fälle der Attributzuweisung, der lokalen Zuweisung und der Rückgabe zu vergleichen. Verfolge die Behandlung von namedtuple-Aufrufen und annotierten Attributen und definiere als erledigt ein konsistentes, dokumentiertes Verhalten oder korrigierte Diagnosen für alle drei Beispiele.
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