-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for string fields that inherit from Generic #2264
Comments
FYI I ultimately implemented this on @classmethod
def validate_typename(cls, value: Any, field: ModelField) -> Any:
"""Validate the typename implements the generic type."""
if field.sub_fields is None:
return value
if not isinstance(value, cls):
return value
# Get the generic type
(sub_field,) = field.sub_fields
klass = sub_field.type_
# Determine all subclasses of T that are EntityData types
if value.typename not in _get_valid_targets(klass):
raise TypeError(
f"{value.typename} does not implement {klass.__name__}"
)
return value |
Also reported as a bug in #4121 - which tbh seems like the correct characterisation (rather than a feature request) because Pydantic is effectively interpreting something like |
@dmontagu might be worth looking at this while working on genetics. |
It looks to me like this issue has its source at least partially in the fact that we don't currently support subclasses for any primitive types. It would be easy to make things fail for generic str entirely (which would be better than the current behavior of assuming you meant a Sequence), but I suspect the goal is to actually perform some validation. I'll open a PR to prevent generic subclasses of |
Checks
Feature Request
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:We have a graph of entities that are linked by ref tuples of (typename, uuid). The
Ref
has a generic type to represent the return type of the Entity. We want to validate when parsing the model that typename refers to a valid reference for this field.It's not currently possible to build a string field that inherits generic following the example in the docs, because the field value is suddenly misinterpreted as a sequence.
The inclusion of
Generic
intoTypeName
results in.The text was updated successfully, but these errors were encountered: