Derived class disambiguating fails, but only sometimes.
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
Direzione di ricerca
Start by reproducing the supplied test_parse_bad_class case with my_converter, BaseClass, and BadSubClass, checking whether structure sometimes selects BaseClass and reports the subclass keys as extra. Trace the disambiguation behavior involved in structuring the shown json_obj, then make the selection deterministic and verify the result is always a BadSubClass.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
- cattrs version: 23.2.3
- Python version: 3.9
- Operating System: MacOS and Linux
Description
I have a set of classes that all inherit from one base class. I have one class that has a unique required attribute to differentiate it defined like this:
@define(kw_only=True, init=False)
class BadSubClass(BaseClass):
# This "arg1" field is used in other derived classes, but is never required in any other class.
# BTW, my converter has registered structure hooks for Union[str, List[str]], which forward to str_to_list.
arg1: Union[List[str], str] = field(converter=str_to_list) # Converter converts single str to a list.
other_arg1: Union[List[str], str] = field(factory=list, converter=str_to_list)
other_arg2: Optional[str] = None
I have a unit test which tries to structure json with this BadSubClass. About half the time I get an exception that the BaseClass deserializing got extra keys (the keys of BadSubClass). Well I say half the time, but now that I'm trying to recreate the error, it won't throw the error. (You might say I'm just crazy. You'd be right, but not for this reason.)
The question is, Has anyone seen this disambiguating problem, especially when it is transient like this?
What I Did
Test case looks like this:
def test_parse_bad_class():
# Aargh: cattrs is non-deterministic in whether it can deserialize this class.
# Should work all the time, but sometimes it throws an exception.
json_obj = {
"arg1": "foo",
"other_arg1": "blah"
"base_class_arg4": { # This is a different non-sub-class
"key1": "..."
},
}
result = my_converter.structure(json_obj, BaseClass)
assert isinstance(result, BadSubClass)
- Lingua principale
- Python
- Stelle
- 1.1k
- Fork
- 159
- Merge medio
- 12h 21m
- PR unite (30g)
- 6
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-attrs/cattrs
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-attrs/cattrs#761 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
python-attrs/cattrs#513 · 4 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
python-attrs/cattrs#779 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 85/100
python-attrs/cattrs#774 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
python-attrs/cattrs#746 · 2 commenti ·
Tutte le issue di python-attrs/cattrs
Issue simili
-
area: harness bug status: needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Human-Agent-Society/reef#625 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
learningequality/kolibri#15351 · 2 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
Name consistency Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
eellak/triplestore#65 · 1 commento ·