#19777 closed Bug (wontfix)

Casting a SimpleLazyObject to an int fails

Reported by: mattrobenolt Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords: functional simplelazyobject
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Should be pretty self explanatory. We noticed the bug when passing request.user into a filter like:


Since Django is attempting to coerce the object to an int, it fails on the SimpleLazyObject yielding:

TypeError: int() argument must be a string or a number, not 'SimpleLazyObject'

The error can be reproduced simple by doing:

class Foo(object):
  __int__ = lambda self: 1


See for a patch.

Attachments (0)

Change History (1)

comment:1 Changed 17 months ago by jacob

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

Closing after IRC discussion:

jacobkm: mattrobenolt: I'm a bit confused: why are you passing a user into something that expects an int?
jacobkm: I don't think it really has anything to do with the lazy object; if you did that with a non-lazy object you'd just get something like " int() argument must be a string or a number, not 'User'"
mattrobenolt: jacobkm: So we've been staring at this.
mattrobenolt: There's a bug in our code which causes us to pass in an AnonymousUser to our filter.
mattrobenolt: Which uncovered the SimpleLazyObject bug.
mattrobenolt: Inside Django's RelatedField._pk_trace, it's looking for a model's pk to get around the coersion.
mattrobenolt: If it's an Anonymous user, there is none of that, so it fails ugly.
mattrobenolt: So I guess we've uncovered a couple issues. 1) A bug in our code.
mattrobenolt: 2) A bug in coercing any SimpleLazyObject to an int with __int__.
mattrobenolt: 3) Should passing an AnonymousUser to a filter yield a better error?
jacobkm: I'd say no to 3 since that would basically require special-casing AnonymousUser somewhere nasty.
mattrobenolt: I agree on that.
jacobkm: I'm trying to think through why you'd need __int__ on the lazy object. It's not really a public API, it's just used within Django in a few places, and we never need it in an int context.
jacobkm: So I'm inclined to say "no"
mattrobenolt: Yeah, after uncovering our bug, it wouldn't have solved it since it's still a bug on our end.
mattrobenolt: Err, that didn't make much sense, but yeah.
mattrobenolt: I'm trying to think of a better way to solve this. At least in terms of a better error message.
mattrobenolt: Because this was terrible to track down considering we pinned it on that. :)
fhahn left the chat room. (Ping timeout: 248 seconds)
jacobkm: Yeah I think I'm gonna close this; if you come up with a way to make better error messages I'm all about that.

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed


Note: See TracTickets for help on using tickets.