5 ms·
The "encoding" argument seems like the perfect use case for a strict type, ideally something that works like a sum type, or union types in TS. I think the diffe
by Zababa 5y ago
The "encoding" argument seems like the perfect use case for a strict type, ideally something that works like a sum type, or union types in TS. I think the difference is that in Python unions are unions of types, and not of values. I think "typing.Literal" would be what you can use here in Python.
- joshuamorton 5y agoDepending on the approach that can work, although there are cases where trying to making encoding a literal would be too restrictive (imagine a system that dynamically loads parser plugins, which isn't as uncommon as it sounds), so a literal restricted to ascii and utf-8 would prevent someone from parsing latin-1 or idk, protobuf bytes.
- Zababa 5y agoThat's a good point. I can see it working with static typing by being more liberal in what you accept as enconding, and accepting a validation function that would take the type of the encoding (for example string) and returning a boolean. But you're right, the type itself of the encoding will have to be wider.