python / python/typeshed

`configparser.RawConfigParser` stub does not account for non-string options

Aperta Adatta ai principianti
#6,820 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stubs: false positive
Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

The stub for configparser.RawConfigParser.set has its value argument typed as str | None (source) when it can accept lots of other types and is leading to a false positive in mypy for me.

Consider this valid script:

from configparser import RawConfigParser

cfg = RawConfigParser()
cfg.add_section("default")

cfg.set("default", "some_option1", True)
r1 = cfg.get("default", "some_option1")
print(r1, type(r1))


cfg.set("default", "some_option2", 1)
r2 = cfg.get("default", "some_option2")
print(r2, type(r2))

cfg.set("default", "some_option3", b"bytes")
r3 = cfg.get("default", "some_option3")
print(r3, type(r3))

It prints:

True <class 'bool'>
1 <class 'int'>
b'bytes' <class 'bytes'>

However, mypy flags this code for the reason described above:

test.py:6: error: Argument 3 to "set" of "RawConfigParser" has incompatible type "bool"; expected "Optional[str]"
test.py:11: error: Argument 3 to "set" of "RawConfigParser" has incompatible type "int"; expected "Optional[str]"
test.py:15: error: Argument 3 to "set" of "RawConfigParser" has incompatible type "bytes"; expected "Optional[str]"

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

Inizia da stdlib/configparser.pyi intorno alla riga 133 e confronta la firma di RawConfigParser.set con il comportamento mostrato nell’issue. Esegui lo script fornito tramite mypy per riprodurre i falsi positivi, quindi verifica che i valori booleani, interi e bytes siano accettati senza errori, mentre il comportamento esistente di get rimanga coperto.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.