This also fixespython/typing#512
This also fixespython/typing#511
As was discussed in both issues, some typing forms deserve to be treated
as immutable by copy and pickle modules, so that:
* copy(X) is X
* deepcopy(X) is X
* loads(dumps(X)) is X # pickled by reference
This PR adds such behaviour to:
* Type variables
* Special forms like Union, Any, ClassVar
* Unsubscripted generic aliases to containers like List, Mapping, Iterable
This not only resolves inconsistencies mentioned in the issues, but also
improves backwards compatibility with previous versions of Python
(including 3.6).
Note that this requires some dances with __module__ for type variables
(similar to NamedTuple) because the class TypeVar itself is define in typing,
while type variables should get module where they were defined.
https://bugs.python.org/issue32873
This makes the default behavior (without specifying `globalns` manually) more
predictable for users, finds the right globalns automatically.
Implementation for classes assumes has a `__module__` attribute and that module
is present in `sys.modules`. It does this recursively for all bases in the
MRO. For modules, the implementation just uses their `__dict__` directly.
This is backwards compatible, will just raise fewer exceptions in naive user
code.
Originally implemented and reviewed at https://github.com/python/typing/pull/470.
This PR contains two updates to typing module:
- Support ContextManager on all versions (original PR by Jelle Zijlstra).
- Add generic AsyncContextManager.
(Upstream is https://github.com/python/typing)
- Add TYPE_CHECKING (false at runtime, true in type checkers) (upstream #230).
- Avoid error on Union[xml.etree.cElementTree.Element, str] (upstream #229).
- Repr of Tuple[()] should be 'Tuple[()]' (upstream #231).
- Add NewType() (upstream #189).
This syncs to rev 7b43ada77821d23e55e3a4b35f6055a59b9e1ad7 there.
Summary:
- Add typing.DefaultDict (as a generic variant of collections.defaultdict).
- Use collections.Reversible if it exists (only relevant for Python 3.6).
- Revamped generic class behavior to conform to updated PEP 484.
- Improve speed of Generic.__new__.
- Make sure __init__ is called for new Generic instances. Fix issue #26391.
- Refactor async support to be compatible with 3.2, 3.3, 3.4.
- Remove 'io' and 're' from __all__ (they still exist, just not
included by "import *"). Fix issue #26234.
- Change @overload -- you can now use it outside stubs (you still
cannot call the decorated function though).