Commit Graph

117 Commits

Author SHA1 Message Date
Miss Islington (bot) 7e55730e3f
gh-92106: Add test that subscription works on arbitrary TypedDicts (GH-92176)
(cherry picked from commit 81fb3548be)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2022-05-02 18:05:49 -07:00
Miss Islington (bot) 29ae7d3996
bpo-46333: Honor `module` parameter in ForwardRef (GH-30536)
The `module` parameter carries semantic information about the forward ref.
Forward refs are different if they refer to different module even if they
have the same name. This affects the `__eq__`, `__repr__` and `__hash__` methods.

Co-authored-by: Andreas Hangauer <andreas.hangauer@siemens.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 6e7b813195)

Co-authored-by: aha79 <34090357+aha79@users.noreply.github.com>
2022-02-16 19:53:31 -08:00
Miss Islington (bot) bde3765a3f
bpo-46685: cover `TypeError` of `ForwardRef(1)` in `test_typing` (GH-31223)
(cherry picked from commit d2d1d49eac)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-02-09 07:12:32 -08:00
Miss Islington (bot) 2532b7c820
[3.9] bpo-46482: add a test for `typing.Annotation.__new__` (GH-30821) (GH-31045)
(cherry picked from commit 4c0612ad00)


Co-authored-by: Nikita Sobolev <mail@sobolevn.me>

Automerge-Triggered-By: GH:Fidget-Spinner
2022-02-01 02:25:25 -08:00
Miss Islington (bot) 37577033ba
bpo-46539: Pass status of special typeforms to forward references (GH-30926)
Previously this didn't matter because there weren't any valid code paths
that could trigger a type check with a special form, but after the bug
fix for `Annotated` wrapping special forms it's now possible to annotate
something like `Annotated['ClassVar[int]', (3, 4)]`. This change would
also be needed for proposed future changes, such as allowing `ClassVar`
and `Final` to nest each other in dataclasses.
(cherry picked from commit ced50051bb)

Co-authored-by: Gregory Beauregard <greg@greg.red>
2022-01-27 08:47:35 -08:00
Miss Islington (bot) 29eefcc9c6
bpo-46529: increase coverage of `typing.Union.__repr__` method (GH-30911)
(cherry picked from commit d0c690b5f8)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-01-26 02:40:40 -08:00
Miss Islington (bot) b0b8388a1c
bpo-46491: Allow Annotated on outside of Final/ClassVar (GH-30864)
We treat Annotated type arg as class-level annotation. This exempts it from checks against Final and ClassVar in order to allow using them in any nesting order.

Automerge-Triggered-By: GH:gvanrossum
(cherry picked from commit e1abffca45)

Co-authored-by: Gregory Beauregard <greg@greg.red>
2022-01-25 06:39:12 -08:00
Miss Islington (bot) 3b51926ee9
bpo-46437: remove useless `hasattr` from `test_typing` (GH-30704)
(cherry picked from commit 263c0dd160)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-01-19 14:12:25 -08:00
Miss Islington (bot) 331378dffc
[3.9] bpo-46424: [typing] cover `Annotation[arg]` invalid usage in tests (GH-30663) (GH-30692)
(cherry picked from commit 32398294fb)


Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-01-19 07:59:46 -08:00
Miss Islington (bot) d9101c4e49
bpo-46345: Add a test case for implicit `Optional` class attribute (GH-30535)
(cherry picked from commit 1de60155d5)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-01-12 17:46:35 -08:00
Serhiy Storchaka bbcf06bf95
[3.9] bpo-45679: Fix caching of multi-value typing.Literal (GH-29334) (GH-29342)
Literal[True, 2] is no longer equal to Literal[1, 2]..
(cherry picked from commit 634984d7db)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-11-03 17:28:55 +08:00
Miss Islington (bot) 08e387ab82
bpo-45280: Add test for empty `NamedTuple` in `test_typing` (GH-28559) (GH-28570)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
(cherry picked from commit f56268a2cd)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2021-09-26 19:09:29 +02:00
Łukasz Langa 1f08d16c90
[3.9] bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279) (GH-28561)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>.
(cherry picked from commit 784905dbef)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2021-09-25 11:30:20 +02:00
Ken Jin 99506dcbbe
[3.9] bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) (GH-28233)
Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
2021-09-08 17:01:51 +02:00
Miss Islington (bot) 0f6a7739df
bpo-44806: Fix __init__ in subclasses of protocols (GH-27545) (GH-27559)
Non-protocol subclasses of protocol ignore now the __init__ method
inherited from protocol base classes.
(cherry picked from commit 043cd60abe)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2021-08-02 18:52:16 +02:00
Ken Jin fa674bdea3
[3.9] bpo-41249: Fix postponed annotations for TypedDict (GH-27017) (GH-27205)
This fixes TypedDict to work with get_type_hints and postponed evaluation of annotations across modules.

