Skip to content

CLN: Remove collections.abc classes from pandas.compat #25957

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
Apr 2, 2019

Conversation

jschendel
Copy link
Member

Removes the following from pandas.compat in favor of direct imports from collections.abc:

  • Hashable
  • Iterable
  • Iterator
  • Mapping
  • MutableMapping
  • Sequence
  • Sized
  • Set

@jschendel jschendel added this to the 0.25.0 milestone Apr 2, 2019
@@ -1,6 +1,7 @@
"""
SparseArray data structure
"""
from collections.abc import Mapping
Copy link
Member Author

Choose a reason for hiding this comment

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

If it'd be preferred, I could instead follow the pattern below:

from collections import abc

...

if isinstance(mapper, abc.Mapping):
    ...

Copy link
Member

@gfyoung gfyoung Apr 2, 2019

Choose a reason for hiding this comment

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

I don't think we (as maintainers) have much of a preference on this.

My personal MO is that if you find yourself using it multiple times in the codebase, best to import directly into the namespace (fewer characters).

Copy link
Member

@gfyoung gfyoung Apr 2, 2019

Choose a reason for hiding this comment

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

From #25957 (review):

we get to the point where we are doing from typing import Iterable

@WillAyd : Hmm...are we really sure we want to do that? IMO, the word Iterable is not immediately obvious as a "type" versus the actual Iterable object itself (e.g. from collections.abc)

IMO, if it's possible, I would prefer that we namespace those.

Copy link
Member

Choose a reason for hiding this comment

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

Yea the convention for imports from typing are called out in MyPy documentation:

https://mypy.readthedocs.io/en/latest/getting_started.html#the-typing-module

Copy link
Member Author

Choose a reason for hiding this comment

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

My personal MO is that if you find yourself using it multiple times in the codebase, best to import directly into the namespace (fewer characters).

Agreed, that's my MO too. The only reason I brought it up here is because these names can be a bit ambiguous, so it might be more readable and immediately obvious to see the abc prefix. Aside from the typing conflicts there's also a conflict where we've named a custom class Iterator:

class Iterator(object):

Will leave the PR as-is for now until more people weigh in. I don't have a strong preference as long as we're consistent throughout the codebase. The first thing I'd think upon seeing Iterable would be the collections.abc version but I'm likely biased from not having done much of anything with typing.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see...sigh...it's rather unfortunate that they decided have Iterable both refer to the object as well as the type.

Fair enough, then let's go with what Python says then. However, I'm relatively certainly that we will be writing abc.Iterable many more times than we will be writing Iterable as a type hint (it's going to be more characters, I suspect 🙂)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, didn't refresh and see the link about the conventions before my last comment; abc prefix it is then.

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.

Generally looks good but I would definitely be +1 on doing from collections import abc and using the namespace prefix from there.

The main reasoning for that is that these will conflict with some items defined in the typing module, so when we get to the point where we are doing from typing import Iterable it would clash with what's done here, and I think giving preference to typing is more idiomatic

@codecov
Copy link

codecov bot commented Apr 2, 2019

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25957      +/-   ##
==========================================
+ Coverage   91.82%   91.82%   +<.01%     
==========================================
  Files         175      175              
  Lines       52581    52568      -13     
==========================================
- Hits        48280    48270      -10     
+ Misses       4301     4298       -3
Flag Coverage Δ
#multiple 90.38% <100%> (+0.01%) ⬆️
#single 41.9% <84%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/compat/__init__.py 72.85% <ø> (+2.15%) ⬆️
pandas/core/groupby/generic.py 87.1% <100%> (+0.01%) ⬆️
pandas/core/series.py 93.67% <100%> (ø) ⬆️
pandas/io/html.py 92.64% <100%> (ø) ⬆️
pandas/core/frame.py 96.79% <100%> (-0.12%) ⬇️
pandas/core/common.py 98.39% <100%> (ø) ⬆️
pandas/core/dtypes/inference.py 98.41% <100%> (ø) ⬆️
pandas/core/arrays/sparse.py 92.19% <100%> (ø) ⬆️
pandas/core/tools/datetimes.py 84.59% <100%> (ø) ⬆️
pandas/core/internals/construction.py 95.89% <100%> (ø) ⬆️
... and 4 more

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 00c119c...4a39f44. Read the comment docs.

@codecov
Copy link

codecov bot commented Apr 2, 2019

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #25957      +/-   ##
==========================================
+ Coverage   91.82%   91.82%   +<.01%     
==========================================
  Files         175      175              
  Lines       52581    52563      -18     
==========================================
- Hits        48280    48266      -14     
+ Misses       4301     4297       -4
Flag Coverage Δ
#multiple 90.38% <100%> (+0.01%) ⬆️
#single 41.89% <70.96%> (-0.08%) ⬇️
Impacted Files Coverage Δ
pandas/compat/__init__.py 72.85% <ø> (+2.15%) ⬆️
pandas/core/groupby/generic.py 87.08% <100%> (ø) ⬆️
pandas/core/sparse/series.py 93.3% <100%> (ø) ⬆️
pandas/io/html.py 92.64% <100%> (ø) ⬆️
pandas/core/frame.py 96.79% <100%> (-0.12%) ⬇️
pandas/core/common.py 98.38% <100%> (-0.01%) ⬇️
pandas/core/tools/datetimes.py 84.59% <100%> (ø) ⬆️
pandas/core/dtypes/inference.py 98.41% <100%> (ø) ⬆️
pandas/core/arrays/sparse.py 92.19% <100%> (ø) ⬆️
pandas/core/internals/construction.py 95.88% <100%> (-0.02%) ⬇️
... and 3 more

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 00c119c...a77580b. Read the comment docs.

@jschendel
Copy link
Member Author

Okay, now using the abc prefix, and updated code_checks.sh to ensure that this remains consistent in the future.

name = 'json'

try:
na_value = collections.UserDict()
na_value = UserDict()
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be changed (separate PR) as well

@jreback
Copy link
Contributor

jreback commented Apr 2, 2019

minor comment but can be a followup.,

@WillAyd WillAyd merged commit 485cbbb into pandas-dev:master Apr 2, 2019
@WillAyd
Copy link
Member

WillAyd commented Apr 2, 2019

Thanks @jschendel

@jschendel jschendel deleted the compat-remove-collections-abc branch April 2, 2019 23:41
@jschendel jschendel mentioned this pull request Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants