python-enforce-typing icon indicating copy to clipboard operation
python-enforce-typing copied to clipboard

Supporting nested types (collections)

Open cunningjames opened this issue 4 years ago • 0 comments

Your README gives the impression that you support collection types like Dict[str, Tuple[int, int]]. That's arguably a bit misleading, since only the outermost type is enforced. E.g., the following works fine:

@enforce_types
@dataclass
class Foo:
    foo: Dict[str, Tuple[int, int]]
    bar: List[str]

Foo(foo={1: "henlo fren"}, bar=[1, 2, 3])

Improving support here would be possible, but would require walking through and checking the type of every item in the collection. I suppose you could do a half-measure like check the first n items, e.g.

@enforce_types(n=5)
@dataclass
class Bar:
    foo: List[int]

Bar(foo=[1, 2, 3, 4, 5])
# But also:
Bar(foo=[1, 2, 3, 4, 5, "6"])

Or even a random sample.

cunningjames avatar Jan 04 '21 16:01 cunningjames