Opened 17 years ago
Closed 12 years ago
#6702 closed Cleanup/optimization (wontfix)
ModelForm: assert given instance is instance of model
Reported by: | Thomas Güttler | Owned by: | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Selwin Ong, bmispelon@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
this assert statement can help to find bugs early. Patch attached.
Attachments (1)
Change History (16)
by , 17 years ago
Attachment: | newforms_models_assert_isinstance.diff added |
---|
comment:1 by , 17 years ago
comment:2 by , 17 years ago
I don't like that asserts are removed when run with python -O
. If a model instance is really required at this point, it would IMO be better to raise an exception.
comment:3 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
An assert is fine here. Asserts are the right control structure for checking that the programmer did what they were meant to do in any case, since it's not user-supplied data (which would require a non-vanishing error). Let's remember that this is Python: ask forgiveness, not permission and all that. Paranoid pre-execution type checking isn't the norm.
comment:4 by , 16 years ago
Needs tests: | set |
---|
comment:5 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:6 by , 15 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:7 by , 14 years ago
Type: | → Cleanup/optimization |
---|
comment:8 by , 13 years ago
Severity: | → Normal |
---|
comment:11 by , 12 years ago
Cc: | added |
---|---|
Needs tests: | unset |
I opened a pull request on Github to fix this issue here: https://github.com/django/django/pull/296 . Test included.
comment:12 by , 12 years ago
Cc: | added |
---|---|
Needs documentation: | set |
Patch needs improvement: | set |
This is technically backwards-incompatible, so a mention in the release notes is warranted I think.
Also, I'd like to see some more tests involving abstract models, inherited models or proxy ones, just to be sure we're not breaking any valid use-cases.
Thanks.
comment:13 by , 12 years ago
I was the author of this ticket five years ago. Now I think different about this: if type checking at runtime is done here, then there are some hundred other places where it could be done. This would sometimes help programmers new to django and python, ..... but it would slow down the execution time and make the code less clean. I think this ticket can be closed (without a modification to django), but since there where contributions by other people, I will leave this decision to you.
comment:14 by , 12 years ago
Cc: | removed |
---|
comment:15 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Yeah, I agree this can be closed wontfix: typechecking isn't particularly Pythonic, and I can think of a few edge-cases where duck-typing a model might be useful.
BTW, all unittests pass