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 provide default value to StrictBool? #690

Closed
pykong opened this issue Jul 25, 2019 · 3 comments · Fixed by #873
Closed

How to provide default value to StrictBool? #690

pykong opened this issue Jul 25, 2019 · 3 comments · Fixed by #873
Labels

Comments

@pykong
Copy link

@pykong pykong commented Jul 25, 2019

How can we provide a default value to a variable with type StrictBool in a way that is accepted by MyPy?

from pydantic import BaseModel

class Flags(BaseModel):
    pretend: StrictBool = False  # MyPy does not like this
@dmontagu

This comment has been minimized.

Copy link
Collaborator

@dmontagu dmontagu commented Jul 26, 2019

Booleans are hard to deal with in a type-hinting context because python explicitly disallows subclassing bool. I might be missing something, but I suspect there isn't a way that you can assign a default value without having to fight mypy at least a little.

The obvious choice would be to just use

class Flags(BaseModel):
    pretend: StrictBool = False  # type: ignore

but that may cause type checking issues since StrictBool is (by necessity) a subclass of int, rather than bool. While this involves "turning off" mypy (which I would agree should generally be avoided), I think this use is small in enough in scope to merit the convenience.

Another option is to put the StrictBool import in an if TYPE_CHECKING: block, having it serve as an alias for bool when type checking:

from typing import TYPE_CHECKING

from pydantic import BaseModel

if TYPE_CHECKING:
    StrictBool = bool
else:
    from pydantic import StrictBool

class Flags(BaseModel):
    pretend: StrictBool = False

This should behave properly with type checking, but is a little uglier everywhere you import it.

@samuelcolvin Do you think it could make sense to just put the above alias for StrictBool (and RelaxedBool if/when merged) directly into pydantic? Or perhaps into a types.pyi or similar? Any obvious downsides to that?

@samuelcolvin

This comment has been minimized.

Copy link
Owner

@samuelcolvin samuelcolvin commented Jul 26, 2019

@samuelcolvin Do you think it could make sense to just put the above alias for StrictBool (and RelaxedBool if/when merged) directly into pydantic? Or perhaps into a types.pyi or similar? Any obvious downsides to that?

Makes sense.

Although my instinct is that the need for StrictBool will reduce a lot when #617 gets deployed.

@pykong

This comment has been minimized.

Copy link
Author

@pykong pykong commented Jul 28, 2019

Thanks for your responses! IMHO a stricter bool validation as implemented in #617 would be the cleanest solution. Until it is merged and released I can fall back on suppressing MyPy warnings of bool and Strictbool type mismatch.

@dmontagu dmontagu mentioned this issue Oct 7, 2019
4 of 4 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.