This is done by adding the module name to ForwardRef at the time the object is created and using that to resolve the globals during the evaluation.

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Germán Méndez Bravo <german.mb@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-07-17 11:36:38 +02:00
Ken Jin 9b90ce6850
[3.9] Revert "[3.9] bpo-38908: Fix issue when non runtime_protocol does not raise TypeError (GH-26067)" (GH-26077)
Reverts python/cpython#26075

Automerge-Triggered-By: GH:gvanrossum
2021-05-12 10:44:14 -07:00
Ken Jin 88136bbd05
[3.9] bpo-38908: Fix issue when non runtime_protocol does not raise TypeError (GH-26067) (GH-26075)
(cherry picked from commit c40486a)

Co-authored-by: Yurii Karabas 1998uriyyo@gmail.com

Automerge-Triggered-By: GH:gvanrossum
2021-05-12 10:04:43 -07:00
Miss Islington (bot) 03e571f1d5
bpo-42740: Fix get_args for PEP 585 collections.abc.Callable (GH-23963)
PR 1/2. Needs backport to 3.9.
(cherry picked from commit 4140f10a16)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2020-12-28 12:26:39 -08:00
Miss Islington (bot) dbb00062dc
bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) (GH-23747)
(cherry picked from commit 67b769f515)

Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
2020-12-14 14:33:27 -08:00
kj 33b3fedd43
[3.9] bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing (GH-23765)
Backport of GH-23060.
2020-12-14 08:30:45 -08:00
Miss Islington (bot) 2acd9d0c6c
bpo-42345: Fix hash implementation of typing.Literal (GH-23383)
Fix hash implementation of `typing.Literal`.

Update docs regarding `typing.Litaral` caching.

Base implementation was done in PR GH-23294.
(cherry picked from commit 1b54077ff6)

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
2020-11-19 08:51:01 -08:00
Yurii Karabas ac472b316c
[3.9] bpo-42345: Fix three issues with typing.Literal parameters (GH-23294) (GH-23335)
Literal equality no longer depends on the order of arguments.

Fix issue related to `typing.Literal` caching by adding `typed` parameter to `typing._tp_cache` function.

Add deduplication of `typing.Literal` arguments.

(cherry picked from commit f03d318ca4)

Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
2020-11-17 07:23:36 -08:00
Miss Islington (bot) e81e09bfc8
bpo-42233: Correctly repr GenericAlias when used with typing module (GH-23081)
Noticed by @serhiy-storchaka in the bpo.  `typing`'s types were not showing the parameterized generic.
Eg. previously:
```python
>>> typing.Union[dict[str, float], list[int]]
'typing.Union[dict, list]'
```
Now:
```python
>>> typing.Union[dict[str, float], list[int]]
'typing.Union[dict[str, float], list[int]]'
```

Automerge-Triggered-By: GH:gvanrossum
(cherry picked from commit 1f7dfb277e)

