Commit Graph

12 Commits

Author SHA1 Message Date
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