matchawine / python-enforce-typing

An easy to use decorator to enforce static typing for function and dataclasses.
GNU General Public License v3.0
43 stars 6 forks source link

Bug: Literal[x] not supported #6

Open cunningjames opened 3 years ago

cunningjames commented 3 years ago

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`
data-stepper commented 2 years ago

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?