python / python/mypy

Odd behavior when creating namedtuple

Aperta
#10,954 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic-error-reporting topic-named-tuple topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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 name to 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_1 give an error, while create_tuple_type_2 doesn't?

This might more issues, but since they occur so close together, they might be correlated?

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

Il reproducer è l’esempio inline test_file.py; inizia eseguendo mypy su di esso e confrontando le diagnosi per i casi di assegnazione di attributi, assegnazione locale e restituzione. Traccia la gestione delle chiamate a namedtuple e degli attributi annotati, quindi definisci come completato un comportamento coerente e documentato oppure diagnosi corrette per tutti e tre gli esempi.

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.