Commit Graph

281 Commits

Author SHA1 Message Date
Alex Waygood 9499b0f138
gh-105974: Revert unintentional behaviour change for protocols with non-callable members and custom `__subclasshook__` methods (#105976) 2023-06-23 14:59:25 +00:00
Alex Waygood 7b1f0f204a
gh-105570: Deprecate unusual ways of creating empty TypedDicts (#105780)
Deprecate two methods of creating typing.TypedDict classes with 0 fields using the functional syntax: `TD = TypedDict("TD")` and `TD = TypedDict("TD", None)`. Both will be disallowed in Python 3.15. To create a TypedDict class with 0 fields, either use `class TD(TypedDict): pass` or `TD = TypedDict("TD", {})`.
2023-06-14 14:58:41 +00:00
Alex Waygood ad56340b66
gh-105566: Deprecate unusual ways of creating `typing.NamedTuple` classes (#105609)
Deprecate creating a typing.NamedTuple class using keyword arguments to denote the fields (`NT = NamedTuple("NT", x=int, y=str)`). This will be disallowed in Python 3.15. Use the class-based syntax or the functional syntax instead.

Two methods of creating `NamedTuple` classes with 0 fields using the functional syntax are also deprecated, and will be disallowed in Python 3.15: `NT = NamedTuple("NT")` and `NT = NamedTuple("NT", None)`. To create a `NamedTuple` class with 0 fields, either use `class NT(NamedTuple): pass` or `NT = NamedTuple("NT", [])`.
2023-06-14 13:38:49 +01:00
Jelle Zijlstra fc8037d84c
gh-104873: Add typing.get_protocol_members and typing.is_protocol (#104878)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-06-14 05:35:06 -07:00
Alex Waygood 8e755923c9
Miscellaneous improvements to the typing docs (#105529)
Mostly, these are changes so that we use shorter sentences and shorter paragraphs. In particular, I've tried to make the first sentence introducing each object in the typing API short and declarative.
2023-06-09 15:08:57 +00:00
Alex Waygood d213c2990f
Further improve docs for `typing.Annotated` (#105498) 2023-06-08 14:36:30 +00:00
Alex Waygood d63a7c3694
typing: Improve documentation of generic classes and aliases (#105369) 2023-06-07 14:02:40 +00:00
Nikita Sobolev 18309ad94b
gh-105430: Remove `typing._Immutable` unused internal helper (#105434) 2023-06-07 06:43:00 -07:00
Alex Waygood 9a89f1bf1e
gh-105286: Further improvements to `typing.py` docstrings (#105363) 2023-06-06 17:21:16 -07:00
Alex Waygood f714aa2c29
gh-105286: Improve `typing.py` docstrings (#105287)
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-06-05 14:16:09 +00:00
Alex Waygood 08756dbba6
gh-105280: Ensure `isinstance([], collections.abc.Mapping)` always evaluates to `False` (#105281) 2023-06-05 14:10:49 +00:00
Alex Waygood cdfb201bfa
gh-105237: Allow calling `issubclass(X, typing.Protocol)` again (#105239) 2023-06-05 06:36:51 -07:00
Alex Waygood c05c31db8c
gh-105144: Runtime-checkable protocols: move all 'sanity checks' to `_ProtocolMeta.__subclasscheck__` (#105152) 2023-05-31 17:02:25 +00:00
Tomas R 46857d0b2a
gh-104943: Remove mentions of old Python versions (#104945) 2023-05-25 23:34:17 -07:00
Tomas R fea8632ec6
gh-104786: Remove kwargs-based TypedDict creation (#104891)
Deprecated since Python 3.11.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-05-25 21:14:58 +01:00
Jelle Zijlstra 2b7027d0b2
gh-104935: typing: Fix interactions between `@runtime_checkable` and `Generic` (#104939)
---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-05-25 16:43:40 +00:00
Jelle Zijlstra c0ab7d401c
gh-104797: Allow Protocols to inherit from collections.abc.Buffer (#104827) 2023-05-24 08:24:53 +00:00
Sebastian Rittau abdda5b133
gh-92871: Remove typing.{io,re} namespaces (#92873)
Closes #92871

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2023-05-23 13:44:26 +00:00
Alex Waygood a412fc58cc
Improve readability of `typing._ProtocolMeta.__instancecheck__` (#104649) 2023-05-19 14:30:02 +01:00
Alex Waygood f7835fc7e9
gh-74690: Don't set special protocol attributes on non-protocol subclasses of protocols (#104622)
Don't set special protocol attributes on non-protocol subclasses of protocols
2023-05-19 00:34:40 +01:00
Alex Waygood b27fe67f3c
gh-104555: Runtime-checkable protocols: Don't let previous calls to `isinstance()` influence whether `issubclass()` raises an exception (#104559)
Co-authored-by: Carl Meyer <carl@oddbird.net>
2023-05-17 23:43:12 +00:00
Jelle Zijlstra 0cb2fdc621
typing: Use PEP 695 syntax in typing.py (#104553)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-05-17 06:03:58 -07:00
Alex Waygood 1163782868
gh-104555: Fix isinstance() and issubclass() for runtime-checkable protocols that use PEP 695 (#104556)
Fixes #104555
2023-05-16 16:38:10 +00:00
Jelle Zijlstra 24d8b88420
gh-103763: Implement PEP 695 (#103764)
This implements PEP 695, Type Parameter Syntax. It adds support for:

- Generic functions (def func[T](): ...)
- Generic classes (class X[T](): ...)
- Type aliases (type X = ...)
- New scoping when the new syntax is used within a class body
- Compiler and interpreter changes to support the new syntax and scoping rules 

Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: Eric Traut <eric@traut.com>
Co-authored-by: Larry Hastings <larry@hastings.org>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-05-15 20:36:23 -07:00
Alex Waygood c527eb1c2a
gh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (#104424) 2023-05-13 09:55:35 +01:00
Nikita Sobolev 5b8cd5abe5
gh-104415: Fix refleak tests for `typing.ByteString` deprecation (#104416) 2023-05-12 05:36:12 -07:00
Alex Waygood f0f5bb3204
gh-91896: Improve visibility of `ByteString` deprecation warnings (#104294) 2023-05-11 23:01:31 -07:00
Jelle Zijlstra 37a5d256b9
gh-104392: Remove _paramspec_tvars from typing (#104393)
This does nothing.
2023-05-11 16:41:26 -07:00
Kirill Podoprigora 01c321ca34
gh-104328: Fix typo in ``typing.Generic`` multiple inheritance error message (#104335) 2023-05-09 13:18:15 -06:00
Shantanu 82ba6ce303
Improve assert_type phrasing (#104081)
I'd like to make the fact that this does nothing at runtime
really obvious, since I suspect this is unintuitive for users who are
unfamiliar with static type checking.

I thought of this because of
https://discuss.python.org/t/add-arg-check-type-to-types/26384
wherein I'm skeptical that the user really did want `assert_type`.
2023-05-01 23:05:25 -07:00
Franek Magiera 2d526cd32f
GH-103629: Update Unpack's repr in compliance with PEP 692 (#104048) 2023-05-01 17:58:50 +00:00
Adrian Garcia Badaracco 0056701aa3
GH-103699: Add `__orig_bases__` to various typing classes (#103698)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2023-04-23 08:33:39 -06:00
Nikita Sobolev dc604a8c58
gh-97797: Mention `__metadata__` in docstrings of `typing.{_AnnotatedAlias, Annotated}` (#103405)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Kirill <80244920+Eclips4@users.noreply.github.com>
2023-04-10 15:57:17 +01:00
Alex Waygood de18267685
gh-74690: Further optimise `typing._ProtocolMeta.__instancecheck__` (#103280) 2023-04-05 17:37:36 +01:00
Alex Waygood 3246688918
gh-74690: typing: Call `_get_protocol_attrs` and `_callable_members_only` at protocol class creation time, not during `isinstance()` checks (#103160) 2023-04-05 12:19:03 +01:00
Alex Waygood 47753ecde2
gh-74690: typing: Simplify and optimise `_ProtocolMeta.__instancecheck__` (#103159) 2023-04-05 10:07:30 +01:00
Alex Waygood 6d59c9e32e
gh-102433: Use `inspect.getattr_static` in `typing._ProtocolMeta.__instancecheck__` (#103034) 2023-04-02 14:22:19 +01:00
Alex Waygood 361a3eaf1b
gh-74690: Micro-optimise `typing._get_protocol_attrs` (#103152)
Improve performance of `isinstance()` checks against runtime-checkable protocols
2023-03-31 21:54:50 +01:00
Alex Waygood 9048d73f7a
gh-74690: typing: Don't unnecessarily call `_get_protocol_attrs` twice in `_ProtocolMeta.__instancecheck__` (#103141)
Speed up `isinstance()` calls against runtime-checkable protocols
2023-03-31 18:37:24 +01:00
Nikita Sobolev adb0621652
gh-88965: typing: fix type substitution of a list of types after initial `ParamSpec` substitution (#102808)
Previously, this used to fail:

```py
from typing import *
T = TypeVar("T")
P = ParamSpec("P")

class X(Generic[P]):
    f: Callable[P, int]

Y = X[[int, T]]
Z = Y[str]
```

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-03-23 16:26:11 +00:00
Nikita Sobolev a297d59609
Add comments to `{typing,_collections_abc}._type_repr` about each other (#102752)
Remove `if` condition in `_collections_abc._type_repr` that's no longer needed, bringing it in sync with `typing._type_repr`.
2023-03-16 13:05:38 +00:00
Nikita Sobolev 2b5781d659
gh-102615: Use `list` instead of `tuple` in `repr` of paramspec (#102637)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-03-15 09:33:41 +00:00
Steven Troxler 0f89acf6cc
gh-101561: Add typing.override decorator (#101564)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2023-02-27 13:16:11 -08:00
Nikita Sobolev 807d6b576f
gh-101015: Fix `typing.get_type_hints` with unpacked `*tuple` (PEP 646) (#101031) 2023-01-23 07:52:58 +00:00
Steve Dower b5d4347950
gh-86682: Adds sys._getframemodulename as an alternative to using _getframe (GH-99520)
Also updates calls in collections, doctest, enum, and typing modules to use _getframemodulename first when available.
2023-01-13 11:31:06 +00:00
david-why 68981578ec
Correct typo in typing.py (#100423)
In the docstring of `ParamSpec`, the name of `P = ParamSpec('P')` was
mistakenly written as `'T'`.
2022-12-22 11:20:52 +00:00
Erik De Bonte 5c19050546
gh-99957: Add `frozen_default` parameter on `dataclass_transform` (#99958) 2022-12-05 19:35:43 -08:00
Wenzel Jakob c314198fad
gh-98253: Break potential reference cycles in external code worsened by typing.py lru_cache (#98591) 2022-11-30 08:33:32 +00:00
Serhiy Storchaka 8f2fb7dfe7
gh-99344, gh-99379, gh-99382: Fix issues in substitution of ParamSpec and TypeVarTuple (GH-99412)
* Fix substitution of TypeVarTuple and ParamSpec together in user generics.

* Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases.

* Check the number of arguments in substitution in user generics containing a
  TypeVarTuple and one or more TypeVar.
2022-11-29 09:46:52 +02:00
Serhiy Storchaka 0e15c31c7e
gh-98852: Fix subscription of type aliases (GH-98920)
Fix subscription of type aliases containing bare generic types or types
like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int],
where A is a generic type, and T is a type variable.
2022-11-01 09:33:09 +02:00