0 users browsing Discussion. | 1 guest | 41 bots  
    Main » Discussion » YAML files: Statically typed, confusing if poorly formatted
    Pages: 1
    Posted on 19-03-01, 10:17
    Post: #26 of 77
    Since: 10-31-18

    Last post: 1189 days
    Last view: 1116 days
    (branching off https://helmet.kafuka.org/bboard/thread.php?pid=1719#1719 )

    https://github.com/ambv/black places trailing commas after each line in a function call or list literal, to explicitly make swapping/commenting elements easy.

    To the contrary, I noticed that ruamel.yaml's YAML formatting makes it hard to comment-out or reorder the first line:

    State 1:

    orders:
    - name: Apples
    price: 1.5
    - name: Onions
    price: 2
    "2d_identity_matrix": # quotes optional
    - - 1

    Diff 2: Newline before [maps within lists]. (Neither PyCharm nor ruamel.yaml does this)

    orders:
    -
    name: Apples
    price: 1.5
    -
    name: Onions
    price: 2
    "2d_identity_matrix":
    -
    - 1

    Diff 3: indenting lists within maps. (PyCharm does this, ruamel.yaml does not)

    orders:
    -
    name: Apples
    price: 1.5
    -
    name: Onions
    price: 2
    "2d_identity_matrix":
    -
    - 1

    - All the above parse to the same tree... I think YAML is too permissive.
    - There is no "standard" formatting style. I think State 1 confuses people unfamiliar with YAML (like myself at first).
    - ruamel.yaml uses State 1.
    - The YAML 1.2 spec uses Option 3, which I like. https://yaml.org/spec/1.2/spec.html#id2760193
    - Pycharm does 3 but not 2 (but it's completely configurable).
    - I dislike the first 2 are dumb because you have a Dict[List[Dict]] but the innermost dict is only indented 1 level.
    --------------------
    One thing I like about YAML is (unlike JSON) you can cleanly represent strongly-typed objects (call it tagged unions, or subclass polymorphism, whatever you prefer):

    !MyAppConfig
    output: !FFmpegOutputConfig
    foo: bar

    I use this style in my project https://github.com/jimbo1qaz/corrscope . In Python, I store all config as statically-type-hinted attrs/dataclasses. I use ruamel.yaml to serialize the config to YAML files, and expose the names of the config classes to the YAML dump.
    My app auto-generates YAML files which are subsequently hand-edited (primarily GUI-edited, ever since I added one). I believe that !Tagged YAML files are more self-documenting and easier for *me* to cross-reference between YAML and source code (when I use my own program).
    But it turns out that ever since moving "ffmpeg vs ffplay" to a runtime flag, I only every dump 1 class to any given parameter, and currently have no uses for polymorphism when loading (other than generating type errors at runtime)... But I still feel it's good to have (I have no issues with it, and don't want to rewrite my working code).
    -------
    I think https://github.com/ron-rs/ron has a similar "typed/tagged object" feature.
    Sadly I feel this "killer feature" of YAML is underutilized.
    - https://github.com/dtolnay/serde-yaml/issues/115
    Rust Serde added support for *reading* YAML tags. But it *writes* them the "dumb" way, abusing a 1-element map to act as a type tag:

    time: TimeOfDay: asdf
    # is equal to
    time:
    TimeOfDay: asdf

    I can't imagine why. Rust is a type-heavy language with tagged unions as a central concept.
    Appveyor also abuses a 1-element map as a type tag:

    build_script:
    - exit # run everywhere
    - cmd: exit # cmd batch
    - sh: exit # bash
    - ps: exit # powershell

    I could imagine people who only use dynamically typed languages would prefer the above to:

    build_script:
    - exit # run everywhere
    - !cmd exit # cmd batch
    - !sh exit # bash
    - !ps exit # powershell
    Pages: 1
      Main » Discussion » YAML files: Statically typed, confusing if poorly formatted
      Yes, it's an ad.