-
Notifications
You must be signed in to change notification settings - Fork 295
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
Lazily generate US_STATES, STATE_CHOICES, and USPS_CHOICES #272
Conversation
Thanks for finding a solution for this problem. I think it makes sense to get this into the next release (which I'm planning to do soon). The workaround for the optimization issue in the Python interpreter should be part of the documentation. It doesn't have to be detailed - just the workaround would be ok. The documentation could be included with each tuple where the problem could occur or as general documentation at the top of the |
@@ -51,6 +51,8 @@ Other changes: | |||
- Ensure the migration framework generates schema migrations for model fields that change the max_length | |||
(`gh-257 <https://github.com/django/django-localflavor/pull/257>`_). Users will need to generate migrations for any | |||
model fields they use with 'makemigrations'. | |||
- Lazily generate US_STATES, STATE_CHOICES, and USPS_CHOICES | |||
(`gh-272 <https://github.com/django/django-localflavor/pull/272>`_). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a link to issue 203? You can see how to add 2 links in other changelog entries. Thanks.
Updated |
Thanks for the quick turnaround. The documentation isn't being picked up by Sphinx. For instance,
Could you look into this? You can generate the documentation by install the packages in |
Bleh. I think we're hitting sphinx-doc/sphinx#857 I've copied the documentation from us_states.py to us.rst to work around it. |
Commit 4369237 introduced lazy translations for US state names. However, US_STATES, STATE_CHOICES, and USPS_CHOICES are sorted at import time based on the translated state names. This causes two problems: 1) If localflavor.us is imported during Django initialization, then an exception will be thrown because translations cannot be made during Django initialization. (See #203) 2) When the run-time translations are different than the import-time translations, US_STATES, STATE_CHOICES, and USPS_CHOICES are not re-sorted based on the run-time translations, so those lists may be mis-sorted. This commit corrects these problems by lazily sorting US_STATES, STATE_CHOICES, and USPS_CHOICES. Unfortunately, while this works as expected in most cases, the lazy evaluation may not work as expected in every case due to internal tuple optimizations in the Python interpreter. For example, `US_STATES + ('XX', _('Select a State'))` works as expected, but `('XX', _('Select a State')) + US_STATES` throws 'TypeError: can only concatenate tuple (not "__proxy__") to tuple' because Python concatenates the tuples entirely in C code without making any Python code calls to the US_STATES object, which prevents US_STATES from being lazily generated before the concatenation is processed. To work around this, you can use a slice index to force a Python code call on the object before any other operations are processed: `('XX', _('Select a State')) + US_STATES[:]`
Thanks! |
Commit 4369237 introduced lazy translations for US state names.
However, US_STATES, STATE_CHOICES, and USPS_CHOICES are sorted at import
time based on the translated state names. This causes two problems:
exception will be thrown because translations cannot be made during
Django initialization.
(See Error - "The translation infrastructure cannot be initialized before the " #203)
translations, US_STATES, STATE_CHOICES, and USPS_CHOICES are not
re-sorted based on the run-time translations, so those lists may be
mis-sorted.
This commit corrects these problems by lazily sorting US_STATES,
STATE_CHOICES, and USPS_CHOICES. (Fixes #203)
Unfortunately, while this works as expected in most cases, the lazy
evaluation may not work as expected in every case due to internal tuple
optimizations in the Python interpreter. For example,
US_STATES + ('XX', _('Select a State'))
works as expected, but('XX', _('Select a State')) + US_STATES
throws'TypeError: can only concatenate tuple (not "proxy") to tuple'
because Python concatenates the tuples in C code without making any
Python code calls to the US_STATES object. To work around this, you can
use a slice index to force the necessary Python code calls:
('XX', _('Select a State')) + US_STATES[:]
Add an entry to the docs/changelog.rst describing the change.
Add an entry for your name in the docs/authors.rst file if it's not
already there.
Adjust your imports to a standard form by running this command: