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

Bug: Literal[x] not supported

Open cunningjames opened this issue 4 years ago • 1 comments

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`

cunningjames avatar Jan 04 '21 17:01 cunningjames