Skip to content

fix: coerce env var strings to schema-declared boolean/integer types#482

Open
gaoflow wants to merge 1 commit into
liiight:mainfrom
gaoflow:fix/environ-type-coercion
Open

fix: coerce env var strings to schema-declared boolean/integer types#482
gaoflow wants to merge 1 commit into
liiight:mainfrom
gaoflow:fix/environ-type-coercion

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 25, 2026

Copy link
Copy Markdown

Problem

Environment variables are always strings, but provider schemas declare fields with "type": "boolean" or "type": "integer". When these settings are supplied through environment variables (e.g. NOTIFIERS_EMAIL_TLS=true), they are passed as raw strings to jsonschema validation, which raises BadArguments:

notifiers.exceptions.BadArguments: Error with sent data: 'true' is not of type 'boolean'

This has been reported in issue #387 (open since 2020).

Fix

Add _coerce_environs() to SchemaResource, called at the end of _get_environs(). It inspects each returned key against the provider's JSON schema and casts:

  • "boolean" → uses the existing text_to_bool() helper ("true" / "1" / "yes" / "on"True, etc.)
  • "integer"int()
  • "number"float()
  • Everything else → unchanged string

The fix is ~30 lines, all pure Python, and places the responsibility where the schema context is already available.

Before / After

# Before — BadArguments raised
import os, notifiers
os.environ["NOTIFIERS_EMAIL_TLS"] = "true"
notifiers.get_notifier("email").notify(message="hi", ...)
# notifiers.exceptions.BadArguments: 'true' is not of type 'boolean'

# After — works correctly
result = notifiers.get_notifier("email")._get_environs("NOTIFIERS_")
assert result["tls"] is True
assert result["port"] == 587  # integer coercion also works

Tests

A new test_environ_bool_and_int_coercion test in tests/test_core.py covers "true"True, "false"False, and "587"587. All 34 tests pass.


This pull request was prepared with the assistance of AI, under my direction and review.

Environment variables are always strings, but providers declare schema
properties with type "boolean" or "integer". Reading these settings via
NOTIFIERS_<PROVIDER>_<ARG> raised BadArguments ("'true' is not of type
'boolean'") because the raw string was passed straight to jsonschema.

Add `_coerce_environs()` to `SchemaResource`, called by `_get_environs()`,
which inspects the provider's JSON schema for each returned key and casts:
- "boolean" → uses the existing `text_to_bool()` helper ("true"/"1"/"yes" → True)
- "integer"  → int()
- "number"   → float()
- everything else → unchanged string

Fixes liiight#387.
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

Successfully merging this pull request may close these issues.

1 participant