Skip to content

Conversation

@JelleZijlstra
Copy link
Member

No description provided.

Michael0x2a and others added 30 commits July 20, 2017 17:17
This pull request adds a 'typing_exensions' subproject to 'typing'. The
'typing_extensions' module backports any new additions to 'typing'
for Python 3.5+ users who are using older versions of 'typing' that were
bundled with their standard library (and so can't update to the latest
versions). As well, it will contain new experimental features than could
eventually end up in 'typing'.

See python/typing#435 for motivation and
additional context.
This puts ProtocolTests in a big ``if HAVE_PROTOCOLS: ...` branch.
…on#473)

This fixes a typo, updates required `typing` version to the latest one,
updates the contact e-mail, and adds a brief mention of `Protocol`
to the description.
…#503)

Fixes python#501

The idea is straightforward: special classes in typing_extensions
that have __extra__ should use a metaclass that fixes the problem in
GenericMeta.__subclasscheck__ on older versions of typing.

Note that overriding __subclasscheck__ tries to mimic the behaviour
in the new versions of typing. (I can't just use super().__subclasscheck__
on unaffected versions, since this changes call stack depth and therefore
breaks a sys._getframe hack on some other versions of typing.)
Fixes python#488

The fix is as proposed in python#488, i.e. by tweaking the existing
sys._getframe hack to accommodate it for this special case
(protocols in unions) on older versions of typing.
Check whether Protocol.__doc__ exists before modifying it.
Fixes the crash in typing_extensions when run with -OO flag.
The main fix is rewriting Protocol class and its metaclass.
This is a runtime counterpart of an experimental feature added to mypy in python/mypy#5522

This implementation just mimics the behaviour of `ClassVar` on all Python/`typing` versions, which is probably the most reasonable thing to do.
…ython#580)

Fixes python#579 .

When performing an `issubclass` check of a type against a protocol, the `__annotations__` member of the type is accessed and assumed to be iterable. `__annotations__` is a descriptor in the case of `types.FunctionType`, so while it is iterable when accessed on a function instance it is not iterable when accessed on the type of a function. This causes the `issubclass` check to fail with an exception.

In some cases (AFAICT, non-data protocols), an `isinstance` check of an object will use, internally, a subclass check of the object's type. As a result, `isinstance` will also fail with an exception in these conditions.

The above only seemed to occur in Python 3.

This PR fixes the issue in the Python 3 implementation while adding test coverage for both Python 2 and 3 that ensures that functions (and `types.FunctionType`) can be correctly compared both against protocols that they legitimately implement as well as those that they do not implement.
* Add Literal[...] types to typing_extensions

This diff adds a preliminary draft of literal types to
typing_extensions.

I'll edit this PR to include a link to the proposal for
Literal once I publish it sometime next week.

* Make Meta classes private
This is almost a verbatim copy of the code in `mypy_extensions` plus few small tweaks due to different imports from `typing`.
This covers python 3.53 and above. I added a fair amount of tests.
The Python 2 version is very close to the Python 3.6 code.
Fixes python/typing#643

(Use ignore whitespace option to review this, since it mostly just indents a bunch of stuff.)

PR summary:
* Update Travis config to run on a bunch of newer Python versions (including 3.8-dev).
* Only run `typing` tests on Python 3.6 or older, starting from Python 3.7 development of `typing` moved to CPython repo, where it was significantly reworked. We still run `typing_extension` tests on all versions.
* Update `repr()` tests in `typing_extensions` to conditionally expect correct module name.
* Add `TypedDict` to `__all__`.
* Use `Final`, `Literal`, etc. from `typing` if those are available (as we do for other things).
* Rename `@runtime` to `@runtime_checkable` in `typing_extensions` (I think we can keep `@runtime` in `typing_extensions` as an alias for backwards compatibility).

Note this only updates Python 3 version of `typing_extensions`. There will be a corresponding update for the Python 2 version of `typing_extensions` as part of python/typing#648
…t to typing (python#649)

Fixes python/typing#648

This essentially just moves a bunch of code and tests from Python 2 version of `typing_extensions` to Python 2 version of `typing`. We don't need to copy the definitions in the former, and can just re-export them from `typing`, since there is not built-in `typing` module on Python 2, so that we can always assume the latest version of `typing` (we will just need to release them in right order, `typing` first, and then update dependency in `typing_extensions`).

Notable changes apart from the code move:
* Like on Python 3, keep `runtime` in `typing_extensions` as an alias for `runtime_checkable` for backwards compatibility (the former is not included in `__all__`)
* Update `GenericMeta.__new__` and `GenericMeta.__getitem__` instead of almost identical overrides in `_ProtocolMeta`.
This was recently fixed in CPython repo (while adding `Protocol` there). This PR backports the fix to all the older versions including now Python 2 version in `typing`.

I also removed the unnecessary conditional import in Python 2 tests since I stumbled at it while adding the test.
JelleZijlstra and others added 27 commits February 10, 2022 18:21
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
… get_type_hints() (#1057)

* PEP 655 Add interaction w/ required/optional keys

Change TypedDict to respect keys that are marked as Required
or NotRequired (requires PEP 560).
Make TypedDict and is_typeddict accessible if typing
doesn't implement Required.

* PEP 655 Add interaction with get_type_hints()

Replace _strip_annotations() with _strip_extras() to strip Annotated, Required
and NotRequired.
Change get_type_hints() to pass include_extras=True to newer versions of
typing.get_type_hints() and use _strip_extras().
Make get_type_hints accessible if typing
doesn't implement Required.
- Add "a" to make the message grammatical
- Add a trailing period because _type_check adds another sentence after it. For example, `Unpack[3]` on 3.10 currently fails with `TypeError: Unpack accepts only single type Got 3.`.
- Use an f-string
Looks like this is in `typing.__all__` but was missed out of `typing_extensions.__all__`
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
- Defer to the PEP 646 implementation in typing.py on 3.11
- Adjust some tests accordingly. Noted a bug in
  python/cpython#32341 (comment)
- typing._type_check() is more lenient in 3.11 and no longer rejects ints
- The representation of the empty tuple type changed

Tests pass for me on a 3.11 build from today now.
The README is displayed below the file tree anyway, but this makes it
easier to explore the repository.
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.