ContainsOnly raises TypeError for unhashable input with set-based choices

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

I maintainer di solito rispondono entro 1 giorno

@prathamchoksi ci sta già lavorando.

Dal 16/11/2026.

  • #3116 di @prathamchoksi — aperta

Valutazione

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

Direzione di ricerca

Leggi le implementazioni di ContainsOnly e OneOf nel modulo validators, quindi individua i test dei validatori per le scelte basate su insiemi. Riproduci il problema con un input non hashable, ad esempio un dict, e conferma che sia load sia validate restituiscono normali errori di validazione. Il lavoro è concluso quando i test di regressione passano e il messaggio di errore a livello di raccolta viene preservato.

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

Descrizione

ContainsOnly raises an uncaught TypeError when its choices are a set or frozenset and an input element is unhashable. OneOf converts the same membership error into ValidationError, but ContainsOnly overrides that method without the exception handling.

Reproduced on dev at 777ff660 (marshmallow 4.4.0, Python 3.14, Windows):

from marshmallow import Schema, fields, validate

class PermissionSchema(Schema):
    permissions = fields.List(
        fields.Raw(), validate=validate.ContainsOnly({"read", "write"})
    )

schema = PermissionSchema()
schema.load({"permissions": [{}]})
# TypeError: cannot use 'dict' as a set element (unhashable type: 'dict')

schema.validate({"permissions": [["read"]]})
# TypeError: cannot use 'list' as a set element (unhashable type: 'list')

Expected: load should raise a field-level ValidationError, and validate should return the permissions error. Both input elements are outside the allowed choices. With list-based choices, invalid elements already produce a normal validation error.

The uncaught exception prevents callers from handling malformed input through marshmallow's normal validation API. A focused fix is to handle membership TypeError in ContainsOnly, matching OneOf while preserving the collection-level error message.

Codex assisted with reproducing the bug and preparing regression tests. Both examples above were executed locally.

Lingua principale
Python
Stelle
7.2k
Fork
790
Merge medio
10h 40m
PR unite (30g)
6

Preparare l'ambiente

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.

Altre issue di marshmallow-code/marshmallow

Tutte le issue di marshmallow-code/marshmallow

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.