Skip to content

CLN: Use pandas.compat instead of sys.version_info for Python version checks #20545

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 3 commits into from
Mar 31, 2018

Conversation

jschendel
Copy link
Member

Hopefully this will make code changes related to dropping support for specific versions of Python slightly easier to identify.

Summary:

  • Replaced instances sys.version_info with equivalent checks from pandas.compat
  • Removed code blocks specific to unsupported versions of Python (<2.7, 3.0-3.4)

There were some very specific examples that I left as-is:

@pytest.mark.xfail((3, 6, 5) > sys.version_info >= (3, 5),

Didn't change any Cython related code, as it appears that we want to avoid imports from outside _libs:

# Avoid import from outside _libs
if sys.version_info.major == 2:

@@ -2088,7 +2088,7 @@ def dec(f):
# and conditionally raise on these exception types
_network_error_classes = (IOError, httplib.HTTPException)

if sys.version_info >= (3, 3):
if PY3:
Copy link
Member Author

Choose a reason for hiding this comment

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

Since 3.0 - 3.4 are no longer supported, seems fine to use PY3. Could use PY35 if being explicit would be preferred here (or maybe just leave as-is).

Copy link
Member

@gfyoung gfyoung Mar 30, 2018

Choose a reason for hiding this comment

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

I'm fine with this change. We should be encouraging code that relies solely on the supported versions of Python and not do the C-esque thing of making it super-backwards-compatible 😄

@@ -104,7 +103,7 @@ def test_cant_compare_tz_naive_w_aware(self):
pytest.raises(Exception, b.__lt__, a)
pytest.raises(Exception, b.__gt__, a)

if sys.version_info < (3, 3):
if PY2:
Copy link
Member Author

Choose a reason for hiding this comment

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

Likewise, <3.3 implies PY2 based on the currently supported versions. Could be more explicit if desired though.

Copy link
Member

@gfyoung gfyoung Mar 30, 2018

Choose a reason for hiding this comment

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

That's reasonable. 2.7, 3.5 - 3.6 are the only supported versions.

@codecov
Copy link

codecov bot commented Mar 30, 2018

Codecov Report

Merging #20545 into master will decrease coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20545      +/-   ##
==========================================
- Coverage   91.84%   91.81%   -0.03%     
==========================================
  Files         152      152              
  Lines       49268    49266       -2     
==========================================
- Hits        45250    45236      -14     
- Misses       4018     4030      +12
Flag Coverage Δ
#multiple 90.2% <100%> (-0.03%) ⬇️
#single 41.89% <100%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/io/formats/terminal.py 20.98% <100%> (ø) ⬆️
pandas/util/testing.py 84.73% <100%> (ø) ⬆️
pandas/compat/__init__.py 57.74% <100%> (ø) ⬆️
pandas/io/clipboard/clipboards.py 30.58% <100%> (-1.6%) ⬇️
pandas/plotting/_converter.py 65.07% <0%> (-1.74%) ⬇️

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 d7a4f5b...6cf2e98. Read the comment docs.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good to me.
Thanks for the clean-up!

@jreback jreback added Testing pandas testing functions or related to the test suite 2/3 Compat Compat pandas objects compatability with Numpy or Python functions labels Mar 30, 2018
@jreback jreback added this to the 0.23.0 milestone Mar 30, 2018
@jreback
Copy link
Contributor

jreback commented Mar 30, 2018

lgtm. the only thing would add is if you can fail lint.sh on a grep for sys.version_info would be great (might need to exclude the .pyx for now on that though)

@jschendel
Copy link
Member Author

I can add a check for this to lint.sh. There are a situations where sys.version_info is still valid though, as there are some very specific checks that probably don't make sense to encode in pandas.compat, eg:

@pytest.mark.xfail((3, 6, 5) > sys.version_info >= (3, 5),

This would get caught by lint.sh, but I'm guessing we'd rather be more aggressive here, and selectively allow failing cases to be merged when appropriate? I'm assuming (possibly mistakenly) that lint.sh only runs over newly added code? If it hits the entire codebase, we might not want to do this since the corner cases will fail every time. I suppose I could try checking for specific examples that are clearly invalid if this is the case, though it seems like that could get out of sync as support for versions are added/dropped.

@jreback
Copy link
Contributor

jreback commented Mar 30, 2018

we have a PY35 already. guess could just exclude this file) The lint check just enforces that the code all uses the same pattern, don't like to have people making up ways to do very standard things (and docs on how to do things like this, while helpful, get out of date), better to have an actual check.

no lint.sh runs over ALL code. as i said, you might need to exclude .pyx files as we (can't) import pandas.compat there, but otherwise I see no reason not to enforce this (and if there is a specific case pls point it out).

@jschendel
Copy link
Member Author

jschendel commented Mar 30, 2018

The example I showed is actually different than PY35. It's only skipping for 3.5-3.6.4; the test should pass for 2.7 and 3.6.5+. Something like PY2 or PY365 would work, so it'd be a matter of adding PY365.

That actually looks like the only oddly specific case of a version check. The only other non-pandas.compat instance of sys.version_info that I can't get rid of is in util/_print_versions.py:

("python", '.'.join(map(str, sys.version_info))),

Could either skip that file or create a variable like PYVERSION = sys.version_info in pandas.compat and import; seems like PYVERSION could just be abused like sys.version_info though, so will go with the skip route unless told otherwise.

Copy link
Member

@gfyoung gfyoung left a comment

Choose a reason for hiding this comment

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

Nice fixes!

@jreback

@jreback
Copy link
Contributor

jreback commented Mar 30, 2018

yeah have special cases is a bit annoying. how about you just remove the remaining ones that are possible? and leave lint.sh alone is ok.

@jschendel
Copy link
Member Author

Added some small fixes for the remaining easily removable instances of sys.version_info.

@jreback
Copy link
Contributor

jreback commented Mar 30, 2018

great. can you rebase. just merged a patch which conflicts.

@@ -1277,8 +1277,7 @@ def test_verbose_import(self):

def test_iteration_open_handle(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you change this to a pytest.mark.skip decorator

@jschendel jschendel force-pushed the compat-sys-version branch from cfa5a45 to 6cf2e98 Compare March 30, 2018 22:16
@jschendel
Copy link
Member Author

rebased and made the requested change

@jreback
Copy link
Contributor

jreback commented Mar 31, 2018

thanks!

@jreback jreback merged commit 14f03ce into pandas-dev:master Mar 31, 2018
kornilova203 pushed a commit to kornilova203/pandas that referenced this pull request Apr 23, 2018
@jschendel jschendel deleted the compat-sys-version branch September 24, 2018 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants