Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneOf validator fails when given a set of byte characters #1070

Open
nan-af opened this issue Feb 22, 2024 · 5 comments
Open

OneOf validator fails when given a set of byte characters #1070

nan-af opened this issue Feb 22, 2024 · 5 comments

Comments

@nan-af
Copy link

nan-af commented Feb 22, 2024

In [2]: from construct import *

In [3]: criteria = OneOf(Byte, {b'h', b'i'})

In [4]: criteria.parse(b'h')
ValidationError: Error in path (parsing)
object failed validation: 104


In [5]: criteria.parse(b'i')
ValidationError: Error in path (parsing)
object failed validation: 105


In [6]: criteria = OneOf(Byte, {ord('h'), ord('i')})

In [7]: criteria.parse(b'h')
Out[7]: 104

In [8]: criteria.parse(b'i')
Out[8]: 105

I believe we should get identical behaviour for b'h' and ord('h'). I have observed this behaviour when running construct 2.10.68 on python 3.10.12.

@franzhaas
Copy link

I think this is correct

https://construct.readthedocs.io/en/latest/advanced.html

Byte is a 8bit integer, not a single element byte array.

What you are looking for is probably Bytes(1)

from construct import *
criteria = OneOf(Bytes(1), {b'h', b'i'})
criteria.parse(b'h')

@nan-af
Copy link
Author

nan-af commented Mar 15, 2024

🤔
It's confusing but okay

@franzhaas
Copy link

Took me a while to get used to as well... but actually it is a beautiful solution, as all arrays (1-N elements) behave similarly, and all scalars do, there is no "Schroedingers" datatype which might be a single element array or a scalar.

If that's ok for you would you mind to close the ticket?

@nan-af
Copy link
Author

nan-af commented May 2, 2024

Hello, my apologies for the delay.

I was actually hoping to see what the author has to say about it :)

@arekbulski
Copy link
Member

I am too confused to give you a meaningful response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants