Question Body
I'm currently developing a Python coding standard for use in enterprise-level internal systems. Our typical use cases include:
- API input/output validation
- Reading from databases, transforming into JSON, and returning results
- Validating and transforming user-provided JSON/dict data
In this context, I'm questioning whether static type hints (with tools like mypy or pyright) are truly effective compared to a runtime validation approach using JSON Schema.
Problem Context
In practice, static type hints work well for primitive types (int, str, list),
but they fall short for complex or deeply nested dict/JSON structures,
which often end up being typed as Any, rendering type hints meaningless.
For example:
def handle_user(user: dict): # Type hint exists, but internal structure is unchecked
return user["profile"]["address"]["zipcode"]
Most actual runtime errors come from:
- Missing keys (
KeyError) - Wrong structure (
TypeError) - Invalid values or out-of-range inputs (
ValueError)
These cannot be prevented by static typing alone. In the end, we still need runtime testing or validation logic.
Advantages of JSON Schema Approach
- Clear and declarative schema definitions (structure, required fields, value ranges, etc.)
- Easy to validate with
jsonschema.validate()or similar tools - Supports complex, nested structures
- Schemas can be reused for API documentation and test automation
Example:
from jsonschema import validate
user_schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 18}
},
"required": ["name", "age"]
}
validate({"name": "Alice", "age": 22}, user_schema)
Key Questions
- How useful is static type checking (e.g., via
mypy,pyright) in real-world enterprise development? - For complex and dynamic
dict-based structures, isn't runtime validation via JSON Schema more effective? - Are there known real-world examples (companies, frameworks) that adopt a runtime-first validation strategy using JSON Schema?
- Is reducing static type hints in favor of explicit runtime validation considered unorthodox or discouraged in Python best practices?
Summary
Static typing may prevent basic type errors, but most real-world bugs stem from runtime structural problems. So we are asking:
Is it more practical in production code to rely primarily on runtime validation using JSON Schema, while keeping static type hints to a minimum?
Would love to hear community perspectives or counterexamples.