brettviren / moo

ruminants on module oriented programming
GNU General Public License v3.0
4 stars 4 forks source link

moo.otypes record and positional args #15

Closed brettviren closed 3 years ago

brettviren commented 3 years ago

Constructing a record with positional args fails to produce a valid object.

Eg, this should work:

import moo.otypes
moo.io.default_load_path = ("test",)
moo.otypes.load_types("issue13-schema.jsonnet");
from test.issue13 import Object, Name, Count, Counts, CountsObject
o1 = Object(Name("rname"), Count(1))
print(o1.pod())

Instead it gives

Traceback (most recent call last):
  File "junk.py", line 6, in <module>
    print(o1.pod())
  File "/home/bv/dev/moo/moo/otypes.py", line 116, in pod
    raise AttributeError("%s missing required field %s" %
AttributeError: Object missing required field rname

Further, literal Python values should be acceptable as positional args

import moo.otypes
moo.io.default_load_path = ("test",)
moo.otypes.load_types("issue13-schema.jsonnet");
from test.issue13 import Object, Name, Count, Counts, CountsObject
o1 = Object("rname", 1)
print(o1.pod())

instead of giving

Traceback (most recent call last):
  File "junk.py", line 5, in <module>
    o1 = Object("rname", 1)
  File "<record Object>", line 16, in __init__
  File "/home/bv/dev/moo/moo/otypes.py", line 135, in update
    self._from_string(arg)
  File "/home/bv/dev/moo/moo/otypes.py", line 151, in _from_string
    raise ValueError("attempt to set record %s with garbage string" %
ValueError: attempt to set record Object with garbage string
brettviren commented 3 years ago

This is actually "wontfix". I've forgotten that the positional args are used for "bulk" setting based on a single value. Eg a record may be set by a JSON string or Python dict representing an object.

This mimics dict() constructor semantics.