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

add typing.ContextManager on all supported Python versions #422

Merged
merged 12 commits into from
May 6, 2017

Conversation

JelleZijlstra
Copy link
Member

So that there is a way to type hint for a context manager even on
language versions before 3.6.

This is just copied from contextlib.AbstractContextManager in 3.6.

Fixes #274

So that there is a way to type hint for a context manager even on
language versions before 3.6.

This is just copied from contextlib.AbstractContextManager in 3.6.

Fixes python#274
src/typing.py Outdated
def __subclasshook__(cls, C):
if cls is ContextManager:
if (any("__enter__" in B.__dict__ for B in C.__mro__) and
any("__exit__" in B.__dict__ for B in C.__mro__)):
Copy link
Member

Choose a reason for hiding this comment

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

I understand this is just copied from contextlib, but I think both should be updated to support __enter__ = None pattern, see my other comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that makes sense here, because this class will only be used in Python < 3.6, and the = None pattern became generally supported only in 3.6. The collections ABCs don't support anti-registration either before 3.6.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think that makes sense here, because this class will only be used in Python < 3.6, and the = None pattern became generally supported only in 3.6. The collections ABCs don't support anti-registration either before 3.6.

OK, if @gvanrossum agrees with this, then I think this can be merged as is, I don't have any other comments.

Copy link
Member

Choose a reason for hiding this comment

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

OK, but this is worth a comment in the code.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

I agree this is the right way forward. We may also need a tweak to PEP 484? Here are some minor style nits.

def __subclasshook__(cls, C):
if cls is ContextManager:
if (any("__enter__" in B.__dict__ for B in C.__mro__) and
any("__exit__" in B.__dict__ for B in C.__mro__)):
Copy link
Member

Choose a reason for hiding this comment

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

This is a cringe-worthy indentation (due to a stupid flake rule), since the two any() calls are actually siblings. I propose to stop contorting our code and instead disable that specific flake error. @ambv thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm pushing a commit to do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changing this required more changes in the flake8 configuration than I would have liked. To keep this PR focused, maybe we should keep the cringeworthy formatting here for now, then change the flake8 configuration in a separate PR?

src/typing.py Outdated
def __subclasshook__(cls, C):
if cls is ContextManager:
if (any("__enter__" in B.__dict__ for B in C.__mro__) and
any("__exit__" in B.__dict__ for B in C.__mro__)):
Copy link
Member

Choose a reason for hiding this comment

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

OK, but this is worth a comment in the code.

src/typing.py Outdated

@abc.abstractmethod
def __exit__(self, exc_type, exc_value, traceback):
"""Raise any exception triggered within the runtime context."""
Copy link
Member

Choose a reason for hiding this comment

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

This docstring confused me. Maybe the code is better off without it?

@JelleZijlstra
Copy link
Member Author

PEP 484 already lists ContextManager in its laundry list of ABCs provided by typing, so I think no change is needed there.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

I still don't understand why you don't just get rid of .flake8 at all and update the [flake8] in tox.ini -- in the absence of a .flake8 file flake8 will search for a [flake8] section in tox.ini so all will be good.

@JelleZijlstra
Copy link
Member Author

Oh I didn't know that, let me try it.

@gvanrossum gvanrossum merged commit 47e0860 into python:master May 6, 2017
@gvanrossum
Copy link
Member

OK, much better!

(Also would be nice to get rid of the separate .flake8-tests but that seems a ways away -- if I run flake8 on the test files with the config from tox.ini I get a ton of errors.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants