Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Nullable is always Falsey #161

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dataclasses_jsonschema/type_defs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Union, Dict, Any, List, TypeVar
from typing import Union, Dict, Any, List, TypeVar, Literal

try:
# Supported in future python versions
Expand Down Expand Up @@ -47,7 +47,8 @@ class _NULL_TYPE:
"""Sentinel value to represent null json values for nullable fields, to distinguish them from `None`
for omitted fields.
"""
pass
def __bool__(self) -> Literal[False]:
return False


NULL = _NULL_TYPE()
Expand Down
20 changes: 20 additions & 0 deletions tests/nullable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
This script is used to test if mypy understands that the Nullable type is always False-y.
"""

from __future__ import annotations
from dataclasses import dataclass

from dataclasses_jsonschema.type_defs import Nullable


@dataclass
class Example:
name: Nullable[str | None] = None


example = Example("sienna")

assert example.name # If this assert passes we know `name` is a string (because it isn't None or Nullable)

name_upper = example.name.upper()