-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Default type for Field.errors
of tuple()
is immediately overwritten
#865
Comments
I generally use I think there is some value to having the default be immutable, since it indicates that Generally the data model is a bit dangerous here. since it relies on people calling things in the proper order, so although it can be a little bit annoying to use in practice, it seems preferable to me over letting people do things that are unsafe. We could potentially be more generous by detecting incorrect use in some other way and either emit an Although looking at the code there probably wasn't a big safety concern behind the default value, an empty tuple just happened to be the only safe class-scoped default value they could use. Personally I'm +0 on changing |
I'm finding the same in my code, type narrowing to tell mypy what's what.
Maybe the correct thing to do here is to modify the stubs instead? Instead of a Sequence, would a MutableSequence would be appropriate here, and thus no changes to the actual codebase? |
I did consider writing the stubs to be more permissive initially, but I ultimately decided against it, since Changing |
The initial data type for
Field.errors
is set to a tuple:wtforms/src/wtforms/fields/core.py
Line 19 in 9cc223a
which is reassigned to a
list
if there's any errors:wtforms/src/wtforms/fields/core.py
Line 215 in 9cc223a
which causes type checkers like mypy to be sad when you use the field in your own code directly.
Actual Behavior
in
repro.py
:Expected Behavior
Environment
Yes, this specific example could be solved another way with
validate_name
, but that's not the point. The point is that the error's initial datatype is incorrect.Everywhere it's used, it's treated as a list.
I'd be happy to send a PR changing to an empty list, but I didn't know if there was some other reason to have the data type as a tuple initially.
The text was updated successfully, but these errors were encountered: