Commit Graph

228 Commits

Author SHA1 Message Date
Nikita Sobolev 395029b0bd
bpo-46571: improve `typing.no_type_check` to skip foreign objects (GH-31042)
There are several changes:
1. We now don't explicitly check for any base / sub types, because new name check covers it
2. I've also checked that `no_type_check` do not modify foreign functions. It was the same as with `type`s
3. I've also covered `except TypeError` in `no_type_check` with a simple test case, it was not covered at all
4. I also felt like adding `lambda` test is a good idea: because `lambda` is a bit of both in class bodies: a function and an assignment

<!-- issue-number: [bpo-46571](https://bugs.python.org/issue46571) -->
https://bugs.python.org/issue46571
<!-- /issue-number -->
2022-02-18 17:53:29 -08:00
aha79 6e7b813195
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>
2022-02-16 19:28:18 -08:00
97littleleaf11 de6043e596
bpo-46066: Deprecate kwargs syntax for TypedDict definitions (GH-31126)
Closes python/typing#981

https://bugs.python.org/issue46066
2022-02-16 19:26:07 -08:00
aha79 b70690bb37
bpo-46333: include `module` in `ForwardRef.__repr__` (#31283)
The module parameter carries semantic information about the forward ref.
Show to the user that forward refs with same argument but different
module are different.

Co-authored-by: Andreas Hangauer <andreas.hangauer@siemens.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2022-02-12 07:35:57 -08:00
Nikita Sobolev d2d1d49eac
bpo-46685: cover `TypeError` of `ForwardRef(1)` in `test_typing` (GH-31223) 2022-02-09 23:23:07 +09:00
Jelle Zijlstra 243436f377
bpo-46475: Add typing.Never and typing.assert_never (GH-30842) 2022-02-08 10:50:26 -08:00
Gregory Beauregard c8b62bbe46
bpo-46676: Make ParamSpec args and kwargs equal to themselves (GH-31203) 2022-02-08 09:46:58 +02:00
James Hilton-Balfe 7ba1cc8049
bpo-46534: Implement PEP 673 Self in typing.py (GH-30924)
Co-authored-by: Pradeep Kumar Srinivasan <gohanpra@gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2022-02-07 12:47:48 -08:00
Nikita Sobolev 067c03bf40
bpo-46611: add coverage to instance and class checks in `typing.py` (GH-31078) 2022-02-06 17:35:48 -08:00
Gregory Beauregard 77b025be4a
bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156) 2022-02-06 15:16:22 -08:00
Serhiy Storchaka 3da5526136
Add more tests for variable substitution in generics (GH-31170) 2022-02-06 21:52:39 +02:00
Jelle Zijlstra abcc3d75f6
bpo-46414: Add typing.reveal_type (#30646)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2022-02-01 18:48:55 -08:00
Nikita Sobolev 4c0612ad00
bpo-46482: add a test for `typing.Annotation.__new__` (GH-30821) 2022-02-01 17:58:41 +08:00
Gregory Beauregard 5445e173e7
bpo-46553: allow bare typing.ClassVar annotations (#30983)
These are used in the wild and covered by dataclasses unit tests.
Several static type checkers support this pattern.
2022-01-28 08:58:39 -08:00
Serhiy Storchaka ecfacc362d
bpo-44791: Fix substitution of ParamSpec in Concatenate with different parameter expressions (GH-27518)
* Substitution with a list of types returns now a tuple of types.
* Substitution with Concatenate returns now a Concatenate with
  concatenated lists of arguments.
* Substitution with Ellipsis is not supported.
2022-01-27 14:34:55 +02:00
Gregory Beauregard ced50051bb
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.
2022-01-26 19:11:51 -08:00
Nikita Sobolev d0c690b5f8
bpo-46529: increase coverage of `typing.Union.__repr__` method (GH-30911) 2022-01-26 18:13:02 +08:00
Gregory Beauregard e1abffca45
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
2022-01-24 22:37:15 -08:00
Nikita Sobolev 65b88d5e01
bpo-46445: Cover multiple inheritance of `TypedDict` in `test_typing` (GH-30719) 2022-01-21 13:38:23 -08:00
Nikita Sobolev 263c0dd160
bpo-46437: remove useless `hasattr` from `test_typing` (#30704) 2022-01-19 13:24:27 -08:00
Nikita Sobolev 0a49148e87
bpo-46413: properly test `__{r}or__` code paths in `_SpecialGenericAlias` (GH-30640)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2022-01-19 23:32:25 +08:00
Nikita Sobolev 2792d6d18e
bpo-46416: Allow direct invocation of `Lib/test/test_typing.py` (GH-30641)
Use `__name__`
2022-01-19 22:13:38 +08:00
Nikita Sobolev 32398294fb
bpo-46424: [typing] cover `Annotation[arg]` invalid usage in tests (GH-30663) 2022-01-18 22:43:51 +08:00
Nikita Sobolev 09087b8519
bpo-46386: improve `test_typing:test_immutability_by_copy_and_pickle` (GH-30613) 2022-01-16 16:32:11 +09:00
Nikita Sobolev 8c49d057bf
bpo-46359: Modernize `test_typing` by removing checks for EOL Python versions (GH-30563)
Also removes unused tests meant for older versions of Python.
2022-01-13 21:09:40 +08:00
Nikita Sobolev 1de60155d5
bpo-46345: Add a test case for implicit `Optional` class attribute (GH-30535) 2022-01-12 15:35:44 -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
Nikita Sobolev e2a9c8ef09
bpo-46348: modernize `test_typing` (GH-30547) 2022-01-12 08:48:10 -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
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
Nikita Sobolev 0c4c2e6213
Move several typing tests to a proper class, refs GH-28563 (GH-29126) 2021-10-21 22:16:50 +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 f56268a2cd
bpo-45280: Add test for empty `NamedTuple` in `test_typing` (GH-28559)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
2021-09-26 18:32:18 +02: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
Ken Jin 23384a1749
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841) 2021-08-25 21:13:59 +03:00
Dong-hee Na 24b63c695a
bpo-24234: Implement bytes.__bytes__ (GH-27901) 2021-08-23 19:01:51 +09:00
Mark Dickinson 6082bb5add
bpo-24234: implement complex.__complex__ (GH-27887)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
2021-08-23 09:15:49 +01: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
Serhiy Storchaka be4cb9089a
bpo-44794: Merge tests for typing.Callable and collection.abc.Callable (GH-27507) 2021-07-31 20:05:45 +03:00
Yurii Karabas 7b975f81e4
bpo-44761: Change default value of NewType __module__ attr (GH-27406) 2021-07-30 14:56:12 +02:00
Yurii Karabas 8182c8329c
bpo-44662: Add ability to annotate types.Union (#27214)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
2021-07-29 21:44:48 +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
Yurii Karabas a22b05da87
bpo-44353: Improve tests covering typing.NewType pickling (GH-27302) 2021-07-24 11:53:40 +02: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
Sebastian Rittau 4512848ab9
Fix a deprecation warning in typing tests (#27312)
The test was accessing typing.{io,re}.__all__, which triggered the
warning. This check isn't necessary anymore, since the objects from
typing.{io,re}.__all__ are in typing.__all__ as well, since Python 3.10.
2021-07-23 11:53:29 -07:00
Yurii Karabas 8f42106b5c
bpo-44353: Fix memory leak introduced by GH-27262 (GH-27305) 2021-07-23 16:28:05 +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 4868b94c60
bpo-44353: Add test to cover __or__ of two NewType (#27259) 2021-07-20 16:48:05 +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
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
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
Ikko Ashimine 35b773accb
Fix typo in test_typing.py (GH-26853)
maximium -> maximum
2021-06-22 15:42:28 -07:00
Ken Jin c5d700f0e2
Reset DeprecationWarning filters in test_typing io and re (#26811) 2021-06-21 11:17:55 -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
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
Karthikeyan Singaravelan eb77133564
bpo41515: Fix assert in test which throws SyntaxWarning. (#25379) 2021-04-13 06:54:23 -07: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 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
Victor Stinner 0ee0b2938c
bpo-41521: Replace whitelist/blacklist with allowlist/denylist (GH-21823)
Rename 5 test method names in test_codecs and test_typing.
2020-08-11 15:28:43 +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 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