Skip to content

cleanup inconsistently used imports #19292

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 4 commits into from
Jan 21, 2018
Merged

Conversation

jbrockmendel
Copy link
Member

There are a bunch of modules that will do both import pandas.core.common as com and from pandas.core.common import _whatever, then sprinkled throughout we'll see both _whatever and com._whatever. This PR tracks down a bunch of those and standardizes on com._whatever.

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@pep8speaks
Copy link

Hello @jbrockmendel! Thanks for submitting the PR.

Line 6358:80: E501 line too long (80 > 79 characters)

@jreback
Copy link
Contributor

jreback commented Jan 18, 2018

I have no problem with doing this, but pls add a lint rule for this, otherwise over time it will just change again.

from pandas.core.common import (_count_not_none,
_maybe_box_datetimelike, _values_from_object,
AbstractMethodError, SettingWithCopyError,
from pandas.core.common import (AbstractMethodError, SettingWithCopyError,
Copy link
Contributor

Choose a reason for hiding this comment

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

this is inconsistent with your change

@jreback jreback added Clean Code Style Code style, linting, code_checks labels Jan 18, 2018
@codecov
Copy link

codecov bot commented Jan 19, 2018

Codecov Report

Merging #19292 into master will decrease coverage by 0.02%.
The diff coverage is 82.93%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19292      +/-   ##
==========================================
- Coverage   91.53%    91.5%   -0.03%     
==========================================
  Files         150      150              
  Lines       48738    48727      -11     
==========================================
- Hits        44611    44588      -23     
- Misses       4127     4139      +12
Flag Coverage Δ
#multiple 89.87% <82.53%> (-0.03%) ⬇️
#single 41.71% <39.68%> (-0.02%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimelike.py 97.1% <0%> (-0.01%) ⬇️
pandas/core/computation/expressions.py 93.27% <100%> (ø) ⬆️
pandas/core/indexes/category.py 97.51% <100%> (ø) ⬆️
pandas/core/computation/align.py 97.89% <100%> (ø) ⬆️
pandas/io/json/table_schema.py 98.29% <100%> (ø) ⬆️
pandas/core/internals.py 94.53% <100%> (ø) ⬆️
pandas/core/nanops.py 96.68% <100%> (ø) ⬆️
pandas/core/indexing.py 92.62% <100%> (-0.01%) ⬇️
pandas/util/testing.py 84.45% <100%> (ø) ⬆️
pandas/core/indexes/datetimes.py 95.23% <100%> (-0.01%) ⬇️
... and 32 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 f0cd23c...7872f33. Read the comment docs.

# Check for imports from pandas.core.common instead
# of `import pandas.core.common as com`
echo "Check for non-standard imports"
grep -R --include="*.py*" -E "from pandas.core.common import " pandas
Copy link
Contributor

Choose a reason for hiding this comment

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

this rule includes too many things, you want to only accept from pandas.core import common as com. anything else involving pandas.core.common should error

Copy link
Member Author

Choose a reason for hiding this comment

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

This is tough to do with grep since there are things like sys.modules['pandas.core.common'], warnings.warn("pandas.core.common.{t} is deprecated, # the pandas.core.common introspectors. Even if I string together a workable grep command, maintaining it will be a PITA. I think the 1-line 98% solution is the way to go on this one.

@@ -10,7 +10,9 @@
import pandas as pd
from pandas import compat
from pandas.errors import PerformanceWarning
from pandas.core.common import flatten

import pandas.core.common as com
Copy link
Contributor

Choose a reason for hiding this comment

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

I really don't like extra blank lines between imports for the most part, occasionally for readibility

@@ -8,7 +8,9 @@

import warnings
import numpy as np
from pandas.core.common import _values_from_object

import pandas.core.common as com
Copy link
Contributor

Choose a reason for hiding this comment

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

the one after numpy is ok, but no reason for line 13

@@ -62,12 +62,6 @@
from pandas.core.dtypes.missing import isna, notna

Copy link
Contributor

Choose a reason for hiding this comment

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

extras here

@@ -8,7 +8,9 @@

import pytest
from pandas.compat import intern
from pandas.core.common import _all_none

Copy link
Contributor

Choose a reason for hiding this comment

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

i am not going to comment on all the xtra lines, but pls clean up

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. The habit (that I'll try to break) is to add lines between stdlib vs 3rd-party vs pandas-but-not-same-sub-package vs same-subpackage.

@jreback jreback added this to the 0.23.0 milestone Jan 21, 2018
@jreback jreback merged commit a9d8e04 into pandas-dev:master Jan 21, 2018
@jreback
Copy link
Contributor

jreback commented Jan 21, 2018

thanks. generally making things consistent is good. however import format is almost a style thing and can be somewhat arbitrary and is hard to enforce. so limit these type of PR's unless there is really a massive difference or dichotomy across the codebase (which as true for common)

@jbrockmendel jbrockmendel deleted the usecom branch January 23, 2018 04:40
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Nov 15, 2022

@jbrockmendel how attached are we to importing from pandas.core.common with the com alias?

Because in there is the argument com

pandas/pandas/core/generic.py

Lines 11523 to 11527 in 1f6e44a

@final
@doc(ExponentialMovingWindow)
def ewm(
self,
com: float | None = None,

which shadows the import

from pandas.core import (
algorithms as algos,
arraylike,
common as com,


If it's OK, I'd just import that one without an alias and # noqa it, as local variables shadowing global ones can potentially introduce bugs

@jbrockmendel
Copy link
Member Author

i dont feel strongly about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants