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

Unexpected behavior when validate typing.Tuple[str, int] #138

Closed
xbanke opened this issue Dec 5, 2020 · 1 comment
Closed

Unexpected behavior when validate typing.Tuple[str, int] #138

xbanke opened this issue Dec 5, 2020 · 1 comment
Assignees
Labels
bug Something isn't working
Projects

Comments

@xbanke
Copy link

xbanke commented Dec 5, 2020

  • typical version: 2.0.33
  • Python version: 3.7
  • Operating System: CentOS

Description

Hey, @seandstewart
As title said, when transmute a tuple type with typed elements.

import typing, typic


@typic.al
def foo(a: typing.Tuple[str, int]):
    print(a)

    
foo(('abc', 123))  # ('abc', '123')

The second element type int is transmuted to str type.

@seandstewart seandstewart added the bug Something isn't working label Dec 8, 2020
@seandstewart seandstewart added this to To do in v2.1 via automation Dec 8, 2020
@seandstewart
Copy link
Owner

seandstewart commented Dec 8, 2020

Thanks for submitting. Looks like there a few things not quite right with our tuple implementation.

Tuple validation doesn't respect argument order either:

import typic
from typing import Tuple

proto = typic.protocol(Tuple[str, int])
proto.transmute((1, 1))  # ('1', '1')  <-- should be ('1', 1)
proto.validate(("foo", 1))  # ("foo", 1)  <-- okay
proto.validate((1, "foo"))  # (1, 'foo') <-- should raise a validation error
proto.validate((1, "foo", "bar"))  # (1, 'foo', 'bar') <-- should raise a validation error

I think this is inherent in the implementation, which treats tuple like just another iterable, when in fact the shape of a tuple is quite meaningful. If the tuple is unbounded in size (e.g., notated with an ellipsis), then yes, that is correct, but if the tuple is constrained in size, we should treat it more like we treat a class, TypedDict, or NamedTuple.

@seandstewart seandstewart self-assigned this Jan 5, 2021
@seandstewart seandstewart moved this from To do to Review in progress in v2.1 Jan 5, 2021
@seandstewart seandstewart mentioned this issue Jan 5, 2021
v2.1 automation moved this from Review in progress to Done Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
v2.1
  
Done
Development

No branches or pull requests

2 participants