bloomberg / attrs-strict

Provides runtime validation of attributes specified in Python 'attr'-based data classes.
Apache License 2.0
52 stars 19 forks source link

typing.Literal Support #59

Closed lironhl closed 4 years ago

lironhl commented 4 years ago

Signed-off-by: lironhl liron.lavy@gmail.com

closes #58

Describe your changes I introduced a way to check check attributes with the type typing.Literal (The check supports python 2 and python 3). I also created an error object LiteralError for the cases when the type check fails. Note that I also made changes to _validate_elements function, because having type.Literal, inside isinstance function, yields the error: TypeError: typing.Literal cannot be used with isinstance().

Testing performed I copied the test structure of other features in the project, the tests of typing.Literal test it as the single type of an attribute, and test it as a type inside a "container" type, for example as the type typing.List[typing.Literal['a', 'b', 'c']].

erikseulean commented 4 years ago

@lironhl do you still think this has value ?