Skip to content

Small cleanups. #12899

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 1 commit into from
Mar 3, 2019
Merged

Small cleanups. #12899

merged 1 commit into from
Mar 3, 2019

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Nov 27, 2018

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer anntzer added this to the v3.1 milestone Nov 27, 2018
Copy link
Member

@WeatherGod WeatherGod left a comment

Choose a reason for hiding this comment

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

Haven't fully reviewed this, but I noticed several problems right away that needs to be addressed first.

Returns `True` if the given writable file-like object requires Unicode
to be written to it.
Returns whether the given writable file-like object requires Unicode to be
written to it.
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I like the original wording a bit better because it states what is returned, rather than implying a boolean result via the word "whether", which might be confusing for non-english-native readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A quick grep shows that there's a lot of places where we're using Return whether to indicate a boolean return type (though admittedly I may be responsible for quite a few of them, as I also prefer that wording). It's also quite common in the CPython docs (for example).

raise ValueError("filename must be a path")
with cbook.open_file_cm(fname_or_fh, "w", encoding="utf-8") as file:
if not cbook.file_requires_unicode(file):
file = codecs.getwriter("utf-8")(file)
Copy link
Member

Choose a reason for hiding this comment

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

Can we not clobber file like this, please?

Copy link
Member

Choose a reason for hiding this comment

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

Also, when reassigning a variable within a context like this, I don't think the close() gets called on the new object because the context manager is holding a reference to the original object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clobbering what? file is not a builtin (... anymore). And I'd say it's a much nicer variable name than fh...

The fact that the codec writer doesn't get closed doesn't matter (and anyways it will get closed by the GC), what matters is that the file itself gets closed.

import codecs
import contextlib


class filelike:  # mocks file
    def close(self):
        print("closed")


@contextlib.contextmanager
def cm():  # mocks open_file_cm
    print("enter")
    f = filelike()
    yield f
    f.close()
    print("exit")


with cm() as file:
    file = codecs.getwriter("utf-8")(file)


print("done")

prints

enter
closed
exit
done

so the file is indeed closed before the contextmanager is exited.

@jklymak
Copy link
Member

jklymak commented Feb 26, 2019

@WeatherGod you are blocking this. I don't think any great harm will be done if this is closed if you can't come to terms with @anntzer

@anntzer
Copy link
Contributor Author

anntzer commented Feb 26, 2019

I don't mind changing the PR either, if my replies above are not convincing enough...

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

With or without further changes this is fine.


Parameters
----------
v : bool
v : bool or None
If None, use :rc:`image.resample`.
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jklymak
Copy link
Member

jklymak commented Feb 27, 2019

Lets give @WeatherGod another day or two to respond. If not, I don't think any irrevocable harm is done by merging....

@jklymak jklymak dismissed WeatherGod’s stale review March 3, 2019 00:37

stale review...

@jklymak
Copy link
Member

jklymak commented Mar 3, 2019

@WeatherGod hopefully you aren't too upset, but going to merge this - I don't think the changes made here are a problem, but if you feel strongly we can revert...

@jklymak jklymak merged commit 7875bc5 into matplotlib:master Mar 3, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Mar 3, 2019
timhoffm added a commit that referenced this pull request Mar 3, 2019
…899-on-v3.1.x

Backport PR #12899 on branch v3.1.x (Small cleanups.)
@anntzer anntzer deleted the cleanup branch March 3, 2019 11:03
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.

4 participants