Commit Graph

223 Commits

Author SHA1 Message Date
Nikita Sobolev c144d93631
bpo-46470: remove unused branch from `typing._remove_dups_flatten` (GH-30780)
The branch was a remnant of old 3.6 typing.Union implementation.
2022-01-24 23:42:54 +08:00
Jelle Zijlstra 0bbf30e2b9
bpo-46342: make @typing.final introspectable (GH-30530)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2022-01-12 11:38:25 -08:00
Arie Bovenberg 081a214008
bpo-46244: Remove __slots__ from typing.TypeVar, .ParamSpec (#30444)
* add missing __slots__ to typing._TypeVarLike

* add news entry

* remove slots from _TypeVarLike base classes

* cleanup diff

* fix broken link in blurb
2022-01-11 07:43:39 +08:00
Ken Jin d6e1374716
bpo-45755: [typing] Reveal class attributes of generic in generic aliases in `dir()` (GH-29962) 2021-12-16 22:29:59 +08:00
Alex Waygood 93c65df83c
bpo-44353: Correct docstring for `NewType` (#29785)
The docstring for `typing.NewType` is no longer strictly accurate, now that `NewType` is a class, rather than a function
2021-11-25 20:55:15 -08:00
Serhiy Storchaka 634984d7db
bpo-45679: Fix caching of multi-value typing.Literal (GH-29334)
Literal[True, 2] is no longer equal to Literal[1, 2].
2021-10-31 10:22:16 +02:00
Dong-hee Na 15ad52fbf6
bpo-45489: Update ForwardRef to support | operator. (GH-28991) 2021-10-17 00:12:58 +09:00
Nikita Sobolev 784905dbef
bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-09-25 10:56:22 +02:00
Yurii Karabas c11956a8bd
bpo-45121: Fix RecursionError when calling Protocol.__init__ from a subclass' __init__ (GH-28206) 2021-09-08 18:25:09 +08:00
Yurii Karabas 0635e201be
bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-09-02 18:17:13 +02:00
Ken Jin 23384a1749
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841) 2021-08-25 21:13:59 +03:00
Serhiy Storchaka 4ceec49559
bpo-44524: Do not set _name of _SpecialForm without need (GH-27861)
Because setting non-empty _name affects behavior of other code.

In most cases __name__ can be derived from __origin__.__name__.
2021-08-21 09:47:59 +03:00
Yurii Karabas a3a4d20d67
bpo-44524: Fix cryptic TypeError message when trying to subclass special forms in `typing` (GH-27710)
This was a Python 3.9 regression.
2021-08-18 21:08:32 +02:00
Bas van Beek 8bdf12e99a
bpo-44524: Fix an issue wherein `_GenericAlias._name` was not properly set for specialforms (GH-27614)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-08-06 15:36:35 +02:00
Serhiy Storchaka 3875a69547
bpo-44801: Check arguments in substitution of ParamSpec in Callable (GH-27585) 2021-08-04 20:07:01 +02:00
Serhiy Storchaka 043cd60abe
bpo-44806: Fix __init__ in subclasses of protocols (GH-27545)
Non-protocol subclasses of protocol ignore now the __init__ method
inherited from protocol base classes.
2021-08-02 18:23:22 +02:00
Serhiy Storchaka f92b9133ef
bpo-44793: Fix checking the number of arguments when subscribe a generic type with ParamSpec parameter. (GH-27515)
For example Callable[P, T][[int], str, float] will now raise an error.

Use also term "arguments" instead of "parameters" in error
message for too few/many arguments.
2021-08-02 09:17:46 +03:00
Yurii Karabas ea4673ed07
bpo-44747: Refactor usage of sys._getframe at typing module (#27387)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-07-30 15:49:24 +02:00
Yurii Karabas 7b975f81e4
bpo-44761: Change default value of NewType __module__ attr (GH-27406) 2021-07-30 14:56:12 +02:00
Hasan 2b8ad9e6c5
bpo-44732: Rename types.Union to types.UnionType (#27342)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2021-07-26 18:00:21 +02:00
Serhiy Storchaka 435a0334d3
bpo-44676: Serialize the union type using only public API (GH-27323)
Remove also the _from_args() constructor.
2021-07-24 21:26:02 +03:00
Serhiy Storchaka 0828423127
bpo-44731: Simplify the union type implementation (GH-27318)
Remove direct support of typing types in the C code because they are already supported by defining methods __or__ and __ror__ in the Python code.
2021-07-24 14:34:48 +01:00
Serhiy Storchaka e89ef0ad2a
bpo-44353: Expand NewType tests for complex __qualname__. (#27311)
Make NewType pickleable by name.
2021-07-24 09:40:08 +02:00
Yurii Karabas fe13f0b0f6
bpo-44676: Add ability to serialize types.Union (GH-27244) 2021-07-23 11:47:00 +02:00
Yurii Karabas 96c4cbd96c
bpo-44353: Implement typing.NewType __call__ method in C (#27262)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Denis Laxalde <denis@laxalde.org>
2021-07-22 23:06:54 +02:00
Yurii Karabas 965dd76e90
bpo-44353: Refactor typing.NewType into callable class (GH-27250) 2021-07-20 15:20:38 +02:00
Yurii Karabas bce1418541
bpo-44524: Add missed __name__ and __qualname__ to typing module objects (#27237)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-07-19 19:22:12 +02:00
Serhiy Storchaka 0fd27375ca
bpo-44654: Refactor and clean up the union type implementation (GH-27196) 2021-07-17 22:44:10 +03:00
Germán Méndez Bravo 889036f7ef
bpo-41249: Fix postponed annotations for TypedDict (GH-27017)
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>
2021-07-16 20:49:30 -07:00
Yurii Karabas bf89ff96e6
bpo-44490: Improve typing module compatibility with types.Union (GH-27048) 2021-07-16 20:33:40 -07:00
Serhiy Storchaka 0cd2d51aad
bpo-44652: Preserve natural order of args in the union type. (GH-27185) 2021-07-16 16:11:30 +03:00
Sebastian Rittau 8b849ea0f3
bpo-38291: Fix a spurious warning when using help(object) (#27039)
help(object) via pydoc.TextDoc.docclass(object) iterates over the
subclasses of object, which includes typing.io and typing.re if typing
is imported. It tries to access cls.__module__ for each of those
sub-classes. This change suppresses warnings when accessing
cls.__module__.
2021-07-06 08:01:15 -07:00
will-ca 7569c0fe91
bpo-44468: Never skip base classes in `typing.get_type_hints()`, even with invalid `.__module__`. (GH-26862) 2021-06-26 16:31:32 -07:00
Sebastian Rittau 09eb817115
bpo-38291: DeprecationWarning when importing typing.{io,re} (#26719) 2021-06-19 10:31:18 -07:00
Ken Jin b2f3f8e3d8
bpo-44098: Drop ParamSpec from most ``__parameters__`` in typing generics (GH-26013)
Added two new attributes to ``_GenericAlias``: 
* ``_typevar_types``, a single type or tuple of types indicating what types are treated as a ``TypeVar``. Used for ``isinstance`` checks.
* ``_paramspec_tvars ``, a boolean flag which guards special behavior for dealing with ``ParamSpec``. Setting it to ``True`` means this  class deals with ``ParamSpec``.

Automerge-Triggered-By: GH:gvanrossum
2021-05-12 22:24:35 -07:00
Yurii Karabas c40486a32d
bpo-38908: Fix issue when non runtime_protocol failed to raise TypeError (#26067) 2021-05-12 08:47:49 -07:00
Dong-hee Na f4013eba41
typing: format patch with make patchcheck (GH-25919) 2021-05-06 02:08:20 +09:00
Jelle Zijlstra b115579734
bpo-38352: Add to typing.__all__ (#25821)
This adds IO, TextIO, BinaryIO, Match, and Pattern.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-05-04 11:29:49 +02:00
larryhastings 2f2b69855d
bpo-43901: Lazy-create an empty annotations dict in all unannotated user classes and modules (#25623)
Change class and module objects to lazy-create empty annotations dicts on demand.  The annotations dicts are stored in the object's `__dict__` for backwards compatibility.
2021-04-29 20:09:08 -07:00
Ken Jin 05ab4b60ab
bpo-43766: Implement PEP 647 (User-Defined Type Guards) in typing.py (#25282) 2021-04-27 07:31:04 -07:00
Ken Jin 1b1f9852bd
bpo-42904: Change search order of typing.get_type_hints eval (#25632)
While surprising (searching globals before locals in one specific case), this is needed for backwards compatibility.
2021-04-26 10:31:21 -07:00
Pablo Galindo b0544ba77c
bpo-38605: Revert making 'from __future__ import annotations' the default (GH-25490)
This reverts commits 044a1048ca and 1be456ae9d, adapting the code to changes that happened after it.
2021-04-21 12:41:19 +01:00
Victor Stinner a6a5c91b1e
bpo-40443: Remove unused imports (GH-25429)
* pyclbr no longer uses copy
* typing no longer uses ast

Issue discovered by pyflakes.
2021-04-16 11:26:06 +02:00
Karthikeyan Singaravelan a9cf69df2e
bpo-41515: Fix KeyError raised in get_type_hints (GH-25352)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: efahl <36704995+efahl@users.noreply.github.com>
2021-04-12 11:17:25 -07:00
Ken Jin 852150ddfe
bpo-42904: Fix get_type_hints for class local namespaces (GH-24201) 2021-04-12 10:23:12 -07:00
Jelle Zijlstra 9045919bfa
bpo-43772: Fix TypeVar.__ror__ (GH-25339) 2021-04-10 20:00:05 -07:00
Jelle Zijlstra 522433601a
bpo-43783: Add ParamSpecArgs/Kwargs (GH-25298) 2021-04-10 19:57:05 -07:00
Ken Jin ace008c531
Fix various ParamSpec errors in typing (GH-24176)
1. ParamSpec -> TypeVar for ``typing.Concatenate``
2. ParamSpec's call signature should align with its documentation.
Noticed in GH-24169
2021-01-10 16:11:41 -08:00
Ken Jin 11276cd9c4
bpo-41559: Documentation for PEP 612 (GH-24000) 2021-01-01 16:45:50 -08:00
Ross c1af128f5a
bpo-41781: Fix typo in internal function name in typing (GH-23957) 2020-12-29 13:55:28 +02:00
Ken Jin efb1f0918f
bpo-42740: Support PEP 604, 612 for typing.py get_args and get_origin (GH-23942) 2020-12-28 18:26:19 -08:00
Ken Jin 4140f10a16
bpo-42740: Fix get_args for PEP 585 collections.abc.Callable (GH-23963)
PR 1/2. Needs backport to 3.9.
2020-12-28 12:06:19 -08:00
kj 73607be686
bpo-41559: Implement PEP 612 - Add ParamSpec and Concatenate to typing (#23702) 2020-12-23 20:33:48 -08:00
kj 463c7d3d14
bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing (GH-23060) 2020-12-13 10:38:24 -08:00
Alex Grönholm 67b769f515
bpo-42059: Fix required/optional keys for TypedDict(..., total=False) (GH-22736) 2020-12-10 13:49:05 -08:00
Yurii Karabas 1b54077ff6
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 #23294.
2020-11-19 08:17:38 -08:00
Yurii Karabas f03d318ca4
bpo-42345: Fix three issues with typing.Literal parameters (GH-23294)
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.
2020-11-16 18:23:19 -08:00
kj 1f7dfb277e
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
2020-11-01 10:13:38 -08:00
Mikhail Golubev 4f3c25043d
bpo-41923: PEP 613: Add TypeAlias to typing module (#22532)
This special marker annotation is intended to help in distinguishing
proper PEP 484-compliant type aliases from regular top-level variable
assignments.
2020-10-07 14:44:31 -07:00
Batuhan Taskaya 044a1048ca
bpo-38605: Make 'from __future__ import annotations' the default (GH-20434)
The hard part was making all the tests pass; there are some subtle issues here, because apparently the future import wasn't tested very thoroughly in previous Python versions.

For example, `inspect.signature()` returned type objects normally (except for forward references), but strings with the future import. We changed it to try and return type objects by calling `typing.get_type_hints()`, but fall back on returning strings if that function fails (which it may do if there are future references in the annotations that require passing in a specific namespace to resolve).
2020-10-06 13:03:02 -07:00
Patrick Reader 0705ec8a14
bpo-41792: Add is_typeddict function to typing.py (GH-22254)
Closes issue41792.

Also closes https://github.com/python/typing/issues/751.
2020-09-15 21:58:32 -07:00
Maggie Moss 1b4552c5e8
bpo-41428: Implementation for PEP 604 (GH-21515)
See https://www.python.org/dev/peps/pep-0604/ for more information.

Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
2020-09-09 21:23:24 +01:00
Graham Bleaney 84ef33c511
Fix typo in typing.py (GH-22121)
This is a trivial PR to fix a typo in a docstring in typing.py. From reverences -> references
2020-09-08 15:41:10 -07:00
Victor Stinner 0e95bbf085
bpo-41521, typing: Rename _PROTO_WHITELIST to _PROTO_ALLOWLIST (#21825) 2020-08-12 10:53:12 +02:00
wyfo 653f420b53
bpo-41341: Recursive evaluation of ForwardRef in get_type_hints (#21553)
The issue raised by recursive evaluation is infinite recursion with
recursive types. In that case, only the first recursive ForwardRef is
evaluated.
2020-07-22 12:47:28 -07:00
Zackery Spytz 7fed75597f
bpo-39168: Remove the __new__ method of typing.Generic (GH-21327)
Automerge-Triggered-By: @gvanrossum
2020-07-04 22:07:43 -07:00
Serhiy Storchaka 2fbc57af85
bpo-40257: Tweak docstrings for special generic aliases. (GH-20022)
* Add the terminating period.
* Omit module name for builtin types.
2020-05-10 15:14:27 +03:00
Serhiy Storchaka 0122d48681
bpo-40397: Fix subscription of nested generic alias without parameters. (GH-20021) 2020-05-10 13:39:40 +03:00
Serhiy Storchaka fcb285609a
bpo-40397: Remove __args__ and __parameters__ from _SpecialGenericAlias (GH-19984) 2020-05-10 11:53:16 +03:00
Serhiy Storchaka c1c7d8ead9
bpo-40397: Refactor typing._GenericAlias (GH-19719)
Make the design more object-oriented.
Split _GenericAlias on two almost independent classes: for special
generic aliases like List and for parametrized generic aliases like List[int].
Add specialized subclasses for Callable, Callable[...], Tuple and Union[...].
2020-05-07 04:09:33 +03:00
Vlad Serebrennikov 138a9b9c2a
bpo-40389: Improve repr of typing.Optional (#19714) 2020-04-29 18:06:39 -07: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
Nickolena Fisher cfaf4c09ab
Fix typo in Lib/typing.py (GH-19717) 2020-04-26 10:49:11 -07:00
Serhiy Storchaka 40ded947f8
bpo-40336: Refactor typing._SpecialForm (GH-19620) 2020-04-23 21:26:48 +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 7e64414f57
bpo-40257: Improve help for the typing module (GH-19546)
* Show docstring for special forms.
* Show docstring for special generic aliases.
* Show documentation for __origin__ for generic aliases.
2020-04-18 17:13:21 +03:00
Serhiy Storchaka f228bf2300
bpo-40187: Refactor typing.TypedDict. (GH-19372) 2020-04-08 11:03:27 +03: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
Raymond Hettinger 1ae6445391
Convert tuples to sets for faster searches (GH-19365) 2020-04-04 11:03:04 -07:00
ananthan-123 ab6423fe2d
bpo-39572: Document ’total’ flag of TypedDict (GH-18554) 2020-02-18 20:33:05 -08: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 38aaaaac80
bpo-39491: Mention Annotated in get_origin() docstring (GH-18379)
I forgot to do it in https://github.com/python/cpython/pull/18260.
2020-02-06 17:15:12 -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
Shantanu 2e6569b669
bpo-39493: Fix definition of IO.closed in typing.py (#18265) 2020-01-29 18:52:36 -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 8252c52e57
bpo-38407: Add docstrings for typing.SupportsXXX classes. (GH-16644) 2019-10-08 16:30:17 +03:00
Serhiy Storchaka 13abda4100
bpo-38405: Make nested subclasses of typing.NamedTuple pickleable. (GH-16641) 2019-10-08 16:29:52 +03:00
HongWeipeng 6ce03ec627 cleanup ababstractproperty in typing.py (GH-16432) 2019-09-27 08:54:26 +01: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
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
Serhiy Storchaka 2085bd0877
bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-13700) 2019-06-01 11:00:15 +03: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
Anthony Sottile d30da5dd9a bpo-36983: Fix typing.__all__ and add test for exported names (GH-13456)
https://bugs.python.org/issue36983
2019-05-29 11:19:37 -07:00