Commit Graph

27 Commits

Author SHA1 Message Date
Jelle Zijlstra
301325b55b Make _GeneratorContextManager covariant (#4733)
Fixes  #4732
2020-10-30 17:31:55 -07:00
Shantanu
e428f2d479 pep 484: last reexports (#4609)
Co-authored-by: hauntsaninja <>
2020-10-04 23:51:04 +02:00
Shantanu
723a23abfc contextlib: get rid of a TODO (#4610)
This dates back three years to https://github.com/python/typeshed/pull/1249

It seems pretty unused in practice, so I think this is fine:
https://grep.app/search?q=from%20contextlib%20import%20ContextManager&case=true
https://grep.app/search?q=contextlib.ContextManager&case=true

Co-authored-by: hauntsaninja <>
2020-10-04 23:49:49 +02:00
Andreas Poehlmann
10a5b070ab Fix contextlib.ContextDecorator.__call__ types (#4399)
The decorated function returned by ContextDecorator.__call__ has
the same call signature as the original function.

Closes #4382.
2020-08-06 17:26:54 -07:00
Eric Traut
04c74640f0 Removed imported symbols that are not accessed or re-exported (#4387)
Co-authored-by: Eric Traut <erictr@microsoft.com>
2020-08-05 22:49:17 -07:00
Jelle Zijlstra
5d553c9584 apply black and isort (#4287)
* apply black and isort

* move some type ignores
2020-06-28 13:31:00 -07:00
Peter Law
ed04d33def Make redirect_std{out,err} yield new stream as context var (#4285)
This matches the implementation, even though this behaviour isn't
actually documented yet. https://bugs.python.org/issue41147 aims
to fix the documentation issue though.

Fixes https://github.com/python/typeshed/issues/4283.
2020-06-28 07:36:09 -07:00
Sebastian Rittau
89d3a55f1a Preparations for the Big Reformat (#4228)
A few comments between imports were removed or moved to the top of the
import block, due to behavioral differences between black and isort. See
psf/black#251 for details.

In two instances @overloads at the top of the file needed to be moved
due to psf/black#1490.
2020-06-14 07:58:26 -07:00
Guido van Rossum
b336182b69 Fix some errors with --disallow-any-generics (#3276)
See #3267. Covers all of stdlib/2and3.
2019-09-29 09:15:27 -07:00
Andriy Teraz
d78747c0f0 Make target optional in contextlib redirecting managers (#3212)
Fixes #3210
2019-08-29 17:38:53 +02:00
Rebecca Chen
fab2ee0d7c Remove unnecessary quotes around forward references. (#3191) 2019-08-16 20:29:32 -07:00
Michael Lee
b294782183 Make most contextmanager __exit__ signatures return Optional[bool] (#3179)
This pull request is a follow-up to https://github.com/python/mypy/issues/7214.

In short, within that mypy issue, we found it would be helpful to
determine between contextmanagers that can "swallow" exceptions vs ones
that can't. This helps prevent some false positive when using flags that
analyze control flow such as `--warn-unreachable`. To do this,
Jelle proposed assuming that only contextmanagers where the `__exit__`
returns `bool` are assumed to swallow exceptions.

This unfortunately required the following typeshed changes:

1. The typing.IO, threading.Lock, and concurrent.futures.Executor
   were all modified so `__exit__` returns `Optional[None]` instead
   of None -- along with all of their subclasses.

   I believe these three types are meant to be subclassed, so I felt
   picking the more general type was correct.

2. There were also a few concrete types (e.g. see socketserver,
   subprocess, ftplib...) that I modified to return `None` -- I checked
   the source code, and these all seem to return None (and don't appear
   to be meant to be subclassable).

3. contextlib.suppress was changed to return bool. I also double-checked
   the unittest modules and modified a subset of those contextmanagers,
   leaving ones like `_AssertRaisesContext` alone.
2019-08-16 16:13:33 -07:00
Michael J. Sullivan
b3f7be523a Fix __call__ type for GeneratorContextManager (#3143) 2019-07-24 22:20:15 +02:00
Rafi Blecher
668d050476 Fix contextlib GeneratorContextManager name for py>=3.2 (#3083) 2019-07-02 10:56:40 +01:00
Brandt Bucher
0b49ce75b4 Add contextlib.nullcontext [Python 3.7]. (#2677)
Closes #2676
2018-12-08 02:55:47 +01:00
Jelle Zijlstra
bbbffb5f4b implement some minor 3.7 features (#1966)
References:
- https://docs.python.org/dev/library/datetime.html#datetime.datetime.fromisoformat
- https://docs.python.org/dev/library/crypt.html#crypt.mksalt
- https://docs.python.org/dev/library/contextlib.html#contextlib.AbstractAsyncContextManager
- https://docs.python.org/dev/library/calendar.html#calendar.HTMLCalendar.cssclasses
- https://docs.python.org/dev/library/binascii.html#binascii.b2a_uu

Part of #1965.
2018-03-22 08:01:09 -07:00
Jason Fried
f5b3979b70 contextlib.pyi ExitStack.callback take Callable (#1908)
* contextlib.pyi ExitStack.callback take Callable

The documentation for ExitStack does not specify the return type of the callable's passed to callback.  
Saying that they always return None breaks a very common use case for ExitStack, which is to "schedule" changes to a collection 
while iterating it.  

```
with ExitStack() as stack:
    for key, value in adict.items():
        if ...:
            stack.callback(adict.pop, key)
```

I also added AsyncExitStack, using Awaitable.

* Moving to Explicit Any in Generics
2018-02-24 09:41:13 -08:00
Ethan Smith
f91c891ad4 Fix name collision in contextlib on 3.7 (#1915) 2018-02-24 09:25:47 -08:00
Ilya Kulakov
1e8b953182 Add stub for AsyncExitContext added by bpo-29302. (#1876)
* Add stub for AsyncExitContext added by bpo-29302.

* ExitStack's pop_all returns an instance of type(self).
2018-02-16 17:17:17 -08:00
Elliott Beach
688f813457 fix subclassing ExitStack (#1810) 2018-01-08 06:02:30 -08:00
Jelle Zijlstra
22f47fd478 add typing.AsyncContextManager and contextlib.asynccontextmanager (#1432)
Implements:
- https://github.com/python/typing/pull/438
- https://github.com/python/cpython/pull/360

Note that https://github.com/python/cpython/pull/1412, which adds
contextlib.AbstractAsyncContextManager, has not yet been merged.
2017-06-27 10:39:40 -07:00
Matthias Kramm
a02f9b6d46 switch order of ContextManager and Generic (#1382)
Since ContextManager inherits from Generic, too, listing Generic first, in the list of base classes, results in a cyclic MRO.
2017-06-01 18:06:43 -07:00
Jelle Zijlstra
7dd2f80194 Fixes to ContextManager (#1249)
* add typing.ContextManager for 3.6+ only

This fixes the easier part of #655.

Would it make sense to add a generic typing.ContextManager that exists in any Python version?

* update comment

* fix argument types for ContextManager.__exit__

* add AsyncContextManager

* add @asynccontextmanager

* typing.ContextManager now always exists

* back out async-related changes

Will submit those in a separate PR later

* fix import order

* AbstractContextManager only exists in 3.6+

* AbstractContextManager -> ContextManager
2017-05-08 16:21:51 -07:00
nimin98
20d9fcf858 Support callable contextmanagers in contextlib (#1152)
mypy could not recognize the case when we use contextmanager as a
decorator, which has been supported since Python 3.2.

In the following code snippet,

from contextlib import contextmanager

@contextmanager
def foo(arg1):
    try:
        print(arg1)
        print('1')
        yield
    finally:
        print('2')

@foo('0')
def foo2():
    print('3')

foo2()

we get mypy error as follows,
error: ContextManager[Any] not callable

The suggested changes can fix this error and properly reflect the
updated contextmanager usage pattern.
2017-04-11 14:51:03 -07:00
Guido van Rossum
53f0ed7e68 Fix signature of nested() -- it actually returns a list of values.
To be on the conservative side I'm making it an Iterable of values;
this should be good enough for common usages.

The types of the values actually correspond to the types of the
argument ContextManagers, but a proper signature would require
variadic type variables (https://github.com/python/typing/issues/193),
which isn't worth waiting for. :-)
2016-08-02 12:03:58 -07:00
Guido van Rossum
6e596e9609 Fix decimal and contextlib (#428)
* Decimal does not support __round__, and its __abs__ returns Decimal.

* Fix contextmanager signature too.
2016-08-02 07:39:13 -07:00
Valérian Rousset
1bd78d4aab Improve contextlib (#406)
* remove old, new stubgen

* comment everything

* contextlib done

* use TypeVar instead of overload

* py2 done
2016-07-29 15:26:14 -07:00