Skip to content

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

Merged
merged 2 commits into from
Aug 13, 2018
Merged

Fix Python2 test failures in certain locales #22213

merged 2 commits into from
Aug 13, 2018

Conversation

ginggs
Copy link
Contributor

@ginggs ginggs commented Aug 6, 2018

Check that we can also get the locale, after setting it, without raising an Exception.

@pep8speaks
Copy link

pep8speaks commented Aug 6, 2018

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

@gfyoung
Copy link
Member

gfyoung commented Aug 6, 2018

https://travis-ci.org/pandas-dev/pandas/jobs/412589280#L3432

@ginggs : Good start! Looks like you have some lint issues though.

@codecov
Copy link

codecov bot commented Aug 6, 2018

Codecov Report

Merging #22213 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Flag Coverage Δ
#multiple 90.49% <100%> (ø) ⬆️
#single 42.34% <100%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/util/testing.py 86.06% <100%> (+0.16%) ⬆️
pandas/core/arrays/categorical.py 95.75% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0370740...6cceebc. Read the comment docs.

Copy link
Member

@WillAyd WillAyd left a 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

@ginggs
Copy link
Contributor Author

ginggs commented Aug 8, 2018

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.

@WillAyd
Copy link
Member

WillAyd commented Aug 8, 2018

Should be able to monkey patch getlocale:

https://docs.pytest.org/en/latest/monkeypatch.html

@ginggs
Copy link
Contributor Author

ginggs commented Aug 8, 2018

Thanks! I'll see what I can do.

@TomAugspurger
Copy link
Contributor

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).

@ginggs
Copy link
Contributor Author

ginggs commented Aug 9, 2018

@TomAugspurger it seems you have a Debian Stretch container there, and the locales-all package is installed.

I'm still struggling to come up with a test case that is not too contrived.

@ginggs
Copy link
Contributor Author

ginggs commented Aug 10, 2018

@WillAyd I've added two test cases, and verified that the second test fails without my changes to pandas/util/testing.py.

@TomAugspurger
Copy link
Contributor

These tests look good to me. Merging later today unless @WillAyd has other comments.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment

def mockgetlocale():
raise ValueError()

monkeypatch.setattr(locale, 'setlocale', mocksetlocale)
Copy link
Member

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

@ginggs
Copy link
Contributor Author

ginggs commented Aug 10, 2018

@WillAyd updated using monkeypatch.context().

Copy link
Member

@WillAyd WillAyd left a 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

@ginggs
Copy link
Contributor Author

ginggs commented Aug 10, 2018

@WillAyd sorry, what is the question?

Copy link
Member

@WillAyd WillAyd left a 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


with monkeypatch.context() as m:
m.setattr(locale, 'setlocale', mocksetlocale)
m.setattr(locale, 'getlocale', mockgetlocale)
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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 (?)

Copy link
Contributor Author

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')

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Contributor Author

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, '')

Copy link
Member

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
@ginggs
Copy link
Contributor Author

ginggs commented Aug 11, 2018

@WillAyd reworked to drop the monkeypatch on setlocale, and added another test.

Copy link
Member

@WillAyd WillAyd left a 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

@@ -417,6 +417,32 @@ def test_numpy_errstate_is_default():
assert np.geterr() == expected


@td.skip_if_windows
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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!

Copy link
Contributor Author

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.

Copy link
Member

@WillAyd WillAyd left a 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

@TomAugspurger TomAugspurger merged commit ab274c2 into pandas-dev:master Aug 13, 2018
@ginggs ginggs deleted the patch-1 branch August 13, 2018 05:54
@WillAyd
Copy link
Member

WillAyd commented Aug 13, 2018

Thanks @ginggs!

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 13, 2018 via email

@WillAyd
Copy link
Member

WillAyd commented Aug 13, 2018

@TomAugspurger good point as that was discussed in #21531 (review) can submit a PR later today to bump

@ginggs
Copy link
Contributor Author

ginggs commented Aug 13, 2018

@WillAyd you are welcome, and thanks for your patience!

@WillAyd
Copy link
Member

WillAyd commented Aug 14, 2018

Just looking back here we probably want a whatsnew as well - @ginggs do you want to do that in a separate PR? Should be able to leverage the same Development section I am creating in #22320

@ginggs
Copy link
Contributor Author

ginggs commented Aug 14, 2018

@WillAyd sure, I can do that. Do I wait until #22320 is merged?

@WillAyd
Copy link
Member

WillAyd commented Aug 14, 2018

Probably easiest that way. Hope to have that merged in soon - thanks!

@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Sep 10, 2018
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 11, 2018
gfyoung added a commit that referenced this pull request Sep 11, 2018
* 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.
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python2 test_datetime_name_accessors failure with some locales
5 participants