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

Specify fixed number of elements in the tuple #221

Closed
ghost opened this issue May 16, 2016 · 5 comments
Closed

Specify fixed number of elements in the tuple #221

ghost opened this issue May 16, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented May 16, 2016

Greetings! Currently I am moving cryptographic library from Python2/3 to mypy. I have got so-called S-box-es: tuple consisting of 8 tuples with 16 integers inside. Like this:

sbox = (
    (4, 2, 15, 5, 9, 1, 0, 8, 14, 3, 11, 12, 13, 7, 10, 6),
    (12, 9, 15, 14, 8, 1, 3, 10, 2, 7, 4, 13, 6, 0, 11, 5),
    (13, 8, 14, 12, 7, 3, 9, 10, 1, 5, 2, 4, 6, 15, 0, 11),
    (14, 9, 11, 2, 5, 15, 7, 1, 0, 13, 12, 6, 10, 4, 3, 8),
    (3, 14, 5, 9, 6, 8, 0, 13, 10, 11, 7, 12, 2, 1, 15, 4),
    (8, 15, 6, 11, 1, 9, 12, 5, 13, 3, 7, 10, 0, 14, 2, 4),
    (9, 11, 12, 0, 3, 6, 7, 5, 4, 8, 14, 15, 1, 10, 2, 13),
    (12, 6, 5, 2, 11, 0, 9, 13, 3, 14, 7, 10, 15, 4, 1, 8),
)

And I have got function requiring exactly that structure as an argument. Currently I wrote the following SboxType:

SboxType = Tuple[
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
    Tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int],
]

def encrypt(sbox: SboxType, data: bytes) -> bytes: ...

Possibly I am misunderstanding something. Is there any better and more beauty way on writing types for that kind of structures?

Similar problem arises when I want to specify bytes string (type: bytes) exactly 8 bytes long.

@JukkaL
Copy link
Contributor

JukkaL commented May 16, 2016

There's no better way to specify a constant-length tuple. I'd recommend considering Tuple[int, ...] (literal ...) for a variable-length tuple, though, as the extra type checking you might get from having a constant-length tuple might not be worth the awkward type.

There is no way to specify the length of a bytes object (or a list, for that matter).

@ghost
Copy link
Author

ghost commented May 17, 2016

I see. Thanks for clarification!

@ghost ghost closed this as completed May 17, 2016
@aldanor
Copy link

aldanor commented Aug 13, 2016

@stargrave A bit late to this issue, but you can always do this:

>>> Tuple[(Tuple[(int,) * 8],) * 4]
typing.Tuple[
    typing.Tuple[int, int, int, int, int, int, int, int], 
    typing.Tuple[int, int, int, int, int, int, int, int], 
    typing.Tuple[int, int, int, int, int, int, int, int], 
    typing.Tuple[int, int, int, int, int, int, int, int]
]

@gvanrossum
Copy link
Member

gvanrossum commented Aug 13, 2016 via email

@aldanor
Copy link

aldanor commented Aug 13, 2016

Ah, apologies then, it works with typing but didn't realise mypy would choke on that.

This issue was closed.
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