Co-authored-by: kj <28750310+Fidget-Spinner@users.noreply.github.com>
2020-11-07 08:46:08 -08:00
Miss Islington (bot) 41d1c04f73
bpo-41341: Recursive evaluation of ForwardRef in get_type_hints (GH-21553)
The issue raised by recursive evaluation is infinite recursion with
recursive types. In that case, only the first recursive ForwardRef is
evaluated.
(cherry picked from commit 653f420b53)

Co-authored-by: wyfo <joperez@hotmail.fr>
2020-07-26 08:31:24 -07:00
Miss Islington (bot) 5a1384935e
bpo-39168: Remove the __new__ method of typing.Generic (GH-21327)
Automerge-Triggered-By: @gvanrossum
(cherry picked from commit 7fed75597f)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
2020-07-05 09:02:40 -07:00
Serhiy Storchaka 0122d48681
bpo-40397: Fix subscription of nested generic alias without parameters. (GH-20021) 2020-05-10 13:39:40 +03:00
Vlad Serebrennikov 138a9b9c2a
bpo-40389: Improve repr of typing.Optional (#19714) 2020-04-29 18:06:39 -07:00
Victor Stinner 57572b103e
bpo-40443: Remove unused imports in tests (GH-19805) 2020-04-30 01:48:37 +02:00
Serhiy Storchaka 6292be7adf
bpo-40398: Fix typing.get_args() for special generic aliases. (GH-19720) 2020-04-27 10:27:21 +03:00
Serhiy Storchaka 68b352a698
bpo-40396: Support GenericAlias in the typing functions. (GH-19718) 2020-04-26 21:21:08 +03:00
HongWeipeng a25a04fea5
bpo-39942:Fix failure in `TypeVar` when missing `__name__` (GH-19616)
https://bugs.python.org/issue39942
2020-04-20 21:01:53 +01:00
Serhiy Storchaka a2ec06938f
bpo-40185: Refactor typing.NamedTuple (GH-19371) 2020-04-08 10:59:04 +03:00
Guido van Rossum 48b069a003
bpo-39481: Implementation for PEP 585 (#18239)
This implements things like `list[int]`,
which returns an object of type `types.GenericAlias`.
This object mostly acts as a proxy for `list`,
but has attributes `__origin__` and `__args__`
that allow recovering the parts (with values `list` and `(int,)`.

There is also an approximate notion of type variables;
e.g. `list[T]` has a `__parameters__` attribute equal to `(T,)`.
Type variables are objects of type `typing.TypeVar`.
2020-04-07 09:50:06 -07:00
Serhiy Storchaka 6fed3c8540
bpo-40182: Remove the _field_types attribute of the NamedTuple class (GH-19368) 2020-04-05 00:43:20 +03:00
Serhiy Storchaka a94e6272f1
bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363) 2020-04-04 21:31:30 +03:00
Furkan Önder 34b0598295
bpo-40086: Update/fix test_etree test case in test_typing (GH-19189) 2020-03-28 12:32:36 +00:00
Vlad Emelianov 10e87e5ef4
bpo-39627: Fix TypedDict totality check for inherited keys (#18503)
(Adapted from https://github.com/python/typing/pull/700)
2020-02-13 11:53:29 -08:00
Jakub Stasiak cf5b109dbb
bpo-39491: Merge PEP 593 (typing.Annotated) support (#18260)
* bpo-39491: Merge PEP 593 (typing.Annotated) support

PEP 593 has been accepted some time ago. I got a green light for merging
this from Till, so I went ahead and combined the code contributed to
typing_extensions[1] and the documentation from the PEP 593 text[2].

My changes were limited to:

* removing code designed for typing_extensions to run on older Python
  versions
* removing some irrelevant parts of the PEP text when copying it over as
  documentation and otherwise changing few small bits to better serve
  the purpose
* changing the get_type_hints signature to match reality (parameter
  names)

I wasn't entirely sure how to go about crediting the authors but I used
my best judgment, let me know if something needs changing in this
regard.

[1] 8280de241f/typing_extensions/src_py3/typing_extensions.py
[2] 17710b8798/pep-0593.rst
2020-02-04 17:10:19 -08:00
Zac Hatfield-Dodds 665ad3dfa9 Better runtime TypedDict (GH-17214)
This patch enables downstream projects inspecting a TypedDict subclass at runtime to tell which keys are optional.

This is essential for generating test data with Hypothesis or validating inputs with typeguard or pydantic.
2019-11-24 10:48:48 +00:00
benedwards14 0aca3a3a1e bpo-37838: get_type_hints for wrapped functions with forward reference (GH-17126)
https://bugs.python.org/issue37838
2019-11-21 17:24:58 +00:00
Serhiy Storchaka 13abda4100
bpo-38405: Make nested subclasses of typing.NamedTuple pickleable. (GH-16641) 2019-10-08 16:29:52 +03:00
Serhiy Storchaka 8fc5839a9d
bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). (GH-16238) 2019-09-17 22:41:55 +03:00
Serhiy Storchaka 2bf31ccab3
bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222)
This includes such names as "cls", "self", "typename", "_typename",
"fields" and "_fields".
Passing positional arguments by keyword is deprecated.
2019-09-17 21:22:00 +03:00
Zackery Spytz d057b896f9 bpo-37953: Fix deprecation warnings in test_typing (GH-16133)
self.assertEquals() is deprecated.

```
./python -We -m test test_typing
Run tests sequentially
0:00:00 load avg: 0.23 [1/1] test_typing
test test_typing failed -- Traceback (most recent call last):
  File "/home/lubuntu2/cpython/Lib/test/test_typing.py", line 2382, in test_forward_equality_gth
    self.assertEquals(Union[c1, c1_gth], Union[c1])
  File "/home/lubuntu2/cpython/Lib/unittest/case.py", line 1390, in deprecated_func
    warnings.warn(
DeprecationWarning: Please use assertEqual instead.

test_typing failed

== Tests result: FAILURE ==

1 test failed:
    test_typing

Total duration: 140 ms
Tests result: FAILURE
```



https://bugs.python.org/issue37953
2019-09-14 00:42:56 -07:00
plokmijnuhby e082e7cbe4 bpo-37953: Fix ForwardRef hash and equality checks (GH-15400)
Ideally if we stick a ForwardRef in a dictionary we would like to reliably be able to get it out again.

https://bugs.python.org/issue37953
2019-09-13 20:40:54 +01:00
Divij Rajkumar 692a0dc915 bpo-38008: Move builtin protocol whitelist to mapping instead of list (GH-15647)
Fixes https://bugs.python.org/issue38008
2019-09-12 11:13:51 +01:00
Ivan Levkivskyi 8889627b53 bpo-28556: Add a regression test to typing (GH-15396)
This adds a regression test for the issue found in the Python 2 backport, see https://github.com/python/typing/issues/656


https://bugs.python.org/issue28556
2019-08-22 10:48:01 -07:00
Benjamin Peterson 910b3fcb01
closes bpo-33758: Skip test_get_type_hints_modules_forwardref. (GH-13977)
This test "works" if things are run in the right order, so it's better to use @skip than @expectedFailure here.
2019-06-11 09:18:31 -07:00
Ivan Levkivskyi 4c23aff065
bpo-29262: Add get_origin() and get_args() introspection helpers to typing (GH-13685)
This is an old feature request that appears from time to time. After a year of experimenting with various introspection capabilities in `typing_inspect` on PyPI, I propose to add these two most commonly used functions: `get_origin()` and `get_args()`. These are essentially thin public wrappers around private APIs: `__origin__` and `__args__`.

As discussed in the issue and on the typing tracker, exposing some public helpers instead of `__origin__` and `__args__` directly will give us more flexibility if we will decide to update the internal representation, while still maintaining backwards compatibility.

The implementation is very simple an is essentially a copy from `typing_inspect` with one exception: `ClassVar` was special-cased in `typing_inspect`, but I think this special-casing doesn't really help and only makes things more complicated.
2019-05-31 00:10:07 +01:00