-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix Python2 test failures in certain locales #22213
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
Conversation
Hello @ginggs! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on August 11, 2018 at 08:35 Hours UTC |
https://travis-ci.org/pandas-dev/pandas/jobs/412589280#L3432 @ginggs : Good start! Looks like you have some lint issues though. |
Codecov Report
@@ Coverage Diff @@
## master #22213 +/- ##
==========================================
+ Coverage 92.08% 92.08% +<.01%
==========================================
Files 169 169
Lines 50694 50691 -3
==========================================
- Hits 46682 46681 -1
+ Misses 4012 4010 -2
Continue to review full report at Codecov.
|
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.
Need to add test cases for this
I don't know how we can test this, since this change is making a test in pandas more robust against things external to pandas. |
Should be able to monkey patch getlocale: |
Thanks! I'll see what I can do. |
Hmm, I seem to have reproduced this in my branch migrating us to circleci 2.0: https://circleci.com/gh/pandas-dev/pandas/16656?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link (search for unknown locale). |
@TomAugspurger it seems you have a Debian Stretch container there, and the I'm still struggling to come up with a test case that is not too contrived. |
@WillAyd I've added two test cases, and verified that the second test fails without my changes to |
These tests look good to me. Merging later today unless @WillAyd has other comments. |
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.
One minor comment
pandas/tests/util/test_util.py
Outdated
def mockgetlocale(): | ||
raise ValueError() | ||
|
||
monkeypatch.setattr(locale, 'setlocale', mocksetlocale) |
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.
Can you use the context manager for patching instead? From the monkey patch docs it is the preferred watching of patching stdlib
@WillAyd updated using monkeypatch.context(). |
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.
Context manager looks good. One question on what is being patched
@WillAyd sorry, what is the question? |
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.
Trying again - not sure what happened to my comment earlier
pandas/tests/util/test_util.py
Outdated
|
||
with monkeypatch.context() as m: | ||
m.setattr(locale, 'setlocale', mocksetlocale) | ||
m.setattr(locale, 'getlocale', mockgetlocale) |
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.
Do we need to patch this? From the comment above, shouldn't it naturally raise a ValueError after setting non-existent_locale
as the locale?
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.
If locale.Error is raised while setting non-existent_locale
, then the locale is not changed, and 'getlocale' will return the current locale.
The second test needs to simulate successfully setting non-existent_locale
and raising a ValueError so we can test the changes in pandas/util/testing.py
.
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.
Hmm so does non-existent_locale
fail when getting or setting? Reason I ask is because I think there's a better way to isolate the expected behavior here, i.e. we should only need to mock the getter or setter but not both within a given test
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.
non-existent_locale
always fails when setting.
In the second test I am trying to simulate the behaviour of en_IL
or ak_GH
or one of several others in locales-all
, which can be set, but then raise a ValueError when getlocale
is called.
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.
Right - so isn't the patch to setlocale
unnecessary? So long as getlocale
raises (as your patch causes to happen) the set behavior is more or less irrelevant (?)
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.
On my system (Ubuntu 18.04), Python2 gives me:
>>> import locale; locale.getlocale()
(None, None)
Whereas Python3 gives me:
>>> import locale; locale.getlocale()
('en_ZA', 'UTF-8')
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.
This works (in Python2 and Python3):
>>> locale.setlocale(locale.LC_ALL, '')
'en_ZA.UTF-8'
So shall we rather go with assert tm.can_set_locale('') is False
and drop the monkeypatch on setlocale
?
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.
Are you saying tm.can_set_locale(locale.getlocale())
isn't working across both versions? I believe a tuple is a valid argument. Would be cleaner to just send that value through instead of the empty string
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.
The documentation gives this an example.
https://docs.python.org/2/library/locale.html
import locale
locale.setlocale(locale.LC_ALL, '')
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.
Ah OK that's fair - thanks for pointing there!
Check that we can also get the locale, after setting it, without raising an Exception. Closes: #22129
@WillAyd reworked to drop the monkeypatch on |
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.
FYI if you merge in your updates instead of rebasing in the future it will maintain the review messages, making this process a little easier
pandas/tests/util/test_util.py
Outdated
@@ -417,6 +417,32 @@ def test_numpy_errstate_is_default(): | |||
assert np.geterr() == expected | |||
|
|||
|
|||
@td.skip_if_windows |
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.
Have these skips always been in place or is this a new addition in latest push?
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.
They've been there since I first added the tests.
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.
OK thanks. Can you move these into the TestLocaleUtils
class? It seems like they should logically be grouped in there and that class already has the decorator to skip windows.
Should be the last change - thanks for hanging with the updates!
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.
OK, will do. Unfortunately I'll only be able to get to it tomorrow.
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.
Lgtm @TomAugspurger merge away if satisfied
Thanks @ginggs! |
Note, `monkeypatch.context` is new in pytest 3.6:
https://docs.pytest.org/en/latest/changelog.html#pytest-3-6-0-2018-05-23
We should update our minimum pytest in `ci/environment-dev.yaml`
…On Mon, Aug 13, 2018 at 1:24 PM William Ayd ***@***.***> wrote:
Thanks @ginggs <https://github.com/ginggs>!
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#22213 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABQHIueJ9BQA6ZZJsO6UuBsrvq9YwD9dks5uQcRlgaJpZM4VwLDD>
.
|
@TomAugspurger good point as that was discussed in #21531 (review) can submit a PR later today to bump |
@WillAyd you are welcome, and thanks for your patience! |
Probably easiest that way. Hope to have that merged in soon - thanks! |
Backported from pandas-devgh-21739. Backport of pandas-devgh-22213.
* CI: Bump NumPy to 1.9.3 Backport of gh-22499. * BLD: Fix openpyxl to 2.5.5 Backport of gh-22601. * CI: Resolve timeout issue on Travis Backported from gh-22429. * CI: Migrate to CircleCI 2.0 Backport of gh-21814. * Upgrade Cython to >=0.28.2 Backported from gh-21688. * TST: Patch locale handling Backported from gh-21739. Backport of gh-22213.
* Fix Python2 test failures in certain locales Check that we can also get the locale, after setting it, without raising an Exception. Closes: pandas-dev#22129
Check that we can also get the locale, after setting it, without raising an Exception.
git diff upstream/master -u -- "*.py" | flake8 --diff