Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get errors on Literal with typechecked annotated class? #160

Closed
SamuelMarks opened this issue Oct 25, 2020 · 4 comments
Closed

How to get errors on Literal with typechecked annotated class? #160

SamuelMarks opened this issue Oct 25, 2020 · 4 comments

Comments

@SamuelMarks
Copy link

from typing import Literal
from dataclasses import dataclass

from typeguard import typechecked


@dataclass
@typechecked
class A(object):
    optimizer: Literal[
        "Adadelta", "Adagrad", "Adam", "Adamax", "Ftrl", "Nadam", "RMSprop"
    ] = None

print(A(optimizer='Foo'), A(optimizer=None), A(optimizer=True), A(optimizer=5))

Output:

(A(optimizer='Foo'),
 A(optimizer=None),
 A(optimizer=True),
 A(optimizer=5),
 A(optimizer=0.2))

Expectation:
Error on all of these. This should work though A(optimizer='Adagrad')

@SamuelMarks
Copy link
Author

@agronholm In the meantime, I've started maintaining the old enforce repo, and added Literal support with: SamuelMarks/enforce@349e6f8

@svermeulen
Copy link

I don't think @typechecked can be applied to dataclass types as discussed in #161 and #61

@SamuelMarks
Copy link
Author

@svermeulen Might give your gist a shot https://gist.github.com/svermeulen/8a6ad727113a6d07f78332152c1d33b2

I suppose avoiding maintaining my own runtime type checker is a good idea…

@agronholm
Copy link
Owner

As stated above, covered by #161 so I'm closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants