python-enforce-typing
python-enforce-typing copied to clipboard
Bug: Literal[x] not supported
Right now, using Literal[x] where x is not a type raises an error:
@enforce_types
@dataclass
class Foo:
foo: Literal[True]
Foo(foo=True)
# => TypeError: isinstance() arg 2 must be a type or tuple of types
This is because Literal[x] passes the typing. _SpecialForm check, but the code then expects x to be a type. This means the following (which I don't think makes sense) passes without an error:
@enforce_types
@dataclass
class Bar:
foo: Literal[bool]
Bar(foo=True) # this should fail since `foo` isn't the type `bool`
I am having the same problem with:
@enforce_types
@dataclass
class Bar:
task: Literal["regression", "classification"]
So I saw the decorator at this point checks the following statement which is obviously wrong:
isinstance('classification', ("regression", "classification"))
Should we extend this module's functionality to include Literals correctly?