Opened 10 years ago

Closed 10 years ago

#23488 closed Bug (fixed)

Missing get_username in AnonymousUser

Reported by: Damien Baty Owned by: Damien Baty
Component: contrib.auth Version: 1.7
Severity: Normal Keywords:
Cc: Damien Baty Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The documentation says that "AnonymousUser is a class that implements the django.contrib.auth.models.User interface" [...] but AnonymousUser does not define the get_username() method.

I am modifying some foreign code that calls get_username() without first checking whether the user is authenticated or not. This may be bad practice, but I would expect AnonymousUser and User to have the same basic methods, including get_username(). It already has the id, pk and username attributes, as well as the groups and user_permissions accessors (that return empty sets).

Does that sound fine?

The changes are obvious. I cannot find any specific tests for the *User models that I could augment, but I do not see the point of adding an obvious unit test anyway. Would that need an addition in the documentation as well (perhaps a "versionadded" block) ?

Attachments (1)

trac_23488_anonymoususer_get_username.patch (344 bytes ) - added by Damien Baty 10 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 by Tim Graham, 10 years ago

Needs documentation: set
Needs tests: set
Triage Stage: UnreviewedAccepted

Yes, a test is required; see django.contrib.auth.tests.test_basic / test_anonymous_user. And documentation here with versionadded is a good idea. I don't think release notes are needed.

comment:2 by Damien Baty, 10 years ago

Owner: changed from nobody to Damien Baty
Status: newassigned

How, seeing the tests/ directory at the root, I missed the tests in django/contrib/auth/tests...

A PR is here: https://github.com/django/django/pull/3235 that has tests and documentation.

comment:3 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In ad491ecc6e20569a4f5c30a671a935f5c766a295:

Fixed #23488 -- Added AnonymousUser.get_username().

Note: See TracTickets for help on using tickets.
Back to Top