question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapPattern with Int() key not working with compound value

See original GitHub issue

I’m trying to write a schema using integer keys for a MapPattern, but am running into a snag:

from strictyaml import MapPattern, Int, Str, load, Any

yaml = """
1:
- ABC
2:
- DEF
- GHI
"""

schema1 = MapPattern(Str(), Any())
schema2 = MapPattern(Int(), Any())

print(load(yaml).data)
print(load(yaml, schema1).data)
print(load(yaml, schema2).data)  # fails

Result:

OrderedDict([('1', ['ABC']), ('2', ['DEF', 'GHI'])])
OrderedDict([('1', ['ABC']), ('2', ['DEF', 'GHI'])])
Traceback (most recent call last):
  File "<untitled>", line 16, in <module>
  File ".../Python3.7/strictyaml/parser.py", line 318, in load
  File ".../Python3.7/strictyaml/parser.py", line 296, in generic_load
  File ".../Python3.7/strictyaml/validators.py", line 17, in __call__
  File ".../Python3.7/strictyaml/compound.py", line 75, in validate
  File ".../Python3.7/strictyaml/validators.py", line 17, in __call__
  File ".../Python3.7/strictyaml/any_validator.py", line 52, in validate
  File ".../Python3.7/strictyaml/validators.py", line 17, in __call__
  File ".../Python3.7/strictyaml/compound.py", line 257, in validate
  File ".../Python3.7/strictyaml/yamllocation.py", line 54, in process
  File ".../Python3.7/strictyaml/yamlpointer.py", line 179, in get
  File ".../Python3.7/ruamel/yaml/comments.py", line 753, in __getitem__
KeyError: '1'

Am I doing something wrong or are Int() keys not supported?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
crdoconnorcommented, May 3, 2020

No, that looks like a bug, and quite an embarrassing one too 😦

I’m going to prioritize fixing this today.

1reaction
crdoconnorcommented, May 4, 2020

Hi, it’s proving to be a little trickier than I thought to do this but I’m still working on it. Thanks for your patience.

On Sat, 2 May 2020, 15:03 Just van Rossum, notifications@github.com wrote:

I’m trying to write a schema using integer keys for a MapPattern, but am running into a snag:

from strictyaml import MapPattern, Int, Str, load, Any

yaml = “”“1:- ABC2:- DEF- GHI”“”

schema1 = MapPattern(Str(), Any()) schema2 = MapPattern(Int(), Any()) print(load(yaml).data)print(load(yaml, schema1).data)print(load(yaml, schema2).data) # fails

Result:

KeyError: ‘1’

Am I doing something wrong or are Int() keys not supported?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/102, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABOJKNPIQQSCC7HDXT23LVLRPQRZVANCNFSM4MXWH3LA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why integer key do not work in HashMap? - Stack Overflow
It's because you're not passing a Long type of key when retrieving the value, but an integer instead. These get autoboxed as different ......
Read more >
Mappings with defined keys (Map) - HitchDev
Mappings of one value to another are represented by : in YAML and parsed as python dicts. Using StrictYAML's 'Map' you can validate...
Read more >
Restrictions on Dictionary Keys and Values - Real Python
Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn't make sense to map a particular...
Read more >
crdoconnor/strictyaml: Type-safe YAML parser and validator.
StrictYAML is a type-safe YAML parser that parses and validates a restricted subset of the YAML specification. Priorities: Beautiful API; Refusing to parse...
Read more >
20. Dictionaries — How to Think Like a Computer Scientist
Dictionaries are yet another kind of compound type. ... The values in a dictionary are accessed with keys, not with indices, so there...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found