Conversation
I've upgraded this to a property, and added basic validation to check the dictionary keys.
The `constraints` property is now a typed dictionary. Assigning dictionary literals to it still ought to work, but it should flag type errors if the keys are incorrect. The method `BaseProperty._validate_constraints` is provided to convert untyped dictionaries with appropriate validation. This now uses `pydantic` to validate the typeddict. It is stricter than what I did before, as it also checks the type of the keys, not just their names. Pydantic was less ugly than coming up with my own logic to coerce an untyped dictionary into a typeddict. I've added a unit test on validation to check it does what I expect. It would be lovely to deduplicate the typeddict and the constant with key names in it - but this is hard to do neatly.
While typing has a TypedDict class from Python 3.8, pydantic requires the use of typing_extensions prior to 3.12.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds better validation and typing to
BaseProperty.constraints. This should be particularly helpful for functional properties, where it's likely that this property will be accessed directly, rather than through the constructor.I've changed the type of
BaseProperty.constraintsto be aTypedDict, because it should retain compatibility with existing code that assigns a (valid)dictliteral while also offering type checking and autocompletion in type-aware IDEs. The first commit of this PR implements basic validation without changing any types, the next two swap toTypedDictand usepydanticto validate the dictionary at runtime. This is primarily because converting an untyped dict into a typed one is really ugly, so I'd rather usepydanticthan implement my own converter. Switching validation topydanticalso means we now check the types of the constraints, which is a nice bonus.Closes #275