From 0826dabf26d6a7e5df6e3186d8e32647184ebd90 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 15 Dec 2023 08:26:01 +0200 Subject: [PATCH] Recast the `PYDANTIC_ERRORS_INCLUDE_URL` environment variable and document it Refs https://github.com/pydantic/pydantic-core/pull/1118#issuecomment-1854040572 --- python/pydantic_core/_pydantic_core.pyi | 12 ++++++++++++ src/errors/validation_exception.rs | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/pydantic_core/_pydantic_core.pyi b/python/pydantic_core/_pydantic_core.pyi index 382a6c804..b8c1f4e94 100644 --- a/python/pydantic_core/_pydantic_core.pyi +++ b/python/pydantic_core/_pydantic_core.pyi @@ -787,6 +787,18 @@ class ValidationError(ValueError): a JSON string. """ + def __repr__(self) -> str: + """ + A string representation of the validation error. + + Whether or not documentation URLs are included in the repr is controlled by the + environment variable `PYDANTIC_ERRORS_INCLUDE_URL` being set to `1` or + `true`; by default, URLs are shown. + + Due to implementation details, this environment variable can only be set once, + before the first validation error is created. + """ + @final class PydanticCustomError(ValueError): def __new__( diff --git a/src/errors/validation_exception.rs b/src/errors/validation_exception.rs index 91ef60a0d..31991f5e7 100644 --- a/src/errors/validation_exception.rs +++ b/src/errors/validation_exception.rs @@ -194,8 +194,8 @@ impl ValidationError { static URL_ENV_VAR: GILOnceCell = GILOnceCell::new(); fn _get_include_url_env() -> bool { - match std::env::var("PYDANTIC_ERRORS_OMIT_URL") { - Ok(val) => val.is_empty(), + match std::env::var("PYDANTIC_ERRORS_INCLUDE_URL") { + Ok(val) => val == "1" || val.to_lowercase() == "true", Err(_) => true, } }