Skip to content
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

Fixing deepcopy for Dataset.attrs #2839

Merged
merged 7 commits into from
Mar 30, 2019
Merged

Fixing deepcopy for Dataset.attrs #2839

merged 7 commits into from
Mar 30, 2019

Conversation

kefirbandi
Copy link
Contributor

@kefirbandi kefirbandi commented Mar 22, 2019

@pep8speaks
Copy link

pep8speaks commented Mar 22, 2019

Hello @kefirbandi! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-03-25 02:33:28 UTC

@@ -1905,6 +1906,8 @@ def test_copy(self):
v1 = copied.variables[k]
assert v0 is not v1

assert data.attrs['Test'] is not copied.attrs['Test']
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 also add a test for shallow copying?

@@ -709,6 +709,7 @@ def _replace( # type: ignore
indexes: 'Optional[OrderedDict[Any, pd.Index]]' = __default,
encoding: Optional[dict] = __default,
inplace: bool = False,
deep: bool = False,
Copy link
Member

Choose a reason for hiding this comment

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

Rather than modifying _replace, could you use pass in attrs explicitly from Dataset.copy() itself?

I'd like to keep this method relatively simple -- it already has a large number of keyword arguments.

@@ -915,7 +915,9 @@ def copy(self: T, deep: bool = False, data: Mapping = None) -> T:
variables = OrderedDict((k, v.copy(deep=deep, data=data.get(k)))
for k, v in self._variables.items())

return self._replace(variables)
attrs = copy.deepcopy(self._attrs) if deep else copy.copy(self._attrs)
Copy link
Member

Choose a reason for hiding this comment

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

I made one small fix, renaming _attrs to attrs.

As a general rule, we use the underscore prefix in Python as a convention for "private" but technically public variables. This isn't necessary for local variables defined within the scope of a function, since these are already private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Actually I first called it "new_attrs" but it was 3 characters above PEP8 line-length limit, so I removed the first 3... :)
Thanks for the fix.

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.

3 participants