Default type for `Field.errors` of `tuple()` is immediately overwritten
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
Research direction
Start in src/wtforms/fields/core.py at the initialization around line 19 and reassignment around line 215, then inspect how Field.errors is used throughout validation. Reproduce the mypy example from the issue and verify that errors is consistently list-compatible without changing existing validation behavior.
Written by the indexing model from the issue text.
Description
The initial data type for Field.errors is set to a tuple:
https://github.com/pallets-eco/wtforms/blob/9cc223ad2fabeabd8d89021ea962b106d1502b6c/src/wtforms/fields/core.py#L19
which is reassigned to a list if there's any errors:
https://github.com/pallets-eco/wtforms/blob/9cc223ad2fabeabd8d89021ea962b106d1502b6c/src/wtforms/fields/core.py#L215
which causes type checkers like mypy to be sad when you use the field in your own code directly.
Actual Behavior
in repro.py:
import wtforms
class MyForm(wtforms.Form):
name = wtforms.StringField()
def validate(self, extra_validators=None, *args, **kwargs):
super().validate(extra_validators, *args, **kwargs)
if self.name.data == 'repro':
# note the use of `append()`, unavailable on a tuple.
self.name.errors.append('Name cannot be "repro"')
$ mypy --check-untyped-defs repro.py
repro.py:10: error: "Sequence[str]" has no attribute "append" [attr-defined]
Found 1 error in 1 file (checked 1 source file)
Expected Behavior
$ mypy --check-untyped-defs repro.py
Success: no issues found in 1 source file
Environment
- Python version: 3.12
- wtforms version: 3.2.1
- mypy version: 1.12.1
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.
- Dominant language
- Python
- Stars
- 1.6k
- Forks
- 409
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pallets-eco/wtforms
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
pallets-eco/wtforms#928 · 2 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
pallets-eco/wtforms#922 · 14 comments · 6 reactions ·
-
breaking change refactoring
Difficulty 5/5 Over a week Newbie friendliness 30/100
pallets-eco/wtforms#911 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
pallets-eco/wtforms#842 · 1 comment ·
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
pallets-eco/wtforms#839 · 5 reactions ·
All issues in pallets-eco/wtforms
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100