typed containers
See original GitHub issueNot sure if this should be classified as a bug or as a feature request.
- OS: Linux
- Python version: 3.7.3
- Pydantic version: 0.24
from typing import Dict, List
from pydantic import BaseModel, ValidationError
class C(BaseModel):
class Config:
validate_assignment = True
f1: List[Dict[int, str]] = []
try:
# for some reason mypy doesn't catch this one
o = C(f1=[1])
print("FAIL")
except ValidationError:
print("PASS")
o = C()
try:
# mypy: List item 0 has incompatible type "int"; expected "Dict[int, str]"
o.f1 = [1]
print("FAIL")
except ValidationError:
print("PASS")
try:
# mypy: Argument 1 to "append" of "list" has incompatible type "int"; expected "Dict[int, str]"
o.f1.append(1)
print("FAIL")
except ValidationError:
print("PASS")
try:
# mypy: Dict entry 0 has incompatible type "str": "int"; expected "int": "str"
o.f1.append({"": 1})
print("FAIL")
except ValidationError:
print("PASS")
o.f1.append({})
try:
# mypy: Incompatible types in assignment (expression has type "int", target has type "str")
o.f1[-1]["x"] = 2
print("FAIL")
except ValidationError:
print("PASS")
results in:
PASS
PASS
FAIL
FAIL
FAIL
types <class 'list'> <class 'dict'>
I am well aware of all the philosophical aspects of Python’s typing, but in my use case, where user uses Python to gradually build complex configuration model (including incremental adding of items to containers), it would be a much more productive workflow to be able to fail immediately at the location of the offending model modification rather than do a model validation afterwards, making user wonder “hey, where in my hundreds of lines of model I have done this wrong assignment/modification”. It is my understanding, that to be able to achieve this, it would be necessary for the fields of container types to be of custom container types performing all the necessary validations on all operations but from my experience, this is perfectly feasible.
P.S. The mypy has been executed as:
mypy --python-executable venv-pydantic/bin/python --strict pydantic1.py
but this issue is not about mypy.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:19 (7 by maintainers)
We’re not talking about lots of people, so probably easier via comments with people saying they want it.
If 5 separate people say they’re interested in the feature I’ll accept a PR.
I think that seems fair?
Having this feature baked into pydantic would be useful. I am currently working on a typed container for my specific use case. Here is what I have so far:
It makes use of
__orig_bases__
from PEP 560, so this works for Python 3.7+. The way it currently runs validation and raises the ValidationError is hacky and fragile. It also requires a subclass of TypedList bound to the specific type of the container before use in a pydantic model.Example usage of this container:
I currently do not have much knowledge of the internals of pydantic, and I am certain that this only works for my use case with the type of validators I am using. Any suggestions on how this can be improved upon, or concerns with the approach that I might not be aware of?