I am very much interested by this topic, as I
spent a couple months trying to come up with a solution with the obvious
constraint of not changing the language.
My goal was type checking
and value validation for applications wishing to get these features (so
obviously, with the possibly to not disable it at runtime even if the
rest of the application is optimized). For type checking I discovered
PEP484 and many type-checkers (enforce, pytypes...) out there, but for
value validation I found nothing really satisfying to me except many
sources saying not to use assert.
I personally align with the
idea already mentioned in this thread that assert is not a tool for
"end-user value validation", but a tool for other kind of purposes -
that are perfectly valid but are just not the same that "end-user value
validation" (if the name is not perfect, feel free to propose another).
What I think we're missing is “get consistent and customizable
validation outcome, whatever the inner validation means”.
Typically ‘assert isfinite(x)‘ is today a good example of what is wrong:
- it can be disabled globally by end-users even if the lib developer does not want it,
- if x is None the exception is different from the exception you get if x is not finite,
- you can not customize the exception type for easy error codes internationalization, you can only customize the message.
My proposal would be to rather define another statement for example